diff options
author | tyranron <tyranron@gmail.com> | 2019-04-08 15:33:14 +0300 |
---|---|---|
committer | tyranron <tyranron@gmail.com> | 2019-04-08 15:33:14 +0300 |
commit | 37403d00d2dcd8c5ceca4ab7f40ef704c6df32d8 (patch) | |
tree | ca15283bab01cf4f0ea6113b38f4585b69ee6a32 | |
parent | 9b0aa7362c21b01376739a013126a9db3ce355bc (diff) |
Fix annoying 'invalid type: unit value, expected struct Settings' error
-rw-r--r-- | src/config.rs | 9 | ||||
-rw-r--r-- | tests/empty.rs | 21 |
2 files changed, 28 insertions, 2 deletions
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); +} |