Skip to content

Commit 37403d0

Browse files
committed
Fix annoying 'invalid type: unit value, expected struct Settings' error
1 parent 9b0aa73 commit 37403d0

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/config.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use ser::ConfigSerializer;
1010
use source::Source;
1111

1212
use path;
13-
use value::{Value, ValueKind, ValueWithKey};
13+
use value::{Table, Value, ValueKind, ValueWithKey};
1414

1515
#[derive(Clone, Debug)]
1616
enum ConfigKind {
@@ -49,7 +49,12 @@ pub struct Config {
4949

5050
impl Config {
5151
pub fn new() -> Self {
52-
Config::default()
52+
Self {
53+
kind: ConfigKind::default(),
54+
// Config root should be instantiated as an empty table
55+
// to avoid deserialization errors.
56+
cache: Value::new(None, Table::new()),
57+
}
5358
}
5459

5560
/// Merge in a configuration property source.

tests/empty.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
extern crate config;
2+
3+
#[macro_use]
4+
extern crate serde_derive;
5+
6+
use config::*;
7+
8+
#[derive(Debug, Serialize, Deserialize)]
9+
struct Settings {
10+
#[serde(skip)]
11+
foo: isize,
12+
#[serde(skip)]
13+
bar: u8,
14+
}
15+
16+
#[test]
17+
fn empty_deserializes() {
18+
let s: Settings = Config::new().try_into().expect("Deserialization failed");
19+
assert_eq!(s.foo, 0);
20+
assert_eq!(s.bar, 0);
21+
}

0 commit comments

Comments
 (0)