]> git.lizzy.rs Git - rust.git/blob - src/config/lists.rs
Merge pull request #3175 from kestred/kstenerson/delimited-overflow
[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     /// Special case tactic for `format!()`, `write!()` style macros.
23     SpecialMacro(usize),
24 }
25
26 impl DefinitiveListTactic {
27     pub fn ends_with_newline(&self, indent_style: IndentStyle) -> bool {
28         match indent_style {
29             IndentStyle::Block => *self != DefinitiveListTactic::Horizontal,
30             IndentStyle::Visual => false,
31         }
32     }
33 }
34
35 /// Formatting tactic for lists. This will be cast down to a
36 /// `DefinitiveListTactic` depending on the number and length of the items and
37 /// their comments.
38 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
39 pub enum ListTactic {
40     // One item per row.
41     Vertical,
42     // All items on one row.
43     Horizontal,
44     // Try Horizontal layout, if that fails then vertical.
45     HorizontalVertical,
46     // HorizontalVertical with a soft limit of n characters.
47     LimitedHorizontalVertical(usize),
48     // Pack as many items as possible per row over (possibly) many rows.
49     Mixed,
50 }
51
52 impl_enum_serialize_and_deserialize!(ListTactic, Vertical, Horizontal, HorizontalVertical, Mixed);
53
54 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
55 pub enum SeparatorTactic {
56     Always,
57     Never,
58     Vertical,
59 }
60
61 impl_enum_serialize_and_deserialize!(SeparatorTactic, Always, Never, Vertical);
62
63 impl SeparatorTactic {
64     pub fn from_bool(b: bool) -> SeparatorTactic {
65         if b {
66             SeparatorTactic::Always
67         } else {
68             SeparatorTactic::Never
69         }
70     }
71 }
72
73 /// Where to put separator.
74 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
75 pub enum SeparatorPlace {
76     Front,
77     Back,
78 }
79
80 impl_enum_serialize_and_deserialize!(SeparatorPlace, Front, Back);
81
82 impl SeparatorPlace {
83     pub fn is_front(self) -> bool {
84         self == SeparatorPlace::Front
85     }
86
87     pub fn is_back(self) -> bool {
88         self == SeparatorPlace::Back
89     }
90
91     pub fn from_tactic(
92         default: SeparatorPlace,
93         tactic: DefinitiveListTactic,
94         sep: &str,
95     ) -> SeparatorPlace {
96         match tactic {
97             DefinitiveListTactic::Vertical => default,
98             _ => {
99                 if sep == "," {
100                     SeparatorPlace::Back
101                 } else {
102                     default
103                 }
104             }
105         }
106     }
107 }