]> git.lizzy.rs Git - rust.git/commitdiff
Config knows if an option was default or set
authorNick Cameron <ncameron@mozilla.com>
Fri, 16 Jun 2017 04:49:54 +0000 (16:49 +1200)
committerNick Cameron <ncameron@mozilla.com>
Fri, 16 Jun 2017 04:49:54 +0000 (16:49 +1200)
src/config.rs

index 931b99bb5da93cc2f7e289c6493b5e088ae1ec36..153fad178cc1462a5c8c086f5b617f25b8627b34 100644 (file)
@@ -220,8 +220,9 @@ macro_rules! create_config {
         #[derive(Clone)]
         pub struct Config {
             // For each config item, we store a bool indicating whether it has
-            // been accessed and the value.
-            $($i: (Cell<bool>, $ty)),+
+            // been accessed and the value, and a bool whether the option was
+            // manually initialised, or taken from the default,
+            $($i: (Cell<bool>, bool, $ty)),+
         }
 
         // Just like the Config struct but with each property wrapped
@@ -255,7 +256,19 @@ pub fn to_toml(&self) -> Result<String, String> {
         impl<'a> ConfigSetter<'a> {
             $(
             pub fn $i(&mut self, value: $ty) {
-                (self.0).$i.1 = value;
+                (self.0).$i.2 = value;
+            }
+            )+
+        }
+
+        // Query each option, returns true if the user set the option, false if
+        // a default was used.
+        pub struct ConfigWasSet<'a>(&'a Config);
+
+        impl<'a> ConfigWasSet<'a> {
+            $(
+            pub fn $i(&self) -> bool {
+                (self.0).$i.1
             }
             )+
         }
@@ -265,7 +278,7 @@ impl Config {
             $(
             pub fn $i(&self) -> $ty {
                 self.$i.0.set(true);
-                self.$i.1.clone()
+                self.$i.2.clone()
             }
             )+
 
@@ -273,10 +286,15 @@ pub fn set<'a>(&'a mut self) -> ConfigSetter<'a> {
                 ConfigSetter(self)
             }
 
+            pub fn was_set<'a>(&'a self) -> ConfigWasSet<'a> {
+                ConfigWasSet(self)
+            }
+
             fn fill_from_parsed_config(mut self, parsed: PartialConfig) -> Config {
             $(
                 if let Some(val) = parsed.$i {
-                    self.$i.1 = val;
+                    self.$i.1 = true;
+                    self.$i.2 = val;
                 }
             )+
                 self
@@ -320,7 +338,7 @@ pub fn used_options(&self) -> PartialConfig {
                 PartialConfig {
                     $(
                         $i: if self.$i.0.get() {
-                                Some(self.$i.1.clone())
+                                Some(self.$i.2.clone())
                             } else {
                                 None
                             },
@@ -331,7 +349,7 @@ pub fn used_options(&self) -> PartialConfig {
             pub fn all_options(&self) -> PartialConfig {
                 PartialConfig {
                     $(
-                        $i: Some(self.$i.1.clone()),
+                        $i: Some(self.$i.2.clone()),
                     )+
                 }
             }
@@ -341,7 +359,7 @@ pub fn override_value(&mut self, key: &str, val: &str)
                 match key {
                     $(
                         stringify!($i) => {
-                            self.$i.1 = val.parse::<$ty>()
+                            self.$i.2 = val.parse::<$ty>()
                                 .expect(&format!("Failed to parse override for {} (\"{}\") as a {}",
                                                  stringify!($i),
                                                  val,
@@ -444,7 +462,7 @@ impl Default for Config {
             fn default() -> Config {
                 Config {
                     $(
-                        $i: (Cell::new(false), $def),
+                        $i: (Cell::new(false), false, $def),
                     )+
                 }
             }
@@ -610,4 +628,12 @@ fn test_config_used_to_toml() {
             format!("verbose = {}\nskip_children = {}\n", verbose, skip_children)
         );
     }
+
+    #[test]
+    fn test_was_set() {
+        let config = Config::from_toml("hard_tabs = true").unwrap();
+
+        assert_eq!(config.was_set().hard_tabs(), true);
+        assert_eq!(config.was_set().verbose(), false);
+    }
 }