Skip to content

Commit

Permalink
Merge pull request #71 from ivanovaleksey/issue-70
Browse files Browse the repository at this point in the history
Deserialize newtype struct
  • Loading branch information
mehcode authored Jul 2, 2018
2 parents e8fa9fe + 375befa commit e5a8323
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 10 deletions.
9 changes: 8 additions & 1 deletion src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,16 @@ impl<'de> de::Deserializer<'de> for Value {
}
}

fn deserialize_newtype_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>
{
visitor.visit_newtype_struct(self)
}

forward_to_deserialize_any! {
char seq
bytes byte_buf map struct unit enum newtype_struct
bytes byte_buf map struct unit enum
identifier ignored_any unit_struct tuple_struct tuple
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,17 @@ pub struct Value {
/// A description of the original location of the value.
///
/// A Value originating from a File might contain:
/// ```
/// ```text
/// Settings.toml
/// ```
///
/// A Value originating from the environment would contain:
/// ```
/// ```text
/// the envrionment
/// ```
///
/// A Value originating from a remote source might contain:
/// ```
/// ```text
/// etcd+http://127.0.0.1:2379
/// ```
origin: Option<String>,
Expand Down
3 changes: 3 additions & 0 deletions tests/Settings.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ debug_s = "true"
production = false
production_s = "false"

code = 53

# errors
boolean_s_parse = "fals"

Expand All @@ -15,6 +17,7 @@ name = "1"
name = "2"

[place]
number = 1
name = "Torre di Pisa"
longitude = 43.7224985
latitude = 10.3970522
Expand Down
2 changes: 1 addition & 1 deletion tests/file_hjson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn test_file() {
let c = make();

// Deserialize the entire file as single struct
let s: Settings = c.deserialize().unwrap();
let s: Settings = c.try_into().unwrap();

assert!(s.debug.approx_eq_ulps(&1.0, 2));
assert_eq!(s.production, Some("false".to_string()));
Expand Down
2 changes: 1 addition & 1 deletion tests/file_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn test_file() {
let c = make();

// Deserialize the entire file as single struct
let s: Settings = c.deserialize().unwrap();
let s: Settings = c.try_into().unwrap();

assert!(s.debug.approx_eq_ulps(&1.0, 2));
assert_eq!(s.production, Some("false".to_string()));
Expand Down
12 changes: 11 additions & 1 deletion tests/file_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use config::*;

#[derive(Debug, Deserialize)]
struct Place {
number: PlaceNumber,
name: String,
longitude: f64,
latitude: f64,
Expand All @@ -21,10 +22,17 @@ struct Place {
rating: Option<f32>,
}

#[derive(Debug, Deserialize, PartialEq)]
struct PlaceNumber(u8);

#[derive(Debug, Deserialize, PartialEq)]
struct AsciiCode(i8);

#[derive(Debug, Deserialize)]
struct Settings {
debug: f64,
production: Option<String>,
code: AsciiCode,
place: Place,
#[serde(rename = "arr")]
elements: Vec<String>,
Expand All @@ -43,10 +51,12 @@ fn test_file() {
let c = make();

// Deserialize the entire file as single struct
let s: Settings = c.deserialize().unwrap();
let s: Settings = c.try_into().unwrap();

assert!(s.debug.approx_eq_ulps(&1.0, 2));
assert_eq!(s.production, Some("false".to_string()));
assert_eq!(s.code, AsciiCode(53));
assert_eq!(s.place.number, PlaceNumber(1));
assert_eq!(s.place.name, "Torre di Pisa");
assert!(s.place.longitude.approx_eq_ulps(&43.7224985, 2));
assert!(s.place.latitude.approx_eq_ulps(&10.3970522, 2));
Expand Down
2 changes: 1 addition & 1 deletion tests/file_yaml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn test_file() {
let c = make();

// Deserialize the entire file as single struct
let s: Settings = c.deserialize().unwrap();
let s: Settings = c.try_into().unwrap();

assert!(s.debug.approx_eq_ulps(&1.0, 2));
assert_eq!(s.production, Some("false".to_string()));
Expand Down
4 changes: 2 additions & 2 deletions tests/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ fn test_map() {
let c = make();
let m: HashMap<String, Value> = c.get("place").unwrap();

assert_eq!(m.len(), 7);
assert_eq!(m.len(), 8);
assert_eq!(
m["name"].clone().into_str().unwrap(),
"Torre di Pisa".to_string()
Expand All @@ -134,7 +134,7 @@ fn test_map_struct() {
let c = make();
let s: Settings = c.try_into().unwrap();

assert_eq!(s.place.len(), 7);
assert_eq!(s.place.len(), 8);
assert_eq!(
s.place["name"].clone().into_str().unwrap(),
"Torre di Pisa".to_string()
Expand Down

0 comments on commit e5a8323

Please sign in to comment.