about summary refs log tree commit diff
diff options
context:
space:
mode:
authorChristoph Burgdorf <christoph.burgdorf@bvsn.org>2015-09-04 23:01:20 +0200
committerChristoph Burgdorf <christoph.burgdorf@bvsn.org>2015-09-05 00:09:26 +0200
commit14a94f0bcd4d6892674cbe7dcaf8f3613cac30fc (patch)
tree51db46d53df05955b7dddaa10e0b4da955ce9e23
parent17302f8967f22919ff374cb8e57f81e3741a85f3 (diff)
Allow partial rustfmt.toml
With this change one can use a config file
that only specifies a subset of config keys
to overwrite. E.g. a config file that looks like this

struct_trailing_comma = "Never"
struct_lit_trailing_comma = "Never"

Fixes #255
-rw-r--r--src/config.rs25
1 files changed, 23 insertions, 2 deletions
diff --git a/src/config.rs b/src/config.rs
index 1b4d79ab6ee..c7525e930ec 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -52,10 +52,30 @@ macro_rules! create_config {
             $(pub $i: $ty),+
         }
 
+        // Just like the Config struct but with each property wrapped
+        // as Option<T>. This is used to parse a rustfmt.toml that doesn't
+        // specity all properties of `Config`.
+        // We first parse into `ParsedConfig`, then create a default `Config`
+        // and overwrite the properties with corresponding values from `ParsedConfig`
+        #[derive(RustcDecodable, Clone)]
+        pub struct ParsedConfig {
+            $(pub $i: Option<$ty>),+
+        }
+
         impl Config {
+
+            fn fill_from_parsed_config(mut self, parsed: &ParsedConfig) -> Config {
+            $(
+                if let Some(val) = parsed.$i {
+                    self.$i = val;
+                }
+            )+
+                self
+            }
+
             pub fn from_toml(toml: &str) -> Config {
                 let parsed = toml.parse().unwrap();
-                match toml::decode(parsed) {
+                let parsed_config:ParsedConfig = match toml::decode(parsed) {
                     Some(decoded) => decoded,
                     None => {
                         println!("Decoding config file failed. Config:\n{}", toml);
@@ -63,7 +83,8 @@ macro_rules! create_config {
                         println!("\n\nParsed:\n{:?}", parsed);
                         panic!();
                     }
-                }
+                };
+                Config::default().fill_from_parsed_config(&parsed_config)
             }
 
             pub fn override_value(&mut self, key: &str, val: &str) {