diff options
author | Ryan Leckey <leckey.ryan@gmail.com> | 2019-05-09 09:55:38 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-09 09:55:38 -0700 |
commit | c03fd36f4929bcb0a0bfa8ff601cb35ffb95106e (patch) | |
tree | 3a35ad844adc09cfd0a10bc5b3099bd1d8187248 /src/config.rs | |
parent | 2502fbcce7295c56efd9825a14748d98f76ef613 (diff) | |
parent | a1eecd00447b66e98bf47b922228a714a3b06782 (diff) |
Merge pull request #106 from tyranron/60-fix-defaults
Fix defaults serialization and 'invalid type: unit value' deserialization error (#60)
Diffstat (limited to 'src/config.rs')
-rw-r--r-- | src/config.rs | 39 |
1 files changed, 36 insertions, 3 deletions
diff --git a/src/config.rs b/src/config.rs index f19e59a..fa43e2a 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. @@ -129,6 +134,26 @@ impl Config { self.refresh() } + /// Set the configuration defaults by serializing them from given value. + pub fn set_defaults<T>(&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<T>(&mut self, key: &str, value: T) -> Result<&mut Config> where T: Into<Value>, @@ -192,13 +217,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<T: Serialize>(from: &T) -> Result<Self> { 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<T: Serialize>(from: &T) -> Result<Self> { + 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<T> { self.try_into() |