]> git.lizzy.rs Git - rust.git/blob - src/config/lists.rs
fix clippy warnings
[rust.git] / src / config / lists.rs
1 //! Configuration options related to rewriting a list.
2
3 use crate::config::config_type::ConfigType;
4 use crate::config::IndentStyle;
5
6 /// The definitive formatting tactic for lists.
7 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
8 pub enum DefinitiveListTactic {
9     Vertical,
10     Horizontal,
11     Mixed,
12     /// Special case tactic for `format!()`, `write!()` style macros.
13     SpecialMacro(usize),
14 }
15
16 impl DefinitiveListTactic {
17     pub fn ends_with_newline(&self, indent_style: IndentStyle) -> bool {
18         match indent_style {
19             IndentStyle::Block => *self != DefinitiveListTactic::Horizontal,
20             IndentStyle::Visual => false,
21         }
22     }
23 }
24
25 /// Formatting tactic for lists. This will be cast down to a
26 /// `DefinitiveListTactic` depending on the number and length of the items and
27 /// their comments.
28 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
29 pub enum ListTactic {
30     // One item per row.
31     Vertical,
32     // All items on one row.
33     Horizontal,
34     // Try Horizontal layout, if that fails then vertical.
35     HorizontalVertical,
36     // HorizontalVertical with a soft limit of n characters.
37     LimitedHorizontalVertical(usize),
38     // Pack as many items as possible per row over (possibly) many rows.
39     Mixed,
40 }
41
42 impl_enum_serialize_and_deserialize!(ListTactic, Vertical, Horizontal, HorizontalVertical, Mixed);
43
44 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
45 pub enum SeparatorTactic {
46     Always,
47     Never,
48     Vertical,
49 }
50
51 impl_enum_serialize_and_deserialize!(SeparatorTactic, Always, Never, Vertical);
52
53 impl SeparatorTactic {
54     pub fn from_bool(b: bool) -> SeparatorTactic {
55         if b {
56             SeparatorTactic::Always
57         } else {
58             SeparatorTactic::Never
59         }
60     }
61 }
62
63 /// Where to put separator.
64 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
65 pub enum SeparatorPlace {
66     Front,
67     Back,
68 }
69
70 impl_enum_serialize_and_deserialize!(SeparatorPlace, Front, Back);
71
72 impl SeparatorPlace {
73     pub fn is_front(self) -> bool {
74         self == SeparatorPlace::Front
75     }
76
77     pub fn is_back(self) -> bool {
78         self == SeparatorPlace::Back
79     }
80
81     pub fn from_tactic(
82         default: SeparatorPlace,
83         tactic: DefinitiveListTactic,
84         sep: &str,
85     ) -> SeparatorPlace {
86         match tactic {
87             DefinitiveListTactic::Vertical => default,
88             _ => {
89                 if sep == "," {
90                     SeparatorPlace::Back
91                 } else {
92                     default
93                 }
94             }
95         }
96     }
97 }