1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
use std::cell::Cell;
use std::fs::File;
use std::io::{Error, ErrorKind, Read};
use std::path::{Path, PathBuf};
use std::{env, fs};

use thiserror::Error;

use crate::config::config_type::ConfigType;
#[allow(unreachable_pub)]
pub use crate::config::file_lines::{FileLines, FileName, Range};
#[allow(unreachable_pub)]
pub use crate::config::lists::*;
#[allow(unreachable_pub)]
pub use crate::config::macro_names::{MacroSelector, MacroSelectors};
#[allow(unreachable_pub)]
pub use crate::config::options::*;

#[macro_use]
pub(crate) mod config_type;
#[macro_use]
#[allow(unreachable_pub)]
pub(crate) mod options;

pub(crate) mod file_lines;
#[allow(unreachable_pub)]
pub(crate) mod lists;
pub(crate) mod macro_names;

// This macro defines configuration options used in rustfmt. Each option
// is defined as follows:
//
// `name: value type, default value, is stable, description;`
create_config! {
    // Fundamental stuff
    max_width: usize, 100, true, "Maximum width of each line";
    hard_tabs: bool, false, true, "Use tab characters for indentation, spaces for alignment";
    tab_spaces: usize, 4, true, "Number of spaces per tab";
    newline_style: NewlineStyle, NewlineStyle::Auto, true, "Unix or Windows line endings";
    indent_style: IndentStyle, IndentStyle::Block, false, "How do we indent expressions or items";

    // Width Heuristics
    use_small_heuristics: Heuristics, Heuristics::Default, true, "Whether to use different \
        formatting for items and expressions if they satisfy a heuristic notion of 'small'";
    width_heuristics: WidthHeuristics, WidthHeuristics::scaled(100), false,
        "'small' heuristic values";
    fn_call_width: usize, 60, true, "Maximum width of the args of a function call before \
        falling back to vertical formatting.";
    attr_fn_like_width: usize, 70, true, "Maximum width of the args of a function-like \
        attributes before falling back to vertical formatting.";
    struct_lit_width: usize, 18, true, "Maximum width in the body of a struct lit before \
        falling back to vertical formatting.";
    struct_variant_width: usize, 35, true, "Maximum width in the body of a struct variant before \
        falling back to vertical formatting.";
    array_width: usize, 60, true,  "Maximum width of an array literal before falling \
        back to vertical formatting.";
    chain_width: usize, 60, true, "Maximum length of a chain to fit on a single line.";
    single_line_if_else_max_width: usize, 50, true, "Maximum line length for single line if-else \
        expressions. A value of zero means always break if-else expressions.";
    single_line_let_else_max_width: usize, 50, true, "Maximum line length for single line \
        let-else statements. A value of zero means always format the divergent `else` block \
        over multiple lines.";

    // Comments. macros, and strings
    wrap_comments: bool, false, false, "Break comments to fit on the line";
    format_code_in_doc_comments: bool, false, false, "Format the code snippet in doc comments.";
    doc_comment_code_block_width: usize, 100, false, "Maximum width for code snippets in doc \
        comments. No effect unless format_code_in_doc_comments = true";
    comment_width: usize, 80, false,
        "Maximum length of comments. No effect unless wrap_comments = true";
    normalize_comments: bool, false, false, "Convert /* */ comments to // comments where possible";
    normalize_doc_attributes: bool, false, false, "Normalize doc attributes as doc comments";
    format_strings: bool, false, false, "Format string literals where necessary";
    format_macro_matchers: bool, false, false,
        "Format the metavariable matching patterns in macros";
    format_macro_bodies: bool, true, false, "Format the bodies of macros";
    skip_macro_invocations: MacroSelectors, MacroSelectors::default(), false,
        "Skip formatting the bodies of macros invoked with the following names.";
    hex_literal_case: HexLiteralCase, HexLiteralCase::Preserve, false,
        "Format hexadecimal integer literals";

    // Single line expressions and items
    empty_item_single_line: bool, true, false,
        "Put empty-body functions and impls on a single line";
    struct_lit_single_line: bool, true, false,
        "Put small struct literals on a single line";
    fn_single_line: bool, false, false, "Put single-expression functions on a single line";
    where_single_line: bool, false, false, "Force where-clauses to be on a single line";

    // Imports
    imports_indent: IndentStyle, IndentStyle::Block, false, "Indent of imports";
    imports_layout: ListTactic, ListTactic::Mixed, false, "Item layout inside a import block";
    imports_granularity: ImportGranularity, ImportGranularity::Preserve, false,
        "Merge or split imports to the provided granularity";
    group_imports: GroupImportsTactic, GroupImportsTactic::Preserve, false,
        "Controls the strategy for how imports are grouped together";
    merge_imports: bool, false, false, "(deprecated: use imports_granularity instead)";

    // Ordering
    reorder_imports: bool, true, true, "Reorder import and extern crate statements alphabetically";
    reorder_modules: bool, true, true, "Reorder module statements alphabetically in group";
    reorder_impl_items: bool, false, false, "Reorder impl items";

    // Spaces around punctuation
    type_punctuation_density: TypeDensity, TypeDensity::Wide, false,
        "Determines if '+' or '=' are wrapped in spaces in the punctuation of types";
    space_before_colon: bool, false, false, "Leave a space before the colon";
    space_after_colon: bool, true, false, "Leave a space after the colon";
    spaces_around_ranges: bool, false, false, "Put spaces around the  .. and ..= range operators";
    binop_separator: SeparatorPlace, SeparatorPlace::Front, false,
        "Where to put a binary operator when a binary expression goes multiline";

    // Misc.
    remove_nested_parens: bool, true, true, "Remove nested parens";
    combine_control_expr: bool, true, false, "Combine control expressions with function calls";
    short_array_element_width_threshold: usize, 10, true,
        "Width threshold for an array element to be considered short";
    overflow_delimited_expr: bool, false, false,
        "Allow trailing bracket/brace delimited expressions to overflow";
    struct_field_align_threshold: usize, 0, false,
        "Align struct fields if their diffs fits within threshold";
    enum_discrim_align_threshold: usize, 0, false,
        "Align enum variants discrims, if their diffs fit within threshold";
    match_arm_blocks: bool, true, false, "Wrap the body of arms in blocks when it does not fit on \
        the same line with the pattern of arms";
    match_arm_leading_pipes: MatchArmLeadingPipe, MatchArmLeadingPipe::Never, true,
        "Determines whether leading pipes are emitted on match arms";
    force_multiline_blocks: bool, false, false,
        "Force multiline closure bodies and match arms to be wrapped in a block";
    fn_args_layout: Density, Density::Tall, true,
        "(deprecated: use fn_params_layout instead)";
    fn_params_layout: Density, Density::Tall, true,
        "Control the layout of parameters in function signatures.";
    brace_style: BraceStyle, BraceStyle::SameLineWhere, false, "Brace style for items";
    control_brace_style: ControlBraceStyle, ControlBraceStyle::AlwaysSameLine, false,
        "Brace style for control flow constructs";
    trailing_semicolon: bool, true, false,
        "Add trailing semicolon after break, continue and return";
    trailing_comma: SeparatorTactic, SeparatorTactic::Vertical, false,
        "How to handle trailing commas for lists";
    match_block_trailing_comma: bool, false, true,
        "Put a trailing comma after a block based match arm (non-block arms are not affected)";
    blank_lines_upper_bound: usize, 1, false,
        "Maximum number of blank lines which can be put between items";
    blank_lines_lower_bound: usize, 0, false,
        "Minimum number of blank lines which must be put between items";
    edition: Edition, Edition::Edition2015, true, "The edition of the parser (RFC 2052)";
    version: Version, Version::One, false, "Version of formatting rules";
    inline_attribute_width: usize, 0, false,
        "Write an item and its attribute on the same line \
        if their combined width is below a threshold";
    format_generated_files: bool, true, false, "Format generated files";

    // Options that can change the source code beyond whitespace/blocks (somewhat linty things)
    merge_derives: bool, true, true, "Merge multiple `#[derive(...)]` into a single one";
    use_try_shorthand: bool, false, true, "Replace uses of the try! macro by the ? shorthand";
    use_field_init_shorthand: bool, false, true, "Use field initialization shorthand if possible";
    force_explicit_abi: bool, true, true, "Always print the abi for extern items";
    condense_wildcard_suffixes: bool, false, false, "Replace strings of _ wildcards by a single .. \
                                                     in tuple patterns";

    // Control options (changes the operation of rustfmt, rather than the formatting)
    color: Color, Color::Auto, false,
        "What Color option to use when none is supplied: Always, Never, Auto";
    required_version: String, env!("CARGO_PKG_VERSION").to_owned(), false,
        "Require a specific version of rustfmt";
    unstable_features: bool, false, false,
            "Enables unstable features. Only available on nightly channel";
    disable_all_formatting: bool, false, true, "Don't reformat anything";
    skip_children: bool, false, false, "Don't reformat out of line modules";
    hide_parse_errors: bool, false, false, "Hide errors from the parser";
    error_on_line_overflow: bool, false, false, "Error if unable to get all lines within max_width";
    error_on_unformatted: bool, false, false,
        "Error if unable to get comments or string literals within max_width, \
         or they are left with trailing whitespaces";
    ignore: IgnoreList, IgnoreList::default(), false,
        "Skip formatting the specified files and directories";

    // Not user-facing
    verbose: Verbosity, Verbosity::Normal, false, "How much to information to emit to the user";
    file_lines: FileLines, FileLines::all(), false,
        "Lines to format; this is not supported in rustfmt.toml, and can only be specified \
         via the --file-lines option";
    emit_mode: EmitMode, EmitMode::Files, false,
        "What emit Mode to use when none is supplied";
    make_backup: bool, false, false, "Backup changed files";
    print_misformatted_file_names: bool, false, true,
        "Prints the names of mismatched files that were formatted. Prints the names of \
         files that would be formatted when used with `--check` mode. ";
}

#[derive(Error, Debug)]
#[error("Could not output config: {0}")]
pub struct ToTomlError(toml::ser::Error);

impl PartialConfig {
    pub fn to_toml(&self) -> Result<String, ToTomlError> {
        // Non-user-facing options can't be specified in TOML
        let mut cloned = self.clone();
        cloned.file_lines = None;
        cloned.verbose = None;
        cloned.width_heuristics = None;
        cloned.print_misformatted_file_names = None;
        cloned.merge_imports = None;
        cloned.fn_args_layout = None;

        ::toml::to_string(&cloned).map_err(ToTomlError)
    }
}

impl Config {
    pub(crate) fn version_meets_requirement(&self) -> bool {
        if self.was_set().required_version() {
            let version = env!("CARGO_PKG_VERSION");
            let required_version = self.required_version();
            if version != required_version {
                println!(
                    "Error: rustfmt version ({version}) doesn't match the required version \
({required_version})"
                );
                return false;
            }
        }

        true
    }

    /// Constructs a `Config` from the toml file specified at `file_path`.
    ///
    /// This method only looks at the provided path, for a method that
    /// searches parents for a `rustfmt.toml` see `from_resolved_toml_path`.
    ///
    /// Returns a `Config` if the config could be read and parsed from
    /// the file, otherwise errors.
    pub(super) fn from_toml_path(file_path: &Path) -> Result<Config, Error> {
        let mut file = File::open(&file_path)?;
        let mut toml = String::new();
        file.read_to_string(&mut toml)?;
        Config::from_toml(&toml, file_path.parent().unwrap())
            .map_err(|err| Error::new(ErrorKind::InvalidData, err))
    }

    /// Resolves the config for input in `dir`.
    ///
    /// Searches for `rustfmt.toml` beginning with `dir`, and
    /// recursively checking parents of `dir` if no config file is found.
    /// If no config file exists in `dir` or in any parent, a
    /// default `Config` will be returned (and the returned path will be empty).
    ///
    /// Returns the `Config` to use, and the path of the project file if there was
    /// one.
    pub(super) fn from_resolved_toml_path(dir: &Path) -> Result<(Config, Option<PathBuf>), Error> {
        /// Try to find a project file in the given directory and its parents.
        /// Returns the path of a the nearest project file if one exists,
        /// or `None` if no project file was found.
        fn resolve_project_file(dir: &Path) -> Result<Option<PathBuf>, Error> {
            let mut current = if dir.is_relative() {
                env::current_dir()?.join(dir)
            } else {
                dir.to_path_buf()
            };

            current = fs::canonicalize(current)?;

            loop {
                match get_toml_path(&current) {
                    Ok(Some(path)) => return Ok(Some(path)),
                    Err(e) => return Err(e),
                    _ => (),
                }

                // If the current directory has no parent, we're done searching.
                if !current.pop() {
                    break;
                }
            }

            // If nothing was found, check in the home directory.
            if let Some(home_dir) = dirs::home_dir() {
                if let Some(path) = get_toml_path(&home_dir)? {
                    return Ok(Some(path));
                }
            }

            // If none was found ther either, check in the user's configuration directory.
            if let Some(mut config_dir) = dirs::config_dir() {
                config_dir.push("rustfmt");
                if let Some(path) = get_toml_path(&config_dir)? {
                    return Ok(Some(path));
                }
            }

            Ok(None)
        }

        match resolve_project_file(dir)? {
            None => Ok((Config::default(), None)),
            Some(path) => Config::from_toml_path(&path).map(|config| (config, Some(path))),
        }
    }

    pub(crate) fn from_toml(toml: &str, dir: &Path) -> Result<Config, String> {
        let parsed: ::toml::Value = toml
            .parse()
            .map_err(|e| format!("Could not parse TOML: {}", e))?;
        let mut err = String::new();
        let table = parsed
            .as_table()
            .ok_or_else(|| String::from("Parsed config was not table"))?;
        for key in table.keys() {
            if !Config::is_valid_name(key) {
                let msg = &format!("Warning: Unknown configuration option `{key}`\n");
                err.push_str(msg)
            }
        }
        match parsed.try_into() {
            Ok(parsed_config) => {
                if !err.is_empty() {
                    eprint!("{err}");
                }
                Ok(Config::default().fill_from_parsed_config(parsed_config, dir))
            }
            Err(e) => {
                err.push_str("Error: Decoding config file failed:\n");
                err.push_str(format!("{e}\n").as_str());
                err.push_str("Please check your config file.");
                Err(err)
            }
        }
    }
}

/// Loads a config by checking the client-supplied options and if appropriate, the
/// file system (including searching the file system for overrides).
pub fn load_config<O: CliOptions>(
    file_path: Option<&Path>,
    options: Option<O>,
) -> Result<(Config, Option<PathBuf>), Error> {
    let over_ride = match options {
        Some(ref opts) => config_path(opts)?,
        None => None,
    };

    let result = if let Some(over_ride) = over_ride {
        Config::from_toml_path(over_ride.as_ref()).map(|p| (p, Some(over_ride.to_owned())))
    } else if let Some(file_path) = file_path {
        Config::from_resolved_toml_path(file_path)
    } else {
        Ok((Config::default(), None))
    };

    result.map(|(mut c, p)| {
        if let Some(options) = options {
            options.apply_to(&mut c);
        }
        (c, p)
    })
}

// Check for the presence of known config file names (`rustfmt.toml, `.rustfmt.toml`) in `dir`
//
// Return the path if a config file exists, empty if no file exists, and Error for IO errors
fn get_toml_path(dir: &Path) -> Result<Option<PathBuf>, Error> {
    const CONFIG_FILE_NAMES: [&str; 2] = [".rustfmt.toml", "rustfmt.toml"];
    for config_file_name in &CONFIG_FILE_NAMES {
        let config_file = dir.join(config_file_name);
        match fs::metadata(&config_file) {
            // Only return if it's a file to handle the unlikely situation of a directory named
            // `rustfmt.toml`.
            Ok(ref md) if md.is_file() => return Ok(Some(config_file)),
            // Return the error if it's something other than `NotFound`; otherwise we didn't
            // find the project file yet, and continue searching.
            Err(e) => {
                if e.kind() != ErrorKind::NotFound {
                    let ctx = format!("Failed to get metadata for config file {:?}", &config_file);
                    let err = anyhow::Error::new(e).context(ctx);
                    return Err(Error::new(ErrorKind::Other, err));
                }
            }
            _ => {}
        }
    }
    Ok(None)
}

fn config_path(options: &dyn CliOptions) -> Result<Option<PathBuf>, Error> {
    let config_path_not_found = |path: &str| -> Result<Option<PathBuf>, Error> {
        Err(Error::new(
            ErrorKind::NotFound,
            format!(
                "Error: unable to find a config file for the given path: `{}`",
                path
            ),
        ))
    };

    // Read the config_path and convert to parent dir if a file is provided.
    // If a config file cannot be found from the given path, return error.
    match options.config_path() {
        Some(path) if !path.exists() => config_path_not_found(path.to_str().unwrap()),
        Some(path) if path.is_dir() => {
            let config_file_path = get_toml_path(path)?;
            if config_file_path.is_some() {
                Ok(config_file_path)
            } else {
                config_path_not_found(path.to_str().unwrap())
            }
        }
        path => Ok(path.map(ToOwned::to_owned)),
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use std::str;

    use crate::config::macro_names::MacroName;
    use rustfmt_config_proc_macro::{nightly_only_test, stable_only_test};

    #[allow(dead_code)]
    mod mock {
        use super::super::*;
        use rustfmt_config_proc_macro::config_type;

        #[config_type]
        pub(crate) enum PartiallyUnstableOption {
            V1,
            V2,
            #[unstable_variant]
            V3,
        }

        create_config! {
            // Options that are used by the generated functions
            max_width: usize, 100, true, "Maximum width of each line";
            required_version: String, env!("CARGO_PKG_VERSION").to_owned(), false,
                "Require a specific version of rustfmt.";
            ignore: IgnoreList, IgnoreList::default(), false,
                "Skip formatting the specified files and directories.";
            verbose: Verbosity, Verbosity::Normal, false,
                "How much to information to emit to the user";
            file_lines: FileLines, FileLines::all(), false,
                "Lines to format; this is not supported in rustfmt.toml, and can only be specified \
                    via the --file-lines option";

            // merge_imports deprecation
            imports_granularity: ImportGranularity, ImportGranularity::Preserve, false,
                "Merge imports";
            merge_imports: bool, false, false, "(deprecated: use imports_granularity instead)";

            // fn_args_layout renamed to fn_params_layout
            fn_args_layout: Density, Density::Tall, true,
                "(deprecated: use fn_params_layout instead)";
            fn_params_layout: Density, Density::Tall, true,
                "Control the layout of parameters in a function signatures.";

            // Width Heuristics
            use_small_heuristics: Heuristics, Heuristics::Default, true,
                "Whether to use different formatting for items and \
                 expressions if they satisfy a heuristic notion of 'small'.";
            width_heuristics: WidthHeuristics, WidthHeuristics::scaled(100), false,
                "'small' heuristic values";

            fn_call_width: usize, 60, true, "Maximum width of the args of a function call before \
                falling back to vertical formatting.";
            attr_fn_like_width: usize, 70, true, "Maximum width of the args of a function-like \
                attributes before falling back to vertical formatting.";
            struct_lit_width: usize, 18, true, "Maximum width in the body of a struct lit before \
                falling back to vertical formatting.";
            struct_variant_width: usize, 35, true, "Maximum width in the body of a struct \
                variant before falling back to vertical formatting.";
            array_width: usize, 60, true,  "Maximum width of an array literal before falling \
                back to vertical formatting.";
            chain_width: usize, 60, true, "Maximum length of a chain to fit on a single line.";
            single_line_if_else_max_width: usize, 50, true, "Maximum line length for single \
                line if-else expressions. A value of zero means always break if-else expressions.";
            single_line_let_else_max_width: usize, 50, false, "Maximum line length for single \
                line let-else statements. A value of zero means always format the divergent \
                `else` block over multiple lines.";

            // Options that are used by the tests
            stable_option: bool, false, true, "A stable option";
            unstable_option: bool, false, false, "An unstable option";
            partially_unstable_option: PartiallyUnstableOption, PartiallyUnstableOption::V1, true,
                "A partially unstable option";
        }

        #[cfg(test)]
        mod partially_unstable_option {
            use super::{Config, PartialConfig, PartiallyUnstableOption};
            use rustfmt_config_proc_macro::{nightly_only_test, stable_only_test};
            use std::path::Path;

            /// From the config file, we can fill with a stable variant
            #[test]
            fn test_from_toml_stable_value() {
                let toml = r#"
                    partially_unstable_option = "V2"
                "#;
                let partial_config: PartialConfig = toml::from_str(toml).unwrap();
                let config = Config::default();
                let config = config.fill_from_parsed_config(partial_config, Path::new(""));
                assert_eq!(
                    config.partially_unstable_option(),
                    PartiallyUnstableOption::V2
                );
            }

            /// From the config file, we cannot fill with an unstable variant (stable only)
            #[stable_only_test]
            #[test]
            fn test_from_toml_unstable_value_on_stable() {
                let toml = r#"
                    partially_unstable_option = "V3"
                "#;
                let partial_config: PartialConfig = toml::from_str(toml).unwrap();
                let config = Config::default();
                let config = config.fill_from_parsed_config(partial_config, Path::new(""));
                assert_eq!(
                    config.partially_unstable_option(),
                    // default value from config, i.e. fill failed
                    PartiallyUnstableOption::V1
                );
            }

            /// From the config file, we can fill with an unstable variant (nightly only)
            #[nightly_only_test]
            #[test]
            fn test_from_toml_unstable_value_on_nightly() {
                let toml = r#"
                    partially_unstable_option = "V3"
                "#;
                let partial_config: PartialConfig = toml::from_str(toml).unwrap();
                let config = Config::default();
                let config = config.fill_from_parsed_config(partial_config, Path::new(""));
                assert_eq!(
                    config.partially_unstable_option(),
                    PartiallyUnstableOption::V3
                );
            }
        }
    }

    #[test]
    fn test_config_set() {
        let mut config = Config::default();
        config.set().verbose(Verbosity::Quiet);
        assert_eq!(config.verbose(), Verbosity::Quiet);
        config.set().verbose(Verbosity::Normal);
        assert_eq!(config.verbose(), Verbosity::Normal);
    }

    #[test]
    fn test_config_used_to_toml() {
        let config = Config::default();

        let merge_derives = config.merge_derives();
        let skip_children = config.skip_children();

        let used_options = config.used_options();
        let toml = used_options.to_toml().unwrap();
        assert_eq!(
            toml,
            format!("merge_derives = {merge_derives}\nskip_children = {skip_children}\n",)
        );
    }

    #[test]
    fn test_was_set() {
        let config = Config::from_toml("hard_tabs = true", Path::new("")).unwrap();

        assert_eq!(config.was_set().hard_tabs(), true);
        assert_eq!(config.was_set().verbose(), false);
    }

    const PRINT_DOCS_STABLE_OPTION: &str = "stable_option <boolean> Default: false";
    const PRINT_DOCS_UNSTABLE_OPTION: &str = "unstable_option <boolean> Default: false (unstable)";
    const PRINT_DOCS_PARTIALLY_UNSTABLE_OPTION: &str =
        "partially_unstable_option [V1|V2|V3 (unstable)] Default: V1";

    #[test]
    fn test_print_docs_exclude_unstable() {
        use self::mock::Config;

        let mut output = Vec::new();
        Config::print_docs(&mut output, false);

        let s = str::from_utf8(&output).unwrap();
        assert_eq!(s.contains(PRINT_DOCS_STABLE_OPTION), true);
        assert_eq!(s.contains(PRINT_DOCS_UNSTABLE_OPTION), false);
        assert_eq!(s.contains(PRINT_DOCS_PARTIALLY_UNSTABLE_OPTION), true);
    }

    #[test]
    fn test_print_docs_include_unstable() {
        use self::mock::Config;

        let mut output = Vec::new();
        Config::print_docs(&mut output, true);

        let s = str::from_utf8(&output).unwrap();
        assert_eq!(s.contains(PRINT_DOCS_STABLE_OPTION), true);
        assert_eq!(s.contains(PRINT_DOCS_UNSTABLE_OPTION), true);
        assert_eq!(s.contains(PRINT_DOCS_PARTIALLY_UNSTABLE_OPTION), true);
    }

    #[test]
    fn test_dump_default_config() {
        let default_config = format!(
            r#"max_width = 100
hard_tabs = false
tab_spaces = 4
newline_style = "Auto"
indent_style = "Block"
use_small_heuristics = "Default"
fn_call_width = 60
attr_fn_like_width = 70
struct_lit_width = 18
struct_variant_width = 35
array_width = 60
chain_width = 60
single_line_if_else_max_width = 50
single_line_let_else_max_width = 50
wrap_comments = false
format_code_in_doc_comments = false
doc_comment_code_block_width = 100
comment_width = 80
normalize_comments = false
normalize_doc_attributes = false
format_strings = false
format_macro_matchers = false
format_macro_bodies = true
skip_macro_invocations = []
hex_literal_case = "Preserve"
empty_item_single_line = true
struct_lit_single_line = true
fn_single_line = false
where_single_line = false
imports_indent = "Block"
imports_layout = "Mixed"
imports_granularity = "Preserve"
group_imports = "Preserve"
reorder_imports = true
reorder_modules = true
reorder_impl_items = false
type_punctuation_density = "Wide"
space_before_colon = false
space_after_colon = true
spaces_around_ranges = false
binop_separator = "Front"
remove_nested_parens = true
combine_control_expr = true
short_array_element_width_threshold = 10
overflow_delimited_expr = false
struct_field_align_threshold = 0
enum_discrim_align_threshold = 0
match_arm_blocks = true
match_arm_leading_pipes = "Never"
force_multiline_blocks = false
fn_params_layout = "Tall"
brace_style = "SameLineWhere"
control_brace_style = "AlwaysSameLine"
trailing_semicolon = true
trailing_comma = "Vertical"
match_block_trailing_comma = false
blank_lines_upper_bound = 1
blank_lines_lower_bound = 0
edition = "2015"
version = "One"
inline_attribute_width = 0
format_generated_files = true
merge_derives = true
use_try_shorthand = false
use_field_init_shorthand = false
force_explicit_abi = true
condense_wildcard_suffixes = false
color = "Auto"
required_version = "{}"
unstable_features = false
disable_all_formatting = false
skip_children = false
hide_parse_errors = false
error_on_line_overflow = false
error_on_unformatted = false
ignore = []
emit_mode = "Files"
make_backup = false
"#,
            env!("CARGO_PKG_VERSION")
        );
        let toml = Config::default().all_options().to_toml().unwrap();
        assert_eq!(&toml, &default_config);
    }

    #[stable_only_test]
    #[test]
    fn test_as_not_nightly_channel() {
        let mut config = Config::default();
        assert_eq!(config.was_set().unstable_features(), false);
        config.set().unstable_features(true);
        assert_eq!(config.was_set().unstable_features(), false);
    }

    #[nightly_only_test]
    #[test]
    fn test_as_nightly_channel() {
        let mut config = Config::default();
        config.set().unstable_features(true);
        // When we don't set the config from toml or command line options it
        // doesn't get marked as set by the user.
        assert_eq!(config.was_set().unstable_features(), false);
        config.set().unstable_features(true);
        assert_eq!(config.unstable_features(), true);
    }

    #[nightly_only_test]
    #[test]
    fn test_unstable_from_toml() {
        let config = Config::from_toml("unstable_features = true", Path::new("")).unwrap();
        assert_eq!(config.was_set().unstable_features(), true);
        assert_eq!(config.unstable_features(), true);
    }

    #[cfg(test)]
    mod deprecated_option_merge_imports {
        use super::*;

        #[nightly_only_test]
        #[test]
        fn test_old_option_set() {
            let toml = r#"
                unstable_features = true
                merge_imports = true
            "#;
            let config = Config::from_toml(toml, Path::new("")).unwrap();
            assert_eq!(config.imports_granularity(), ImportGranularity::Crate);
        }

        #[nightly_only_test]
        #[test]
        fn test_both_set() {
            let toml = r#"
                unstable_features = true
                merge_imports = true
                imports_granularity = "Preserve"
            "#;
            let config = Config::from_toml(toml, Path::new("")).unwrap();
            assert_eq!(config.imports_granularity(), ImportGranularity::Preserve);
        }

        #[nightly_only_test]
        #[test]
        fn test_new_overridden() {
            let toml = r#"
                unstable_features = true
                merge_imports = true
            "#;
            let mut config = Config::from_toml(toml, Path::new("")).unwrap();
            config.override_value("imports_granularity", "Preserve");
            assert_eq!(config.imports_granularity(), ImportGranularity::Preserve);
        }

        #[nightly_only_test]
        #[test]
        fn test_old_overridden() {
            let toml = r#"
                unstable_features = true
                imports_granularity = "Module"
            "#;
            let mut config = Config::from_toml(toml, Path::new("")).unwrap();
            config.override_value("merge_imports", "true");
            // no effect: the new option always takes precedence
            assert_eq!(config.imports_granularity(), ImportGranularity::Module);
        }
    }

    #[cfg(test)]
    mod use_small_heuristics {
        use super::*;

        #[test]
        fn test_default_sets_correct_widths() {
            let toml = r#"
                use_small_heuristics = "Default"
                max_width = 200
            "#;
            let config = Config::from_toml(toml, Path::new("")).unwrap();
            assert_eq!(config.array_width(), 120);
            assert_eq!(config.attr_fn_like_width(), 140);
            assert_eq!(config.chain_width(), 120);
            assert_eq!(config.fn_call_width(), 120);
            assert_eq!(config.single_line_if_else_max_width(), 100);
            assert_eq!(config.struct_lit_width(), 36);
            assert_eq!(config.struct_variant_width(), 70);
        }

        #[test]
        fn test_max_sets_correct_widths() {
            let toml = r#"
                use_small_heuristics = "Max"
                max_width = 120
            "#;
            let config = Config::from_toml(toml, Path::new("")).unwrap();
            assert_eq!(config.array_width(), 120);
            assert_eq!(config.attr_fn_like_width(), 120);
            assert_eq!(config.chain_width(), 120);
            assert_eq!(config.fn_call_width(), 120);
            assert_eq!(config.single_line_if_else_max_width(), 120);
            assert_eq!(config.struct_lit_width(), 120);
            assert_eq!(config.struct_variant_width(), 120);
        }

        #[test]
        fn test_off_sets_correct_widths() {
            let toml = r#"
                use_small_heuristics = "Off"
                max_width = 100
            "#;
            let config = Config::from_toml(toml, Path::new("")).unwrap();
            assert_eq!(config.array_width(), usize::max_value());
            assert_eq!(config.attr_fn_like_width(), usize::max_value());
            assert_eq!(config.chain_width(), usize::max_value());
            assert_eq!(config.fn_call_width(), usize::max_value());
            assert_eq!(config.single_line_if_else_max_width(), 0);
            assert_eq!(config.struct_lit_width(), 0);
            assert_eq!(config.struct_variant_width(), 0);
        }

        #[test]
        fn test_override_works_with_default() {
            let toml = r#"
                use_small_heuristics = "Default"
                array_width = 20
                attr_fn_like_width = 40
                chain_width = 20
                fn_call_width = 90
                single_line_if_else_max_width = 40
                struct_lit_width = 30
                struct_variant_width = 34
            "#;
            let config = Config::from_toml(toml, Path::new("")).unwrap();
            assert_eq!(config.array_width(), 20);
            assert_eq!(config.attr_fn_like_width(), 40);
            assert_eq!(config.chain_width(), 20);
            assert_eq!(config.fn_call_width(), 90);
            assert_eq!(config.single_line_if_else_max_width(), 40);
            assert_eq!(config.struct_lit_width(), 30);
            assert_eq!(config.struct_variant_width(), 34);
        }

        #[test]
        fn test_override_with_max() {
            let toml = r#"
                use_small_heuristics = "Max"
                array_width = 20
                attr_fn_like_width = 40
                chain_width = 20
                fn_call_width = 90
                single_line_if_else_max_width = 40
                struct_lit_width = 30
                struct_variant_width = 34
            "#;
            let config = Config::from_toml(toml, Path::new("")).unwrap();
            assert_eq!(config.array_width(), 20);
            assert_eq!(config.attr_fn_like_width(), 40);
            assert_eq!(config.chain_width(), 20);
            assert_eq!(config.fn_call_width(), 90);
            assert_eq!(config.single_line_if_else_max_width(), 40);
            assert_eq!(config.struct_lit_width(), 30);
            assert_eq!(config.struct_variant_width(), 34);
        }

        #[test]
        fn test_override_with_off() {
            let toml = r#"
                use_small_heuristics = "Off"
                array_width = 20
                attr_fn_like_width = 40
                chain_width = 20
                fn_call_width = 90
                single_line_if_else_max_width = 40
                struct_lit_width = 30
                struct_variant_width = 34
            "#;
            let config = Config::from_toml(toml, Path::new("")).unwrap();
            assert_eq!(config.array_width(), 20);
            assert_eq!(config.attr_fn_like_width(), 40);
            assert_eq!(config.chain_width(), 20);
            assert_eq!(config.fn_call_width(), 90);
            assert_eq!(config.single_line_if_else_max_width(), 40);
            assert_eq!(config.struct_lit_width(), 30);
            assert_eq!(config.struct_variant_width(), 34);
        }

        #[test]
        fn test_fn_call_width_config_exceeds_max_width() {
            let toml = r#"
                max_width = 90
                fn_call_width = 95
            "#;
            let config = Config::from_toml(toml, Path::new("")).unwrap();
            assert_eq!(config.fn_call_width(), 90);
        }

        #[test]
        fn test_attr_fn_like_width_config_exceeds_max_width() {
            let toml = r#"
                max_width = 80
                attr_fn_like_width = 90
            "#;
            let config = Config::from_toml(toml, Path::new("")).unwrap();
            assert_eq!(config.attr_fn_like_width(), 80);
        }

        #[test]
        fn test_struct_lit_config_exceeds_max_width() {
            let toml = r#"
                max_width = 78
                struct_lit_width = 90
            "#;
            let config = Config::from_toml(toml, Path::new("")).unwrap();
            assert_eq!(config.struct_lit_width(), 78);
        }

        #[test]
        fn test_struct_variant_width_config_exceeds_max_width() {
            let toml = r#"
                max_width = 80
                struct_variant_width = 90
            "#;
            let config = Config::from_toml(toml, Path::new("")).unwrap();
            assert_eq!(config.struct_variant_width(), 80);
        }

        #[test]
        fn test_array_width_config_exceeds_max_width() {
            let toml = r#"
                max_width = 60
                array_width = 80
            "#;
            let config = Config::from_toml(toml, Path::new("")).unwrap();
            assert_eq!(config.array_width(), 60);
        }

        #[test]
        fn test_chain_width_config_exceeds_max_width() {
            let toml = r#"
                max_width = 80
                chain_width = 90
            "#;
            let config = Config::from_toml(toml, Path::new("")).unwrap();
            assert_eq!(config.chain_width(), 80);
        }

        #[test]
        fn test_single_line_if_else_max_width_config_exceeds_max_width() {
            let toml = r#"
                max_width = 70
                single_line_if_else_max_width = 90
            "#;
            let config = Config::from_toml(toml, Path::new("")).unwrap();
            assert_eq!(config.single_line_if_else_max_width(), 70);
        }

        #[test]
        fn test_override_fn_call_width_exceeds_max_width() {
            let mut config = Config::default();
            config.override_value("fn_call_width", "101");
            assert_eq!(config.fn_call_width(), 100);
        }

        #[test]
        fn test_override_attr_fn_like_width_exceeds_max_width() {
            let mut config = Config::default();
            config.override_value("attr_fn_like_width", "101");
            assert_eq!(config.attr_fn_like_width(), 100);
        }

        #[test]
        fn test_override_struct_lit_exceeds_max_width() {
            let mut config = Config::default();
            config.override_value("struct_lit_width", "101");
            assert_eq!(config.struct_lit_width(), 100);
        }

        #[test]
        fn test_override_struct_variant_width_exceeds_max_width() {
            let mut config = Config::default();
            config.override_value("struct_variant_width", "101");
            assert_eq!(config.struct_variant_width(), 100);
        }

        #[test]
        fn test_override_array_width_exceeds_max_width() {
            let mut config = Config::default();
            config.override_value("array_width", "101");
            assert_eq!(config.array_width(), 100);
        }

        #[test]
        fn test_override_chain_width_exceeds_max_width() {
            let mut config = Config::default();
            config.override_value("chain_width", "101");
            assert_eq!(config.chain_width(), 100);
        }

        #[test]
        fn test_override_single_line_if_else_max_width_exceeds_max_width() {
            let mut config = Config::default();
            config.override_value("single_line_if_else_max_width", "101");
            assert_eq!(config.single_line_if_else_max_width(), 100);
        }
    }

    #[cfg(test)]
    mod partially_unstable_option {
        use super::mock::{Config, PartiallyUnstableOption};

        /// From the command line, we can override with a stable variant.
        #[test]
        fn test_override_stable_value() {
            let mut config = Config::default();
            config.override_value("partially_unstable_option", "V2");
            assert_eq!(
                config.partially_unstable_option(),
                PartiallyUnstableOption::V2
            );
        }

        /// From the command line, we can override with an unstable variant.
        #[test]
        fn test_override_unstable_value() {
            let mut config = Config::default();
            config.override_value("partially_unstable_option", "V3");
            assert_eq!(
                config.partially_unstable_option(),
                PartiallyUnstableOption::V3
            );
        }
    }

    #[test]
    fn test_override_skip_macro_invocations() {
        let mut config = Config::default();
        config.override_value("skip_macro_invocations", r#"["*", "println"]"#);
        assert_eq!(
            config.skip_macro_invocations(),
            MacroSelectors(vec![
                MacroSelector::All,
                MacroSelector::Name(MacroName::new("println".to_owned()))
            ])
        );
    }
}