]> git.lizzy.rs Git - rust.git/blob - src/config/lists.rs
Merge pull request #2769 from topecongiro/issue-2765
[rust.git] / src / config / lists.rs
1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Configuration options related to rewriting a list.
12
13 use config::config_type::ConfigType;
14 use config::IndentStyle;
15
16 /// The definitive formatting tactic for lists.
17 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
18 pub enum DefinitiveListTactic {
19     Vertical,
20     Horizontal,
21     Mixed,
22     /// Tactic for nested import.
23     NestedImport,
24     /// Special case tactic for `format!()`, `write!()` style macros.
25     SpecialMacro(usize),
26 }
27
28 impl DefinitiveListTactic {
29     pub fn ends_with_newline(&self, indent_style: IndentStyle) -> bool {
30         match indent_style {
31             IndentStyle::Block => *self != DefinitiveListTactic::Horizontal,
32             IndentStyle::Visual => false,
33         }
34     }
35 }
36
37 /// Formatting tactic for lists. This will be cast down to a
38 /// `DefinitiveListTactic` depending on the number and length of the items and
39 /// their comments.
40 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
41 pub enum ListTactic {
42     // One item per row.
43     Vertical,
44     // All items on one row.
45     Horizontal,
46     // Try Horizontal layout, if that fails then vertical.
47     HorizontalVertical,
48     // HorizontalVertical with a soft limit of n characters.
49     LimitedHorizontalVertical(usize),
50     // Pack as many items as possible per row over (possibly) many rows.
51     Mixed,
52 }
53
54 impl_enum_serialize_and_deserialize!(ListTactic, Vertical, Horizontal, HorizontalVertical, Mixed);
55
56 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
57 pub enum SeparatorTactic {
58     Always,
59     Never,
60     Vertical,
61 }
62
63 impl_enum_serialize_and_deserialize!(SeparatorTactic, Always, Never, Vertical);
64
65 impl SeparatorTactic {
66     pub fn from_bool(b: bool) -> SeparatorTactic {
67         if b {
68             SeparatorTactic::Always
69         } else {
70             SeparatorTactic::Never
71         }
72     }
73 }
74
75 /// Where to put separator.
76 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
77 pub enum SeparatorPlace {
78     Front,
79     Back,
80 }
81
82 impl_enum_serialize_and_deserialize!(SeparatorPlace, Front, Back);
83
84 impl SeparatorPlace {
85     pub fn is_front(&self) -> bool {
86         *self == SeparatorPlace::Front
87     }
88
89     pub fn is_back(&self) -> bool {
90         *self == SeparatorPlace::Back
91     }
92
93     pub fn from_tactic(
94         default: SeparatorPlace,
95         tactic: DefinitiveListTactic,
96         sep: &str,
97     ) -> SeparatorPlace {
98         match tactic {
99             DefinitiveListTactic::Vertical => default,
100             _ => if sep == "," {
101                 SeparatorPlace::Back
102             } else {
103                 default
104             },
105         }
106     }
107 }