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.
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.
13 use {NewlineStyle, BraceStyle, ReturnIndent, StructLitStyle};
14 use lists::{SeparatorTactic, ListTactic};
15 use issues::ReportTactic;
17 #[derive(Copy, Clone, Eq, PartialEq, Debug)]
18 pub enum BlockIndentStyle {
19 // Same level as parent.
21 // One level deeper than parent.
23 // Aligned with block open.
27 impl_enum_decodable!(BlockIndentStyle, Inherit, Tabbed, Visual);
29 #[derive(Copy, Clone, Eq, PartialEq, Debug)]
31 // Fit as much on one line as possible.
37 impl_enum_decodable!(Density, Compressed, Tall);
40 pub fn to_list_tactic(self) -> ListTactic {
42 Density::Compressed => ListTactic::Mixed,
43 Density::Tall => ListTactic::HorizontalVertical,
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
52 // Use vertical layout
57 impl_enum_decodable!(MultilineStyle, PreferSingle, ForceMulti);
60 pub fn to_list_tactic(self) -> ListTactic {
62 MultilineStyle::PreferSingle => ListTactic::HorizontalVertical,
63 MultilineStyle::ForceMulti => ListTactic::Vertical,
68 macro_rules! create_config {
69 ($($i:ident: $ty:ty),+ $(,)*) => (
70 #[derive(RustcDecodable, Clone)]
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>),+
87 fn fill_from_parsed_config(mut self, parsed: &ParsedConfig) -> Config {
89 if let Some(val) = parsed.$i {
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,
101 println!("Decoding config file failed. Config:\n{}", toml);
102 let parsed: toml::Value = toml.parse().unwrap();
103 println!("\n\nParsed:\n{:?}", parsed);
107 Config::default().fill_from_parsed_config(&parsed_config)
110 pub fn override_value(&mut self, key: &str, val: &str) {
114 self.$i = val.parse::<$ty>().unwrap();
117 _ => panic!("Bad config key!")
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
157 impl Default for Config {
158 fn default() -> Config {
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,