1 use crate::config::file_lines::FileLines;
2 use crate::config::options::{IgnoreList, WidthHeuristics};
4 /// Trait for types that can be used in `Config`.
5 pub(crate) trait ConfigType: Sized {
6 /// Returns hint text for use in `Config::print_docs()`. For enum types, this is a
7 /// pipe-separated list of variants; for other types it returns "<type>".
8 fn doc_hint() -> String;
11 impl ConfigType for bool {
12 fn doc_hint() -> String {
13 String::from("<boolean>")
17 impl ConfigType for usize {
18 fn doc_hint() -> String {
19 String::from("<unsigned integer>")
23 impl ConfigType for isize {
24 fn doc_hint() -> String {
25 String::from("<signed integer>")
29 impl ConfigType for String {
30 fn doc_hint() -> String {
31 String::from("<string>")
35 impl ConfigType for FileLines {
36 fn doc_hint() -> String {
37 String::from("<json>")
41 impl ConfigType for WidthHeuristics {
42 fn doc_hint() -> String {
47 impl ConfigType for IgnoreList {
48 fn doc_hint() -> String {
49 String::from("[<string>,..]")
53 macro_rules! create_config {
54 ($($i:ident: $ty:ty, $def:expr, $stb:expr, $( $dstring:expr ),+ );+ $(;)*) => (
56 use std::collections::HashSet;
59 use serde::{Deserialize, Serialize};
62 #[allow(unreachable_pub)]
64 // For each config item, we store a bool indicating whether it has
65 // been accessed and the value, and a bool whether the option was
66 // manually initialised, or taken from the default,
67 $($i: (Cell<bool>, bool, $ty, bool)),+
70 // Just like the Config struct but with each property wrapped
71 // as Option<T>. This is used to parse a rustfmt.toml that doesn't
72 // specify all properties of `Config`.
73 // We first parse into `PartialConfig`, then create a default `Config`
74 // and overwrite the properties with corresponding values from `PartialConfig`.
75 #[derive(Deserialize, Serialize, Clone)]
76 #[allow(unreachable_pub)]
77 pub struct PartialConfig {
78 $(pub $i: Option<$ty>),+
81 // Macro hygiene won't allow us to make `set_$i()` methods on Config
82 // for each item, so this struct is used to give the API to set values:
83 // `config.set().option(false)`. It's pretty ugly. Consider replacing
84 // with `config.set_option(false)` if we ever get a stable/usable
85 // `concat_idents!()`.
86 #[allow(unreachable_pub)]
87 pub struct ConfigSetter<'a>(&'a mut Config);
89 impl<'a> ConfigSetter<'a> {
91 #[allow(unreachable_pub)]
92 pub fn $i(&mut self, value: $ty) {
93 (self.0).$i.2 = value;
94 match stringify!($i) {
96 | "use_small_heuristics"
98 | "single_line_if_else_max_width"
99 | "attr_fn_like_width"
101 | "struct_variant_width"
103 | "chain_width" => self.0.set_heuristics(),
104 "merge_imports" => self.0.set_merge_imports(),
111 // Query each option, returns true if the user set the option, false if
112 // a default was used.
113 #[allow(unreachable_pub)]
114 pub struct ConfigWasSet<'a>(&'a Config);
116 impl<'a> ConfigWasSet<'a> {
118 #[allow(unreachable_pub)]
119 pub fn $i(&self) -> bool {
127 #[allow(unreachable_pub)]
128 pub fn $i(&self) -> $ty {
134 #[allow(unreachable_pub)]
135 pub fn set(&mut self) -> ConfigSetter<'_> {
139 #[allow(unreachable_pub)]
140 pub fn was_set(&self) -> ConfigWasSet<'_> {
144 fn fill_from_parsed_config(mut self, parsed: PartialConfig, dir: &Path) -> Config {
146 if let Some(val) = parsed.$i {
151 if crate::is_nightly_channel!() {
155 eprintln!("Warning: can't set `{} = {:?}`, unstable features are only \
156 available in nightly channel.", stringify!($i), val);
161 self.set_heuristics();
162 self.set_ignore(dir);
163 self.set_merge_imports();
167 /// Returns a hash set initialized with every user-facing config option name.
169 pub(crate) fn hash_set() -> HashSet<String> {
170 let mut hash_set = HashSet::new();
172 hash_set.insert(stringify!($i).to_owned());
177 pub(crate) fn is_valid_name(name: &str) -> bool {
180 stringify!($i) => true,
186 #[allow(unreachable_pub)]
187 pub fn is_valid_key_val(key: &str, val: &str) -> bool {
190 stringify!($i) => val.parse::<$ty>().is_ok(),
196 #[allow(unreachable_pub)]
197 pub fn used_options(&self) -> PartialConfig {
200 $i: if self.$i.0.get() {
201 Some(self.$i.2.clone())
209 #[allow(unreachable_pub)]
210 pub fn all_options(&self) -> PartialConfig {
213 $i: Some(self.$i.2.clone()),
218 #[allow(unreachable_pub)]
219 pub fn override_value(&mut self, key: &str, val: &str)
225 self.$i.2 = val.parse::<$ty>()
226 .expect(&format!("Failed to parse override for {} (\"{}\") as a {}",
232 _ => panic!("Unknown config key in override: {}", key)
237 | "use_small_heuristics"
239 | "single_line_if_else_max_width"
240 | "attr_fn_like_width"
242 | "struct_variant_width"
244 | "chain_width" => self.set_heuristics(),
245 "merge_imports" => self.set_merge_imports(),
250 #[allow(unreachable_pub)]
251 pub fn is_hidden_option(name: &str) -> bool {
252 const HIDE_OPTIONS: [&str; 5] =
253 ["verbose", "verbose_diff", "file_lines", "width_heuristics", "merge_imports"];
254 HIDE_OPTIONS.contains(&name)
257 #[allow(unreachable_pub)]
258 pub fn print_docs(out: &mut dyn Write, include_unstable: bool) {
261 $( let max = cmp::max(max, stringify!($i).len()+1); )+
262 let space_str = " ".repeat(max);
263 writeln!(out, "Configuration Options:").unwrap();
265 if $stb || include_unstable {
266 let name_raw = stringify!($i);
268 if !Config::is_hidden_option(name_raw) {
269 let mut name_out = String::with_capacity(max);
270 for _ in name_raw.len()..max-1 {
273 name_out.push_str(name_raw);
275 let mut default_str = format!("{}", $def);
276 if default_str.is_empty() {
277 default_str = String::from("\"\"");
280 "{}{} Default: {}{}",
284 if !$stb { " (unstable)" } else { "" }).unwrap();
286 writeln!(out, "{}{}", space_str, $dstring).unwrap();
288 writeln!(out).unwrap();
294 fn set_width_heuristics(&mut self, heuristics: WidthHeuristics) {
295 let max_width = self.max_width.2;
296 let get_width_value = |
298 override_value: usize,
299 heuristic_value: usize,
303 return heuristic_value;
305 if override_value > max_width {
307 "`{0}` cannot have a value that exceeds `max_width`. \
308 `{0}` will be set to the same value as `max_width`",
316 let fn_call_width = get_width_value(
317 self.was_set().fn_call_width(),
318 self.fn_call_width.2,
319 heuristics.fn_call_width,
322 self.fn_call_width.2 = fn_call_width;
324 let attr_fn_like_width = get_width_value(
325 self.was_set().attr_fn_like_width(),
326 self.attr_fn_like_width.2,
327 heuristics.attr_fn_like_width,
328 "attr_fn_like_width",
330 self.attr_fn_like_width.2 = attr_fn_like_width;
332 let struct_lit_width = get_width_value(
333 self.was_set().struct_lit_width(),
334 self.struct_lit_width.2,
335 heuristics.struct_lit_width,
338 self.struct_lit_width.2 = struct_lit_width;
340 let struct_variant_width = get_width_value(
341 self.was_set().struct_variant_width(),
342 self.struct_variant_width.2,
343 heuristics.struct_variant_width,
344 "struct_variant_width",
346 self.struct_variant_width.2 = struct_variant_width;
348 let array_width = get_width_value(
349 self.was_set().array_width(),
351 heuristics.array_width,
354 self.array_width.2 = array_width;
356 let chain_width = get_width_value(
357 self.was_set().chain_width(),
359 heuristics.chain_width,
362 self.chain_width.2 = chain_width;
364 let single_line_if_else_max_width = get_width_value(
365 self.was_set().single_line_if_else_max_width(),
366 self.single_line_if_else_max_width.2,
367 heuristics.single_line_if_else_max_width,
368 "single_line_if_else_max_width",
370 self.single_line_if_else_max_width.2 = single_line_if_else_max_width;
373 fn set_heuristics(&mut self) {
374 let max_width = self.max_width.2;
375 match self.use_small_heuristics.2 {
376 Heuristics::Default =>
377 self.set_width_heuristics(WidthHeuristics::scaled(max_width)),
378 Heuristics::Max => self.set_width_heuristics(WidthHeuristics::set(max_width)),
379 Heuristics::Off => self.set_width_heuristics(WidthHeuristics::null()),
383 fn set_ignore(&mut self, dir: &Path) {
384 self.ignore.2.add_prefix(dir);
387 fn set_merge_imports(&mut self) {
388 if self.was_set().merge_imports() {
390 "Warning: the `merge_imports` option is deprecated. \
391 Use `imports_granularity=\"Crate\"` instead"
393 if !self.was_set().imports_granularity() {
394 self.imports_granularity.2 = if self.merge_imports() {
395 ImportGranularity::Crate
397 ImportGranularity::Preserve
403 #[allow(unreachable_pub)]
404 /// Returns `true` if the config key was explicitly set and is the default value.
405 pub fn is_default(&self, key: &str) -> bool {
407 if let stringify!($i) = key {
408 return self.$i.1 && self.$i.2 == $def;
415 // Template for the default configuration
416 impl Default for Config {
417 fn default() -> Config {
420 $i: (Cell::new(false), false, $def, $stb),