diff options
author | Aleksey Ivanov <ialexxei@gmail.com> | 2018-04-09 20:03:07 +0300 |
---|---|---|
committer | Aleksey Ivanov <ialexxei@gmail.com> | 2018-04-09 21:40:34 +0300 |
commit | 7ba954942ab9acd8b658636cf775c4360ec76272 (patch) | |
tree | 4d55b16a9c7270c7e0b17afaf20a7b213d24e00b | |
parent | e8fa9fee96185ddd18ebcef8a925c75459111edb (diff) |
[Close #70] Support newtype struct deserialization
-rw-r--r-- | src/de.rs | 9 | ||||
-rw-r--r-- | tests/Settings.toml | 3 | ||||
-rw-r--r-- | tests/file_toml.rs | 10 | ||||
-rw-r--r-- | tests/get.rs | 4 |
4 files changed, 23 insertions, 3 deletions
@@ -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 } } diff --git a/tests/Settings.toml b/tests/Settings.toml index 2f2da74..14f881f 100644 --- a/tests/Settings.toml +++ b/tests/Settings.toml @@ -3,6 +3,8 @@ debug_s = "true" production = false production_s = "false" +code = 53 + # errors boolean_s_parse = "fals" @@ -15,6 +17,7 @@ name = "1" name = "2" [place] +number = 1 name = "Torre di Pisa" longitude = 43.7224985 latitude = 10.3970522 diff --git a/tests/file_toml.rs b/tests/file_toml.rs index f272698..2d8203f 100644 --- a/tests/file_toml.rs +++ b/tests/file_toml.rs @@ -11,6 +11,7 @@ use config::*; #[derive(Debug, Deserialize)] struct Place { + number: PlaceNumber, name: String, longitude: f64, latitude: f64, @@ -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>, @@ -47,6 +55,8 @@ fn test_file() { 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)); diff --git a/tests/get.rs b/tests/get.rs index 517339e..dac138c 100644 --- a/tests/get.rs +++ b/tests/get.rs @@ -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() @@ -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() |