From c26a941541ccd7733f7533372da71f0a5be36ed5 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 14 Mar 2021 09:26:51 +0100 Subject: Rename try_into/try_from Because of the clash in names with the TryInto and TryFrom traits, we're renaming the functions here to try_serialize/try_deserialize. Signed-off-by: Matthias Beyer --- examples/glob/src/main.rs | 6 +++--- examples/hierarchical-env/src/settings.rs | 2 +- examples/simple/src/main.rs | 2 +- examples/watch/src/main.rs | 2 +- src/config.rs | 8 ++++---- src/ser.rs | 4 ++-- src/value.rs | 14 +++++++------- tests/defaults.rs | 6 +++--- tests/empty.rs | 4 +++- tests/errors.rs | 10 +++++----- tests/file_hjson.rs | 2 +- tests/file_ini.rs | 2 +- tests/file_json.rs | 2 +- tests/file_toml.rs | 2 +- tests/file_yaml.rs | 2 +- tests/get.rs | 12 ++++++------ 16 files changed, 41 insertions(+), 39 deletions(-) diff --git a/examples/glob/src/main.rs b/examples/glob/src/main.rs index e653b77..206ce2e 100644 --- a/examples/glob/src/main.rs +++ b/examples/glob/src/main.rs @@ -19,7 +19,7 @@ fn main() { // Print out our settings (as a HashMap) println!("\n{:?} \n\n-----------", - settings.try_into::>().unwrap()); + settings.try_deserialize::>().unwrap()); // Option 2 // -------- @@ -33,7 +33,7 @@ fn main() { // Print out our settings (as a HashMap) println!("\n{:?} \n\n-----------", - settings.try_into::>().unwrap()); + settings.try_deserialize::>().unwrap()); // Option 3 // -------- @@ -48,5 +48,5 @@ fn main() { // Print out our settings (as a HashMap) println!("\n{:?} \n\n-----------", - settings.try_into::>().unwrap()); + settings.try_deserialize::>().unwrap()); } diff --git a/examples/hierarchical-env/src/settings.rs b/examples/hierarchical-env/src/settings.rs index a341a21..353053e 100644 --- a/examples/hierarchical-env/src/settings.rs +++ b/examples/hierarchical-env/src/settings.rs @@ -65,6 +65,6 @@ impl Settings { println!("database: {:?}", s.get::("database.url")); // You can deserialize (and thus freeze) the entire configuration as - s.try_into() + s.try_deserialize() } } diff --git a/examples/simple/src/main.rs b/examples/simple/src/main.rs index 1a4fcd4..e4e5475 100644 --- a/examples/simple/src/main.rs +++ b/examples/simple/src/main.rs @@ -13,5 +13,5 @@ fn main() { // Print out our settings (as a HashMap) println!("{:?}", - settings.try_into::>().unwrap()); + settings.try_deserialize::>().unwrap()); } diff --git a/examples/watch/src/main.rs b/examples/watch/src/main.rs index 91391e0..86f1bc6 100644 --- a/examples/watch/src/main.rs +++ b/examples/watch/src/main.rs @@ -26,7 +26,7 @@ fn show() { .read() .unwrap() .clone() - .try_into::>() + .try_deserialize::>() .unwrap()); } diff --git a/src/config.rs b/src/config.rs index 1019061..99216ad 100644 --- a/src/config.rs +++ b/src/config.rs @@ -227,20 +227,20 @@ impl Config { } /// Attempt to deserialize the entire configuration into the requested type. - pub fn try_into<'de, T: Deserialize<'de>>(self) -> Result { + pub fn try_deserialize<'de, T: Deserialize<'de>>(self) -> Result { T::deserialize(self) } /// Attempt to serialize the entire configuration from the given type. - pub fn try_from(from: &T) -> Result { + pub fn try_serialize(from: &T) -> Result { let mut serializer = ConfigSerializer::default(); from.serialize(&mut serializer)?; Ok(serializer.output) } - #[deprecated(since = "0.7.0", note = "please use 'try_into' instead")] + #[deprecated(since = "0.7.0", note = "please use 'try_deserialize' instead")] pub fn deserialize<'de, T: Deserialize<'de>>(self) -> Result { - self.try_into() + self.try_deserialize() } } diff --git a/src/ser.rs b/src/ser.rs index 39c02b9..ce016bf 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -710,9 +710,9 @@ mod test { int: 1, seq: vec!["a".to_string(), "b".to_string()], }; - let config = Config::try_from(&test).unwrap(); + let config = Config::try_serialize(&test).unwrap(); - let actual: Test = config.try_into().unwrap(); + let actual: Test = config.try_deserialize().unwrap(); assert_eq!(test, actual); } } diff --git a/src/value.rs b/src/value.rs index 177acf7..700279b 100644 --- a/src/value.rs +++ b/src/value.rs @@ -151,12 +151,12 @@ impl Value { } /// Attempt to deserialize this value into the requested type. - pub fn try_into<'de, T: Deserialize<'de>>(self) -> Result { + pub fn try_deserialize<'de, T: Deserialize<'de>>(self) -> Result { T::deserialize(self) } /// Returns `self` as a bool, if possible. - // FIXME: Should this not be `try_into_*` ? + // FIXME: Should this not be `try_deserialize_*` ? pub fn into_bool(self) -> Result { match self.kind { ValueKind::Boolean(value) => Ok(value), @@ -197,7 +197,7 @@ impl Value { } /// Returns `self` into an i64, if possible. - // FIXME: Should this not be `try_into_*` ? + // FIXME: Should this not be `try_deserialize_*` ? pub fn into_int(self) -> Result { match self.kind { ValueKind::Integer(value) => Ok(value), @@ -242,7 +242,7 @@ impl Value { } /// Returns `self` into a f64, if possible. - // FIXME: Should this not be `try_into_*` ? + // FIXME: Should this not be `try_deserialize_*` ? pub fn into_float(self) -> Result { match self.kind { ValueKind::Float(value) => Ok(value), @@ -287,7 +287,7 @@ impl Value { } /// Returns `self` into a str, if possible. - // FIXME: Should this not be `try_into_*` ? + // FIXME: Should this not be `try_deserialize_*` ? pub fn into_str(self) -> Result { match self.kind { ValueKind::String(value) => Ok(value), @@ -316,7 +316,7 @@ impl Value { } /// Returns `self` into an array, if possible - // FIXME: Should this not be `try_into_*` ? + // FIXME: Should this not be `try_deserialize_*` ? pub fn into_array(self) -> Result> { match self.kind { ValueKind::Array(value) => Ok(value), @@ -356,7 +356,7 @@ impl Value { } /// If the `Value` is a Table, returns the associated Map. - // FIXME: Should this not be `try_into_*` ? + // FIXME: Should this not be `try_deserialize_*` ? pub fn into_table(self) -> Result> { match self.kind { ValueKind::Table(value) => Ok(value), diff --git a/tests/defaults.rs b/tests/defaults.rs index c50dd65..779dfdd 100644 --- a/tests/defaults.rs +++ b/tests/defaults.rs @@ -22,14 +22,14 @@ impl Default for Settings { #[test] fn set_defaults() { let c = Config::new(); - let s: Settings = c.try_into().expect("Deserialization failed"); + let s: Settings = c.try_deserialize().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"); + let c = Config::try_serialize(&Settings::default()).expect("Serialization failed"); + let s: Settings = c.try_deserialize().expect("Deserialization failed"); assert_eq!(s.db_host, "default"); } diff --git a/tests/empty.rs b/tests/empty.rs index 92d3384..57a592e 100644 --- a/tests/empty.rs +++ b/tests/empty.rs @@ -15,7 +15,9 @@ struct Settings { #[test] fn empty_deserializes() { - let s: Settings = Config::new().try_into().expect("Deserialization failed"); + let s: Settings = Config::new() + .try_deserialize() + .expect("Deserialization failed"); assert_eq!(s.foo, 0); assert_eq!(s.bar, 0); } diff --git a/tests/errors.rs b/tests/errors.rs index ec96c98..04f11fe 100644 --- a/tests/errors.rs +++ b/tests/errors.rs @@ -56,7 +56,7 @@ fn test_error_type_detached() { let c = make(); let value = c.get::("boolean_s_parse").unwrap(); - let res = value.try_into::(); + let res = value.try_deserialize::(); assert!(res.is_err()); assert_eq!( @@ -76,14 +76,14 @@ fn test_error_enum_de() { } let on_v: Value = "on".into(); - let on_d = on_v.try_into::(); + let on_d = on_v.try_deserialize::(); assert_eq!( on_d.unwrap_err().to_string(), "enum Diode does not have variant constructor on".to_string() ); let array_v: Value = vec![100, 100].into(); - let array_d = array_v.try_into::(); + let array_d = array_v.try_deserialize::(); assert_eq!( array_d.unwrap_err().to_string(), "value of enum Diode should be represented by either string or table with exactly one key" @@ -97,7 +97,7 @@ fn test_error_enum_de() { .cloned() .collect::>() .into(); - let confused_d = confused_v.try_into::(); + let confused_d = confused_v.try_deserialize::(); assert_eq!( confused_d.unwrap_err().to_string(), "value of enum Diode should be represented by either string or table with exactly one key" @@ -122,7 +122,7 @@ inner: let mut cfg = Config::new(); cfg.merge(File::from_str(CFG, FileFormat::Yaml)).unwrap(); - let e = cfg.try_into::().unwrap_err(); + let e = cfg.try_deserialize::().unwrap_err(); if let ConfigError::Type { key: Some(path), .. } = e diff --git a/tests/file_hjson.rs b/tests/file_hjson.rs index b866dd6..c5e07fb 100644 --- a/tests/file_hjson.rs +++ b/tests/file_hjson.rs @@ -46,7 +46,7 @@ fn test_file() { let c = make(); // Deserialize the entire file as single struct - let s: Settings = c.try_into().unwrap(); + let s: Settings = c.try_deserialize().unwrap(); assert!(s.debug.approx_eq_ulps(&1.0, 2)); assert_eq!(s.production, Some("false".to_string())); diff --git a/tests/file_ini.rs b/tests/file_ini.rs index e473158..7df9d38 100644 --- a/tests/file_ini.rs +++ b/tests/file_ini.rs @@ -36,7 +36,7 @@ fn make() -> Config { #[test] fn test_file() { let c = make(); - let s: Settings = c.try_into().unwrap(); + let s: Settings = c.try_deserialize().unwrap(); assert_eq!( s, Settings { diff --git a/tests/file_json.rs b/tests/file_json.rs index 0dfeffe..d5e9be2 100644 --- a/tests/file_json.rs +++ b/tests/file_json.rs @@ -46,7 +46,7 @@ fn test_file() { let c = make(); // Deserialize the entire file as single struct - let s: Settings = c.try_into().unwrap(); + let s: Settings = c.try_deserialize().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 7591c20..9b4ef65 100644 --- a/tests/file_toml.rs +++ b/tests/file_toml.rs @@ -54,7 +54,7 @@ fn test_file() { let c = make(); // Deserialize the entire file as single struct - let s: Settings = c.try_into().unwrap(); + let s: Settings = c.try_deserialize().unwrap(); assert!(s.debug.approx_eq_ulps(&1.0, 2)); assert_eq!(s.production, Some("false".to_string())); diff --git a/tests/file_yaml.rs b/tests/file_yaml.rs index 41b495c..8709019 100644 --- a/tests/file_yaml.rs +++ b/tests/file_yaml.rs @@ -46,7 +46,7 @@ fn test_file() { let c = make(); // Deserialize the entire file as single struct - let s: Settings = c.try_into().unwrap(); + let s: Settings = c.try_deserialize().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 135d3b0..a8a97d2 100644 --- a/tests/get.rs +++ b/tests/get.rs @@ -134,7 +134,7 @@ fn test_map_struct() { } let c = make(); - let s: Settings = c.try_into().unwrap(); + let s: Settings = c.try_deserialize().unwrap(); assert_eq!(s.place.len(), 8); assert_eq!( @@ -149,7 +149,7 @@ fn test_file_struct() { let c = make(); // Deserialize the entire file as single struct - let s: Settings = c.try_into().unwrap(); + let s: Settings = c.try_deserialize().unwrap(); assert!(s.debug.approx_eq_ulps(&1.0, 2)); assert_eq!(s.production, Some("false".to_string())); @@ -197,7 +197,7 @@ fn test_struct_array() { } let c = make(); - let s: Settings = c.try_into().unwrap(); + let s: Settings = c.try_deserialize().unwrap(); assert_eq!(s.elements.len(), 10); assert_eq!(s.elements[3], "4".to_string()); @@ -219,7 +219,7 @@ fn test_enum() { } let c = make(); - let s: Settings = c.try_into().unwrap(); + let s: Settings = c.try_deserialize().unwrap(); assert_eq!(s.diodes["green"], Diode::Off); assert_eq!(s.diodes["red"], Diode::Brightness(100)); @@ -254,7 +254,7 @@ fn test_enum_key() { } let c = make(); - let s: Settings = c.try_into().unwrap(); + let s: Settings = c.try_deserialize().unwrap(); assert_eq!(s.proton[&Quark::Up], 2); assert_eq!(s.quarks.len(), 6); @@ -268,7 +268,7 @@ fn test_int_key() { } let c = make(); - let s: Settings = c.try_into().unwrap(); + let s: Settings = c.try_deserialize().unwrap(); assert_eq!(s.divisors[&4], 3); assert_eq!(s.divisors.len(), 4); } -- cgit v1.2.3