about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorMarcus Klaas <mail@marcusklaas.nl>2015-08-19 21:41:19 +0200
committerMarcus Klaas <mail@marcusklaas.nl>2015-08-19 21:51:03 +0200
commit8e22a73cb73db27e4d4e30b263839164c97efcd6 (patch)
tree2083708c88a4efb64ac462f03b29fc687a704c6d /src
parent2ef0b179558f85a0a0288d8fa1500732cd2512d5 (diff)
Add option to override single configuration lines for tests
Diffstat (limited to 'src')
-rw-r--r--src/config.rs79
-rw-r--r--src/expr.rs6
-rw-r--r--src/lib.rs2
-rw-r--r--src/utils.rs13
4 files changed, 65 insertions, 35 deletions
diff --git a/src/config.rs b/src/config.rs
index 3d41b51661e..357f99a6097 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -26,37 +26,56 @@ pub enum BlockIndentStyle {
 
 impl_enum_decodable!(BlockIndentStyle, Inherit, Tabbed, Visual);
 
-#[derive(RustcDecodable, Clone)]
-pub struct Config {
-    pub max_width: usize,
-    pub ideal_width: usize,
-    pub leeway: usize,
-    pub tab_spaces: usize,
-    pub newline_style: NewlineStyle,
-    pub fn_brace_style: BraceStyle,
-    pub fn_return_indent: ReturnIndent,
-    pub fn_args_paren_newline: bool,
-    pub struct_trailing_comma: SeparatorTactic,
-    pub struct_lit_trailing_comma: SeparatorTactic,
-    pub struct_lit_style: StructLitStyle,
-    pub enum_trailing_comma: bool,
-    pub report_todo: ReportTactic,
-    pub report_fixme: ReportTactic,
-    pub reorder_imports: bool, // Alphabetically, case sensitive.
-    pub expr_indent_style: BlockIndentStyle,
-}
+macro_rules! create_config {
+    ($($i:ident: $ty:ty),+ $(,)*) => (
+        #[derive(RustcDecodable, Clone)]
+        pub struct Config {
+            $(pub $i: $ty),+
+        }
+
+        impl Config {
+            pub fn from_toml(toml: &str) -> Config {
+                let parsed = toml.parse().unwrap();
+                match toml::decode(parsed) {
+                    Some(decoded) => decoded,
+                    None => {
+                        println!("Decoding config file failed. Config:\n{}", toml);
+                        let parsed: toml::Value = toml.parse().unwrap();
+                        println!("\n\nParsed:\n{:?}", parsed);
+                        panic!();
+                    }
+                }
+            }
 
-impl Config {
-    pub fn from_toml(toml: &str) -> Config {
-        let parsed = toml.parse().unwrap();
-        match toml::decode(parsed) {
-            Some(decoded) => decoded,
-            None => {
-                println!("Decoding config file failed. Config:\n{}", toml);
-                let parsed: toml::Value = toml.parse().unwrap();
-                println!("\n\nParsed:\n{:?}", parsed);
-                panic!();
+            pub fn override_value(&mut self, key: &str, val: &str) {
+                match key {
+                    $(
+                        stringify!($i) => {
+                            self.$i = val.parse::<$ty>().unwrap();
+                        }
+                    )+
+                    _ => panic!("Bad config key!")
+                }
             }
         }
-    }
+    )
+}
+
+create_config! {
+    max_width: usize,
+    ideal_width: usize,
+    leeway: usize,
+    tab_spaces: usize,
+    newline_style: NewlineStyle,
+    fn_brace_style: BraceStyle,
+    fn_return_indent: ReturnIndent,
+    fn_args_paren_newline: bool,
+    struct_trailing_comma: SeparatorTactic,
+    struct_lit_trailing_comma: SeparatorTactic,
+    struct_lit_style: StructLitStyle,
+    enum_trailing_comma: bool,
+    report_todo: ReportTactic,
+    report_fixme: ReportTactic,
+    reorder_imports: bool, // Alphabetically, case sensitive.
+    expr_indent_style: BlockIndentStyle,
 }
diff --git a/src/expr.rs b/src/expr.rs
index 1dfff001475..2fbdcb7d6c9 100644
--- a/src/expr.rs
+++ b/src/expr.rs
@@ -617,10 +617,8 @@ fn rewrite_binary_op(context: &RewriteContext,
     let operator_str = context.codemap.span_to_snippet(op.span).unwrap();
 
     // 1 = space between lhs expr and operator
-    let mut result =
-        try_opt!(lhs.rewrite(context,
-                             context.config.max_width - offset - 1 - operator_str.len(),
-                             offset));
+    let max_width = try_opt!(context.config.max_width.checked_sub(operator_str.len() + offset + 1));
+    let mut result = try_opt!(lhs.rewrite(context, max_width, offset));
 
     result.push(' ');
     result.push_str(&operator_str);
diff --git a/src/lib.rs b/src/lib.rs
index cc7111aae27..cd43be56a99 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -80,7 +80,7 @@ pub enum WriteMode {
     NewFile(&'static str),
     // Write the output to stdout.
     Display,
-    // Return the result as a mapping from filenames to StringBuffers.
+    // Return the result as a mapping from filenames to Strings.
     Return(&'static Fn(HashMap<String, String>)),
 }
 
diff --git a/src/utils.rs b/src/utils.rs
index 59f85b3ad27..a57ffb008ea 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -144,6 +144,19 @@ macro_rules! impl_enum_decodable {
                 }
             }
         }
+
+        impl ::std::str::FromStr for $e {
+            type Err = &'static str;
+
+            fn from_str(s: &str) -> Result<Self, Self::Err> {
+                match &*s {
+                    $(
+                        stringify!($x) => Ok($e::$x),
+                    )*
+                    _ => Err("Bad variant"),
+                }
+            }
+        }
     };
 }