1use crate::config::file_lines::FileLines;
2use crate::config::macro_names::MacroSelectors;
3use crate::config::options::{IgnoreList, WidthHeuristics};
4
5pub(crate) trait ConfigType: Sized {
7 fn doc_hint() -> String;
10
11 fn stable_variant(&self) -> bool {
16 true
17 }
18}
19
20impl ConfigType for bool {
21 fn doc_hint() -> String {
22 String::from("<boolean>")
23 }
24}
25
26impl ConfigType for usize {
27 fn doc_hint() -> String {
28 String::from("<unsigned integer>")
29 }
30}
31
32impl ConfigType for isize {
33 fn doc_hint() -> String {
34 String::from("<signed integer>")
35 }
36}
37
38impl ConfigType for String {
39 fn doc_hint() -> String {
40 String::from("<string>")
41 }
42}
43
44impl ConfigType for FileLines {
45 fn doc_hint() -> String {
46 String::from("<json>")
47 }
48}
49
50impl ConfigType for MacroSelectors {
51 fn doc_hint() -> String {
52 String::from("[<string>, ...]")
53 }
54}
55
56impl ConfigType for WidthHeuristics {
57 fn doc_hint() -> String {
58 String::new()
59 }
60}
61
62impl ConfigType for IgnoreList {
63 fn doc_hint() -> String {
64 String::from("[<string>,..]")
65 }
66}
67
68macro_rules! create_config {
69 ($($i:ident: $ty:ty, $stb:expr, $( $dstring:expr ),+ );+ $(;)*) => (
76 #[cfg(test)]
77 use std::collections::HashSet;
78 use std::io::Write;
79
80 use serde::{Deserialize, Serialize};
81 use $crate::config::style_edition::StyleEditionDefault;
82
83 #[derive(Clone)]
84 #[allow(unreachable_pub)]
85 pub struct Config {
86 $($i: (Cell<bool>, bool, <$ty as StyleEditionDefault>::ConfigType, bool, bool)),+
96 }
97
98 #[derive(Deserialize, Serialize, Clone)]
104 #[allow(unreachable_pub)]
105 pub struct PartialConfig {
106 $(pub $i: Option<<$ty as StyleEditionDefault>::ConfigType>),+
107 }
108
109 #[allow(unreachable_pub)]
115 pub struct ConfigSetter<'a>(&'a mut Config);
116
117 impl<'a> ConfigSetter<'a> {
118 $(
119 #[allow(unreachable_pub)]
120 pub fn $i(&mut self, value: <$ty as StyleEditionDefault>::ConfigType) {
121 (self.0).$i.2 = value;
122 match stringify!($i) {
123 "max_width"
124 | "use_small_heuristics"
125 | "fn_call_width"
126 | "single_line_if_else_max_width"
127 | "single_line_let_else_max_width"
128 | "attr_fn_like_width"
129 | "struct_lit_width"
130 | "struct_variant_width"
131 | "array_width"
132 | "chain_width" => self.0.set_heuristics(),
133 "merge_imports" => self.0.set_merge_imports(),
134 "fn_args_layout" => self.0.set_fn_args_layout(),
135 "hide_parse_errors" => self.0.set_hide_parse_errors(),
136 "version" => self.0.set_version(),
137 &_ => (),
138 }
139 }
140 )+
141 }
142
143 #[allow(unreachable_pub)]
144 pub struct CliConfigSetter<'a>(&'a mut Config);
145
146 impl<'a> CliConfigSetter<'a> {
147 $(
148 #[allow(unreachable_pub)]
149 pub fn $i(&mut self, value: <$ty as StyleEditionDefault>::ConfigType) {
150 (self.0).$i.2 = value;
151 (self.0).$i.4 = true;
152 match stringify!($i) {
153 "max_width"
154 | "use_small_heuristics"
155 | "fn_call_width"
156 | "single_line_if_else_max_width"
157 | "single_line_let_else_max_width"
158 | "attr_fn_like_width"
159 | "struct_lit_width"
160 | "struct_variant_width"
161 | "array_width"
162 | "chain_width" => self.0.set_heuristics(),
163 "merge_imports" => self.0.set_merge_imports(),
164 "fn_args_layout" => self.0.set_fn_args_layout(),
165 "hide_parse_errors" => self.0.set_hide_parse_errors(),
166 "version" => self.0.set_version(),
167 &_ => (),
168 }
169 }
170 )+
171 }
172
173 #[allow(unreachable_pub)]
176 pub struct ConfigWasSet<'a>(&'a Config);
177
178 impl<'a> ConfigWasSet<'a> {
179 $(
180 #[allow(unreachable_pub)]
181 pub fn $i(&self) -> bool {
182 (self.0).$i.1
183 }
184 )+
185 }
186
187 #[allow(unreachable_pub)]
190 pub struct CliConfigWasSet<'a>(&'a Config);
191
192 impl<'a> CliConfigWasSet<'a> {
193 $(
194 #[allow(unreachable_pub)]
195 pub fn $i(&self) -> bool {
196 (self.0).$i.4
197 }
198 )+
199 }
200
201 impl Config {
202 $(
203 #[allow(unreachable_pub)]
204 pub fn $i(&self) -> <$ty as StyleEditionDefault>::ConfigType {
205 self.$i.0.set(true);
206 self.$i.2.clone()
207 }
208 )+
209
210 #[allow(unreachable_pub)]
211 pub(super) fn default_with_style_edition(style_edition: StyleEdition) -> Config {
212 Config {
213 $(
214 $i: (
215 Cell::new(false),
216 false,
217 <$ty as StyleEditionDefault>::style_edition_default(
218 style_edition
219 ),
220 $stb,
221 false,
222 ),
223 )+
224 }
225 }
226
227 #[allow(unreachable_pub)]
228 pub fn set(&mut self) -> ConfigSetter<'_> {
229 ConfigSetter(self)
230 }
231
232 #[allow(unreachable_pub)]
233 pub fn set_cli(&mut self) -> CliConfigSetter<'_> {
234 CliConfigSetter(self)
235 }
236
237 #[allow(unreachable_pub)]
238 pub fn was_set(&self) -> ConfigWasSet<'_> {
239 ConfigWasSet(self)
240 }
241
242 #[allow(unreachable_pub)]
243 pub fn was_set_cli(&self) -> CliConfigWasSet<'_> {
244 CliConfigWasSet(self)
245 }
246
247 #[allow(unreachable_pub)]
248 pub fn was_option_set(&self, key: &str) -> bool {
249 match key {
250 $(
251 stringify!($i) => {
252 ConfigWasSet(self).$i()
253 }
254 )+
255 _ => false
256 }
257 }
258
259 fn fill_from_parsed_config(mut self, parsed: PartialConfig, dir: &Path) -> Config {
260 $(
261 if let Some(option_value) = parsed.$i {
262 let option_stable = self.$i.3;
263 if $crate::config::config_type::is_stable_option_and_value(
264 stringify!($i), option_stable, &option_value
265 ) {
266 self.$i.1 = true;
267 self.$i.2 = option_value;
268 }
269 }
270 )+
271 self.set_heuristics();
272 self.set_ignore(dir);
273 self.set_merge_imports();
274 self.set_fn_args_layout();
275 self.set_hide_parse_errors();
276 self.set_version();
277 self
278 }
279
280 #[cfg(test)]
282 pub(crate) fn hash_set() -> HashSet<String> {
283 let mut hash_set = HashSet::new();
284 $(
285 hash_set.insert(stringify!($i).to_owned());
286 )+
287 hash_set
288 }
289
290 pub(crate) fn is_valid_name(name: &str) -> bool {
291 match name {
292 $(
293 stringify!($i) => true,
294 )+
295 _ => false,
296 }
297 }
298
299 #[allow(unreachable_pub)]
300 pub fn is_valid_key_val(key: &str, val: &str) -> bool {
301 match key {
302 $(
303 stringify!($i) => {
304 val.parse::<<$ty as StyleEditionDefault>::ConfigType>().is_ok()
305 }
306 )+
307 _ => false,
308 }
309 }
310
311 #[allow(unreachable_pub)]
312 pub fn used_options(&self) -> PartialConfig {
313 PartialConfig {
314 $(
315 $i: if self.$i.0.get() {
316 Some(self.$i.2.clone())
317 } else {
318 None
319 },
320 )+
321 }
322 }
323
324 #[allow(unreachable_pub)]
325 pub fn all_options(&self) -> PartialConfig {
326 PartialConfig {
327 $(
328 $i: Some(self.$i.2.clone()),
329 )+
330 }
331 }
332
333 #[allow(unreachable_pub)]
334 pub fn override_value(&mut self, key: &str, val: &str)
335 {
336 match key {
337 $(
338 stringify!($i) => {
339 let value = val.parse::<<$ty as StyleEditionDefault>::ConfigType>()
340 .expect(
341 &format!(
342 "Failed to parse override for {} (\"{}\") as a {}",
343 stringify!($i),
344 val,
345 stringify!(<$ty as StyleEditionDefault>::ConfigType)
346 )
347 );
348
349 self.$i.1 = true;
358 self.$i.2 = value;
359 }
360 )+
361 _ => panic!("Unknown config key in override: {}", key)
362 }
363
364 match key {
365 "max_width"
366 | "use_small_heuristics"
367 | "fn_call_width"
368 | "single_line_if_else_max_width"
369 | "single_line_let_else_max_width"
370 | "attr_fn_like_width"
371 | "struct_lit_width"
372 | "struct_variant_width"
373 | "array_width"
374 | "chain_width" => self.set_heuristics(),
375 "merge_imports" => self.set_merge_imports(),
376 "fn_args_layout" => self.set_fn_args_layout(),
377 "hide_parse_errors" => self.set_hide_parse_errors(),
378 "version" => self.set_version(),
379 &_ => (),
380 }
381 }
382
383 #[allow(unreachable_pub)]
384 pub fn is_hidden_option(name: &str) -> bool {
385 const HIDE_OPTIONS: [&str; 7] = [
386 "verbose",
387 "verbose_diff",
388 "file_lines",
389 "width_heuristics",
390 "merge_imports",
391 "fn_args_layout",
392 "hide_parse_errors"
393 ];
394 HIDE_OPTIONS.contains(&name)
395 }
396
397 #[allow(unreachable_pub)]
398 pub fn print_docs(out: &mut dyn Write, include_unstable: bool) {
399 let style_edition = StyleEdition::Edition2015;
400 use std::cmp;
401 let max = 0;
402 $( let max = cmp::max(max, stringify!($i).len()+1); )+
403 let space_str = " ".repeat(max);
404 writeln!(out, "Configuration Options:").unwrap();
405 $(
406 if $stb || include_unstable {
407 let name_raw = stringify!($i);
408
409 if !Config::is_hidden_option(name_raw) {
410 let mut name_out = String::with_capacity(max);
411 for _ in name_raw.len()..max-1 {
412 name_out.push(' ')
413 }
414 name_out.push_str(name_raw);
415 name_out.push(' ');
416 let default_value = <$ty as StyleEditionDefault>::style_edition_default(
417 style_edition
418 );
419 let mut default_str = format!("{}", default_value);
420 if default_str.is_empty() {
421 default_str = String::from("\"\"");
422 }
423 writeln!(out,
424 "{}{} Default: {}{}",
425 name_out,
426 <<$ty as StyleEditionDefault>::ConfigType>::doc_hint(),
427 default_str,
428 if !$stb { " (unstable)" } else { "" }).unwrap();
429 $(
430 writeln!(out, "{}{}", space_str, $dstring).unwrap();
431 )+
432 writeln!(out).unwrap();
433 }
434 }
435 )+
436 }
437
438 fn set_width_heuristics(&mut self, heuristics: WidthHeuristics) {
439 let max_width = self.max_width.2;
440 let get_width_value = |
441 was_set: bool,
442 override_value: usize,
443 heuristic_value: usize,
444 config_key: &str,
445 | -> usize {
446 if !was_set {
447 return heuristic_value;
448 }
449 if override_value > max_width {
450 eprintln!(
451 "`{0}` cannot have a value that exceeds `max_width`. \
452 `{0}` will be set to the same value as `max_width`",
453 config_key,
454 );
455 return max_width;
456 }
457 override_value
458 };
459
460 let fn_call_width = get_width_value(
461 self.was_set().fn_call_width(),
462 self.fn_call_width.2,
463 heuristics.fn_call_width,
464 "fn_call_width",
465 );
466 self.fn_call_width.2 = fn_call_width;
467
468 let attr_fn_like_width = get_width_value(
469 self.was_set().attr_fn_like_width(),
470 self.attr_fn_like_width.2,
471 heuristics.attr_fn_like_width,
472 "attr_fn_like_width",
473 );
474 self.attr_fn_like_width.2 = attr_fn_like_width;
475
476 let struct_lit_width = get_width_value(
477 self.was_set().struct_lit_width(),
478 self.struct_lit_width.2,
479 heuristics.struct_lit_width,
480 "struct_lit_width",
481 );
482 self.struct_lit_width.2 = struct_lit_width;
483
484 let struct_variant_width = get_width_value(
485 self.was_set().struct_variant_width(),
486 self.struct_variant_width.2,
487 heuristics.struct_variant_width,
488 "struct_variant_width",
489 );
490 self.struct_variant_width.2 = struct_variant_width;
491
492 let array_width = get_width_value(
493 self.was_set().array_width(),
494 self.array_width.2,
495 heuristics.array_width,
496 "array_width",
497 );
498 self.array_width.2 = array_width;
499
500 let chain_width = get_width_value(
501 self.was_set().chain_width(),
502 self.chain_width.2,
503 heuristics.chain_width,
504 "chain_width",
505 );
506 self.chain_width.2 = chain_width;
507
508 let single_line_if_else_max_width = get_width_value(
509 self.was_set().single_line_if_else_max_width(),
510 self.single_line_if_else_max_width.2,
511 heuristics.single_line_if_else_max_width,
512 "single_line_if_else_max_width",
513 );
514 self.single_line_if_else_max_width.2 = single_line_if_else_max_width;
515
516 let single_line_let_else_max_width = get_width_value(
517 self.was_set().single_line_let_else_max_width(),
518 self.single_line_let_else_max_width.2,
519 heuristics.single_line_let_else_max_width,
520 "single_line_let_else_max_width",
521 );
522 self.single_line_let_else_max_width.2 = single_line_let_else_max_width;
523 }
524
525 fn set_heuristics(&mut self) {
526 let max_width = self.max_width.2;
527 match self.use_small_heuristics.2 {
528 Heuristics::Default =>
529 self.set_width_heuristics(WidthHeuristics::scaled(max_width)),
530 Heuristics::Max => self.set_width_heuristics(WidthHeuristics::set(max_width)),
531 Heuristics::Off => self.set_width_heuristics(WidthHeuristics::null()),
532 };
533 }
534
535 fn set_ignore(&mut self, dir: &Path) {
536 self.ignore.2.add_prefix(dir);
537 }
538
539 fn set_merge_imports(&mut self) {
540 if self.was_set().merge_imports() {
541 eprintln!(
542 "Warning: the `merge_imports` option is deprecated. \
543 Use `imports_granularity=\"Crate\"` instead"
544 );
545 if !self.was_set().imports_granularity() {
546 self.imports_granularity.2 = if self.merge_imports() {
547 ImportGranularity::Crate
548 } else {
549 ImportGranularity::Preserve
550 };
551 }
552 }
553 }
554
555 fn set_fn_args_layout(&mut self) {
556 if self.was_set().fn_args_layout() {
557 eprintln!(
558 "Warning: the `fn_args_layout` option is deprecated. \
559 Use `fn_params_layout`. instead"
560 );
561 if !self.was_set().fn_params_layout() {
562 self.fn_params_layout.2 = self.fn_args_layout();
563 }
564 }
565 }
566
567 fn set_hide_parse_errors(&mut self) {
568 if self.was_set().hide_parse_errors() {
569 eprintln!(
570 "Warning: the `hide_parse_errors` option is deprecated. \
571 Use `show_parse_errors` instead"
572 );
573 if !self.was_set().show_parse_errors() {
574 self.show_parse_errors.2 = self.hide_parse_errors();
575 }
576 }
577 }
578
579 fn set_version(&mut self) {
580 if !self.was_set().version() {
581 return;
582 }
583
584 eprintln!(
585 "Warning: the `version` option is deprecated. \
586 Use `style_edition=\"{0}\"` instead.",
587 match self.version.2 {
588 Version::One => "2015",
589 Version::Two => "2024",
590 }
591 );
592
593 if self.was_set().style_edition() || self.was_set_cli().style_edition() {
594 eprintln!(
595 "Warning: the deprecated `version` option was \
596 used in conjunction with the `style_edition` \
597 option which takes precedence. \
598 The value of the `version` option will be ignored."
599 );
600 }
601 }
602
603 #[allow(unreachable_pub)]
604 pub fn is_default(&self, key: &str) -> bool {
606 let style_edition = StyleEdition::Edition2015;
607 $(
608 let default_value = <$ty as StyleEditionDefault>::style_edition_default(
609 style_edition
610 );
611 if let stringify!($i) = key {
612 return self.$i.1 && self.$i.2 == default_value;
613 }
614 )+
615 false
616 }
617 }
618
619 impl Default for Config {
621 fn default() -> Config {
622 Config::default_with_style_edition(StyleEdition::Edition2015)
623 }
624 }
625 )
626}
627
628pub(crate) fn is_stable_option_and_value<T>(
629 option_name: &str,
630 option_stable: bool,
631 option_value: &T,
632) -> bool
633where
634 T: PartialEq + std::fmt::Debug + ConfigType,
635{
636 let nightly = crate::is_nightly_channel!();
637 let variant_stable = option_value.stable_variant();
638 match (nightly, option_stable, variant_stable) {
639 (false, false, _) => {
641 eprintln!(
642 "Warning: can't set `{option_name} = {option_value:?}`, unstable features are only \
643 available in nightly channel."
644 );
645 false
646 }
647 (false, true, false) => {
649 eprintln!(
650 "Warning: can't set `{option_name} = {option_value:?}`, unstable variants are only \
651 available in nightly channel."
652 );
653 false
654 }
655 (true, _, _) | (false, true, true) => true,
658 }
659}