From 3f34ff82297a4f1d5f4052fff5ff44bd28504a3e Mon Sep 17 00:00:00 2001 From: Michael Killough Date: Thu, 18 May 2017 12:09:14 +0700 Subject: [PATCH] Return `PartialConfig` from `Config` methods. Leave serialization to the caller, but provide a `PartialConfig.to_toml()` method, to deal with the fact that `file_lines` can't be serialized. Add a simple test. --- src/config.rs | 61 ++++++++++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/src/config.rs b/src/config.rs index d629d4176ca7..2d3077ed43d9 100644 --- a/src/config.rs +++ b/src/config.rs @@ -225,10 +225,21 @@ pub struct Config { // We first parse into `PartialConfig`, then create a default `Config` // and overwrite the properties with corresponding values from `PartialConfig`. #[derive(Deserialize, Serialize, Clone)] - struct PartialConfig { + pub struct PartialConfig { $(pub $i: Option<$ty>),+ } + impl PartialConfig { + pub fn to_toml(&self) -> Result { + // file_lines can't be specified in TOML + let mut cloned = self.clone(); + cloned.file_lines = None; + + toml::to_string(&cloned) + .map_err(|e| format!("Could not output config: {}", e.to_string())) + } + } + // Macro hygiene won't allow us to make `set_$i()` methods on Config // for each item, so this struct is used to give the API to set values: // `config.get().option(false)`. It's pretty ugly. Consider replacing @@ -300,8 +311,8 @@ pub fn from_toml(toml: &str) -> Result { } } - pub fn used_to_toml(&self) -> Result { - let mut partial = PartialConfig { + pub fn used_options(&self) -> PartialConfig { + PartialConfig { $( $i: if self.$i.0.get() { Some(self.$i.1.clone()) @@ -309,27 +320,15 @@ pub fn used_to_toml(&self) -> Result { None }, )+ - }; - - // file_lines is special and can't be specified in toml. - partial.file_lines = None; - - toml::to_string(&partial) - .map_err(|e| format!("Could not output config: {}", e.to_string())) + } } - pub fn to_toml(&self) -> Result { - let mut partial = PartialConfig { + pub fn all_options(&self) -> PartialConfig { + PartialConfig { $( $i: Some(self.$i.1.clone()), )+ - }; - - // file_lines is special and can't be specified in toml. - partial.file_lines = None; - - toml::to_string(&partial) - .map_err(|e| format!("Could not output config: {}", e.to_string())) + } } pub fn override_value(&mut self, key: &str, val: &str) @@ -491,17 +490,6 @@ fn default() -> Config { mod test { use super::Config; - #[test] - fn test_config_tracking() { - let config = Config::default(); - assert!(!config.verbose.0.get()); - config.verbose(); - config.skip_children(); - assert!(config.verbose.0.get()); - assert!(config.skip_children.0.get()); - assert!(!config.disable_all_formatting.0.get()); - } - #[test] fn test_config_set() { let mut config = Config::default(); @@ -510,4 +498,17 @@ fn test_config_set() { config.set().verbose(true); assert_eq!(config.verbose(), true); } + + #[test] + fn test_config_used_to_toml() { + let config = Config::default(); + + let verbose = config.verbose(); + let skip_children = config.skip_children(); + + let used_options = config.used_options(); + let toml = used_options.to_toml().unwrap(); + assert_eq!(toml, + format!("verbose = {}\nskip_children = {}\n", verbose, skip_children)); + } }