From 9b0aa7362c21b01376739a013126a9db3ce355bc Mon Sep 17 00:00:00 2001 From: tyranron Date: Mon, 8 Apr 2019 15:11:55 +0300 Subject: Bootstrap solution --- src/config.rs | 29 ++++++++++++++++++++++++++++- tests/defaults.rs | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 tests/defaults.rs diff --git a/src/config.rs b/src/config.rs index f19e59a..893baea 100644 --- a/src/config.rs +++ b/src/config.rs @@ -129,6 +129,25 @@ impl Config { self.refresh() } + pub fn set_defaults(&mut self, value: &T) -> Result<&mut Config> + where + T: Serialize, + { + match self.kind { + ConfigKind::Mutable { + ref mut defaults, .. + } => { + for (key, val) in Self::try_from(&value)?.collect()? { + defaults.insert(key.parse()?, val); + } + } + + ConfigKind::Frozen => return Err(ConfigError::Frozen), + } + + self.refresh() + } + pub fn set(&mut self, key: &str, value: T) -> Result<&mut Config> where T: Into, @@ -192,13 +211,21 @@ impl Config { T::deserialize(self) } - /// Attempt to deserialize the entire configuration into the requested type. + /// Attempt to serialize the entire configuration from the given type. pub fn try_from(from: &T) -> Result { let mut serializer = ConfigSerializer::default(); from.serialize(&mut serializer)?; Ok(serializer.output) } + /// Attempt to serialize the entire configuration from the given type + /// as default values. + pub fn try_defaults_from(from: &T) -> Result { + let mut c = Self::new(); + c.set_defaults(from)?; + Ok(c) + } + #[deprecated(since = "0.7.0", note = "please use 'try_into' instead")] pub fn deserialize<'de, T: Deserialize<'de>>(self) -> Result { self.try_into() diff --git a/tests/defaults.rs b/tests/defaults.rs new file mode 100644 index 0000000..b00da29 --- /dev/null +++ b/tests/defaults.rs @@ -0,0 +1,36 @@ +extern crate config; + +#[macro_use] +extern crate serde_derive; + +use config::*; + +#[derive(Debug, Serialize, Deserialize)] +pub struct Settings { + pub db_host: String, +} + +impl Default for Settings { + fn default() -> Self { + Settings { + db_host: String::from("default"), + } + } +} + +#[test] +fn set_defaults() { + let mut c = Config::new(); + c.set_defaults(&Settings::default()) + .expect("Setting defaults failed"); + let s: Settings = c.try_into().expect("Deserialization failed"); + + assert_eq!(s.db_host, "default"); +} + +#[test] +fn try_from_defaults() { + let c = Config::try_from(&Settings::default()).expect("Serialization failed"); + let s: Settings = c.try_into().expect("Deserialization failed"); + assert_eq!(s.db_host, "default"); +} -- cgit v1.2.3 From 37403d00d2dcd8c5ceca4ab7f40ef704c6df32d8 Mon Sep 17 00:00:00 2001 From: tyranron Date: Mon, 8 Apr 2019 15:33:14 +0300 Subject: Fix annoying 'invalid type: unit value, expected struct Settings' error --- src/config.rs | 9 +++++++-- tests/empty.rs | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 tests/empty.rs diff --git a/src/config.rs b/src/config.rs index 893baea..03d2453 100644 --- a/src/config.rs +++ b/src/config.rs @@ -10,7 +10,7 @@ use ser::ConfigSerializer; use source::Source; use path; -use value::{Value, ValueKind, ValueWithKey}; +use value::{Table, Value, ValueKind, ValueWithKey}; #[derive(Clone, Debug)] enum ConfigKind { @@ -49,7 +49,12 @@ pub struct Config { impl Config { pub fn new() -> Self { - Config::default() + Self { + kind: ConfigKind::default(), + // Config root should be instantiated as an empty table + // to avoid deserialization errors. + cache: Value::new(None, Table::new()), + } } /// Merge in a configuration property source. diff --git a/tests/empty.rs b/tests/empty.rs new file mode 100644 index 0000000..1f56d38 --- /dev/null +++ b/tests/empty.rs @@ -0,0 +1,21 @@ +extern crate config; + +#[macro_use] +extern crate serde_derive; + +use config::*; + +#[derive(Debug, Serialize, Deserialize)] +struct Settings { + #[serde(skip)] + foo: isize, + #[serde(skip)] + bar: u8, +} + +#[test] +fn empty_deserializes() { + let s: Settings = Config::new().try_into().expect("Deserialization failed"); + assert_eq!(s.foo, 0); + assert_eq!(s.bar, 0); +} -- cgit v1.2.3 From a1eecd00447b66e98bf47b922228a714a3b06782 Mon Sep 17 00:00:00 2001 From: tyranron Date: Mon, 8 Apr 2019 15:47:37 +0300 Subject: Add missing doc --- src/config.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/config.rs b/src/config.rs index 03d2453..fa43e2a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -134,6 +134,7 @@ impl Config { self.refresh() } + /// Set the configuration defaults by serializing them from given value. pub fn set_defaults(&mut self, value: &T) -> Result<&mut Config> where T: Serialize, -- cgit v1.2.3