use crate::config::options::{IgnoreList, WidthHeuristics};
/// Trait for types that can be used in `Config`.
-pub trait ConfigType: Sized {
+pub(crate) trait ConfigType: Sized {
/// Returns hint text for use in `Config::print_docs()`. For enum types, this is a
/// pipe-separated list of variants; for other types it returns "<type>".
fn doc_hint() -> String;
}
}
-/// Checks if we're in a nightly build.
-///
-/// The environment variable `CFG_RELEASE_CHANNEL` is set during the rustc bootstrap
-/// to "stable", "beta", or "nightly" depending on what toolchain is being built.
-/// If we are being built as part of the stable or beta toolchains, we want
-/// to disable unstable configuration options.
-///
-/// If we're being built by cargo (e.g., `cargo +nightly install rustfmt-nightly`),
-/// `CFG_RELEASE_CHANNEL` is not set. As we only support being built against the
-/// nightly compiler when installed from crates.io, default to nightly mode.
-macro_rules! is_nightly_channel {
- () => {
- option_env!("CFG_RELEASE_CHANNEL").map_or(true, |c| c == "nightly" || c == "dev")
- };
-}
-
macro_rules! create_config {
($($i:ident: $ty:ty, $def:expr, $stb:expr, $( $dstring:expr ),+ );+ $(;)*) => (
#[cfg(test)]
use std::collections::HashSet;
use std::io::Write;
+ use serde::{Deserialize, Serialize};
+
#[derive(Clone)]
+ #[allow(unreachable_pub)]
pub struct Config {
// if a license_template_path has been specified, successfully read, parsed and compiled
// into a regex, it will be stored here
// We first parse into `PartialConfig`, then create a default `Config`
// and overwrite the properties with corresponding values from `PartialConfig`.
#[derive(Deserialize, Serialize, Clone)]
+ #[allow(unreachable_pub)]
pub struct PartialConfig {
$(pub $i: Option<$ty>),+
}
// `config.set().option(false)`. It's pretty ugly. Consider replacing
// with `config.set_option(false)` if we ever get a stable/usable
// `concat_idents!()`.
+ #[allow(unreachable_pub)]
pub struct ConfigSetter<'a>(&'a mut Config);
impl<'a> ConfigSetter<'a> {
$(
+ #[allow(unreachable_pub)]
pub fn $i(&mut self, value: $ty) {
(self.0).$i.2 = value;
match stringify!($i) {
// Query each option, returns true if the user set the option, false if
// a default was used.
+ #[allow(unreachable_pub)]
pub struct ConfigWasSet<'a>(&'a Config);
impl<'a> ConfigWasSet<'a> {
$(
+ #[allow(unreachable_pub)]
pub fn $i(&self) -> bool {
(self.0).$i.1
}
impl Config {
$(
+ #[allow(unreachable_pub)]
pub fn $i(&self) -> $ty {
self.$i.0.set(true);
self.$i.2.clone()
}
)+
+ #[allow(unreachable_pub)]
pub fn set(&mut self) -> ConfigSetter<'_> {
ConfigSetter(self)
}
+ #[allow(unreachable_pub)]
pub fn was_set(&self) -> ConfigWasSet<'_> {
ConfigWasSet(self)
}
- fn fill_from_parsed_config(mut self, parsed: PartialConfig) -> Config {
+ fn fill_from_parsed_config(mut self, parsed: PartialConfig, dir: &Path) -> Config {
$(
if let Some(val) = parsed.$i {
if self.$i.3 {
self.$i.1 = true;
self.$i.2 = val;
} else {
- if is_nightly_channel!() {
+ if crate::is_nightly_channel!() {
self.$i.1 = true;
self.$i.2 = val;
} else {
)+
self.set_heuristics();
self.set_license_template();
+ self.set_ignore(dir);
self
}
}
}
+ #[allow(unreachable_pub)]
pub fn used_options(&self) -> PartialConfig {
PartialConfig {
$(
}
}
+ #[allow(unreachable_pub)]
pub fn all_options(&self) -> PartialConfig {
PartialConfig {
$(
}
}
+ #[allow(unreachable_pub)]
pub fn override_value(&mut self, key: &str, val: &str)
{
match key {
}
}
+ #[allow(unreachable_pub)]
pub fn is_hidden_option(name: &str) -> bool {
const HIDE_OPTIONS: [&str; 4] =
["verbose", "verbose_diff", "file_lines", "width_heuristics"];
HIDE_OPTIONS.contains(&name)
}
+ #[allow(unreachable_pub)]
pub fn print_docs(out: &mut dyn Write, include_unstable: bool) {
use std::cmp;
let max = 0;
}
name_out.push_str(name_raw);
name_out.push(' ');
+ let mut default_str = format!("{}", $def);
+ if default_str.is_empty() {
+ default_str = String::from("\"\"");
+ }
writeln!(out,
- "{}{} Default: {:?}{}",
+ "{}{} Default: {}{}",
name_out,
<$ty>::doc_hint(),
- $def,
+ default_str,
if !$stb { " (unstable)" } else { "" }).unwrap();
$(
writeln!(out, "{}{}", space_str, $dstring).unwrap();
}
}
+ fn set_ignore(&mut self, dir: &Path) {
+ self.ignore.2.add_prefix(dir);
+ }
+ #[allow(unreachable_pub)]
/// Returns `true` if the config key was explicitly set and is the default value.
pub fn is_default(&self, key: &str) -> bool {
$(