-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #106 from tyranron/60-fix-defaults
Fix defaults serialization and 'invalid type: unit value' deserialization error (#60)
- Loading branch information
Showing
3 changed files
with
93 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |