]> git.lizzy.rs Git - rust.git/blob - src/config.rs
Merge pull request #309 from marcusklaas/array-literals
[rust.git] / src / config.rs
1 // Copyright 2015 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 extern crate toml;
12
13 use {NewlineStyle, BraceStyle, ReturnIndent, StructLitStyle};
14 use lists::{SeparatorTactic, ListTactic};
15 use issues::ReportTactic;
16
17 #[derive(Copy, Clone, Eq, PartialEq, Debug)]
18 pub enum BlockIndentStyle {
19     // Same level as parent.
20     Inherit,
21     // One level deeper than parent.
22     Tabbed,
23     // Aligned with block open.
24     Visual,
25 }
26
27 impl_enum_decodable!(BlockIndentStyle, Inherit, Tabbed, Visual);
28
29 #[derive(Copy, Clone, Eq, PartialEq, Debug)]
30 pub enum Density {
31     // Fit as much on one line as possible.
32     Compressed,
33     // Use more lines.
34     Tall,
35 }
36
37 impl_enum_decodable!(Density, Compressed, Tall);
38
39 impl Density {
40     pub fn to_list_tactic(self) -> ListTactic {
41         match self {
42             Density::Compressed => ListTactic::Mixed,
43             Density::Tall => ListTactic::HorizontalVertical,
44         }
45     }
46 }
47
48 #[derive(Copy, Clone, Eq, PartialEq, Debug)]
49 pub enum MultilineStyle {
50     // Use horizontal layout if it fits in one line, fall back to vertical
51     PreferSingle,
52     // Use vertical layout
53     ForceMulti,
54 }
55
56
57 impl_enum_decodable!(MultilineStyle, PreferSingle, ForceMulti);
58
59 impl MultilineStyle {
60     pub fn to_list_tactic(self) -> ListTactic {
61         match self {
62             MultilineStyle::PreferSingle => ListTactic::HorizontalVertical,
63             MultilineStyle::ForceMulti => ListTactic::Vertical,
64         }
65     }
66 }
67
68 macro_rules! create_config {
69     ($($i:ident: $ty:ty),+ $(,)*) => (
70         #[derive(RustcDecodable, Clone)]
71         pub struct Config {
72             $(pub $i: $ty),+
73         }
74
75         // Just like the Config struct but with each property wrapped
76         // as Option<T>. This is used to parse a rustfmt.toml that doesn't
77         // specity all properties of `Config`.
78         // We first parse into `ParsedConfig`, then create a default `Config`
79         // and overwrite the properties with corresponding values from `ParsedConfig`
80         #[derive(RustcDecodable, Clone)]
81         pub struct ParsedConfig {
82             $(pub $i: Option<$ty>),+
83         }
84
85         impl Config {
86
87             fn fill_from_parsed_config(mut self, parsed: &ParsedConfig) -> Config {
88             $(
89                 if let Some(val) = parsed.$i {
90                     self.$i = val;
91                 }
92             )+
93                 self
94             }
95
96             pub fn from_toml(toml: &str) -> Config {
97                 let parsed = toml.parse().unwrap();
98                 let parsed_config:ParsedConfig = match toml::decode(parsed) {
99                     Some(decoded) => decoded,
100                     None => {
101                         println!("Decoding config file failed. Config:\n{}", toml);
102                         let parsed: toml::Value = toml.parse().unwrap();
103                         println!("\n\nParsed:\n{:?}", parsed);
104                         panic!();
105                     }
106                 };
107                 Config::default().fill_from_parsed_config(&parsed_config)
108             }
109
110             pub fn override_value(&mut self, key: &str, val: &str) {
111                 match key {
112                     $(
113                         stringify!($i) => {
114                             self.$i = val.parse::<$ty>().unwrap();
115                         }
116                     )+
117                     _ => panic!("Bad config key!")
118                 }
119             }
120         }
121     )
122 }
123
124 create_config! {
125     max_width: usize,
126     ideal_width: usize,
127     leeway: usize,
128     tab_spaces: usize,
129     newline_style: NewlineStyle,
130     fn_brace_style: BraceStyle,
131     fn_return_indent: ReturnIndent,
132     fn_args_paren_newline: bool,
133     fn_args_density: Density,
134     fn_args_layout: StructLitStyle,
135     fn_arg_indent: BlockIndentStyle,
136     where_density: Density, // Should we at least try to put the where clause on
137                             // the same line as the rest of the function decl?
138     where_indent: BlockIndentStyle, // Visual will be treated like Tabbed
139     where_layout: ListTactic,
140     where_pred_indent: BlockIndentStyle,
141     generics_indent: BlockIndentStyle,
142     struct_trailing_comma: SeparatorTactic,
143     struct_lit_trailing_comma: SeparatorTactic,
144     struct_lit_style: StructLitStyle,
145     struct_lit_multiline_style: MultilineStyle,
146     enum_trailing_comma: bool,
147     report_todo: ReportTactic,
148     report_fixme: ReportTactic,
149     reorder_imports: bool, // Alphabetically, case sensitive.
150     single_line_if_else: bool,
151     format_strings: bool,
152     chains_overflow_last: bool,
153     take_source_hints: bool, // Retain some formatting characteristics from
154                              // the source code.
155 }
156
157 impl Default for Config {
158     fn default() -> Config {
159         Config {
160             max_width: 100,
161             ideal_width: 80,
162             leeway: 5,
163             tab_spaces: 4,
164             newline_style: NewlineStyle::Unix,
165             fn_brace_style: BraceStyle::SameLineWhere,
166             fn_return_indent: ReturnIndent::WithArgs,
167             fn_args_paren_newline: true,
168             fn_args_density: Density::Tall,
169             fn_args_layout: StructLitStyle::Visual,
170             fn_arg_indent: BlockIndentStyle::Visual,
171             where_density: Density::Tall,
172             where_indent: BlockIndentStyle::Tabbed,
173             where_layout: ListTactic::Vertical,
174             where_pred_indent: BlockIndentStyle::Visual,
175             generics_indent: BlockIndentStyle::Visual,
176             struct_trailing_comma: SeparatorTactic::Vertical,
177             struct_lit_trailing_comma: SeparatorTactic::Vertical,
178             struct_lit_style: StructLitStyle::Block,
179             struct_lit_multiline_style: MultilineStyle::PreferSingle,
180             enum_trailing_comma: true,
181             report_todo: ReportTactic::Always,
182             report_fixme: ReportTactic::Never,
183             reorder_imports: false,
184             single_line_if_else: false,
185             format_strings: true,
186             chains_overflow_last: true,
187             take_source_hints: true,
188         }
189     }
190 }