summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRyan Leckey <leckey.ryan@gmail.com>2018-07-02 15:04:52 -0700
committerGitHub <noreply@github.com>2018-07-02 15:04:52 -0700
commite5a8323544ec98254d459ec6ecac43c162529390 (patch)
treeffe6f758823f5e9eabf0f0a60a3a5294c1520888
parente8fa9fee96185ddd18ebcef8a925c75459111edb (diff)
parent375befa7ddbdb1761611947226a0c117f4c35a5e (diff)
Merge pull request #71 from ivanovaleksey/issue-70
Deserialize newtype struct
-rw-r--r--src/de.rs9
-rw-r--r--src/value.rs6
-rw-r--r--tests/Settings.toml3
-rw-r--r--tests/file_hjson.rs2
-rw-r--r--tests/file_json.rs2
-rw-r--r--tests/file_toml.rs12
-rw-r--r--tests/file_yaml.rs2
-rw-r--r--tests/get.rs4
8 files changed, 30 insertions, 10 deletions
diff --git a/src/de.rs b/src/de.rs
index 3e86bd2..89c21e0 100644
--- a/src/de.rs
+++ b/src/de.rs
@@ -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/src/value.rs b/src/value.rs
index a8ae94a..4a3186b 100644
--- a/src/value.rs
+++ b/src/value.rs
@@ -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>,
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_hjson.rs b/tests/file_hjson.rs
index f36843a..f8465be 100644
--- a/tests/file_hjson.rs
+++ b/tests/file_hjson.rs
@@ -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()));
diff --git a/tests/file_json.rs b/tests/file_json.rs
index 1d35cae..48836d8 100644
--- a/tests/file_json.rs
+++ b/tests/file_json.rs
@@ -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()));
diff --git a/tests/file_toml.rs b/tests/file_toml.rs
index f272698..7feb6b5 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>,
@@ -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));
diff --git a/tests/file_yaml.rs b/tests/file_yaml.rs
index 529a8e4..9864133 100644
--- a/tests/file_yaml.rs
+++ b/tests/file_yaml.rs
@@ -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()));
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()