From 0d2e1a871083bd5c2631f150bc6bbb4db68e3e48 Mon Sep 17 00:00:00 2001 From: Egor Larionov Date: Tue, 21 Apr 2020 12:01:06 -0700 Subject: [PATCH] Fix from_str errors caused by a clippy fix The issue was that the from_str implementation on Value was moved to the FromStr trait as recommended by Clippy. This caused many of the tests to fail since now the FromStr trait must be in scope. Instead of bringing FromStr into scope everywhere, however, the `Value::from_str(_)` calls were replaced with `_.parse()` calls as recommended by the std lib documentation. --- examples/transcode.rs | 2 +- src/de/value.rs | 3 ++- tests/value.rs | 32 ++++++++++++++++---------------- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/examples/transcode.rs b/examples/transcode.rs index 401f6512..115ac469 100644 --- a/examples/transcode.rs +++ b/examples/transcode.rs @@ -25,7 +25,7 @@ fn main() { ) "#; - let value = Value::from_str(data).expect("Failed to deserialize"); + let value: Value = data.parse().expect("Failed to deserialize"); let mut ser = serde_json::Serializer::pretty(std::io::stdout()); value.serialize(&mut ser).expect("Failed to serialize"); } diff --git a/src/de/value.rs b/src/de/value.rs index 8cf184a6..da4c6176 100644 --- a/src/de/value.rs +++ b/src/de/value.rs @@ -168,9 +168,10 @@ impl<'de> Visitor<'de> for ValueVisitor { #[cfg(test)] mod tests { use super::*; + use std::str::FromStr; fn eval(s: &str) -> Value { - Value::from_str(s).expect("Failed to parse") + s.parse().expect("Failed to parse") } #[test] diff --git a/tests/value.rs b/tests/value.rs index 6180e597..3ac90c2d 100644 --- a/tests/value.rs +++ b/tests/value.rs @@ -3,13 +3,13 @@ use serde::Serialize; #[test] fn bool() { - assert_eq!(Value::from_str("true"), Ok(Value::Bool(true))); - assert_eq!(Value::from_str("false"), Ok(Value::Bool(false))); + assert_eq!("true".parse(), Ok(Value::Bool(true))); + assert_eq!("false".parse(), Ok(Value::Bool(false))); } #[test] fn char() { - assert_eq!(Value::from_str("'a'"), Ok(Value::Char('a'))); + assert_eq!("'a'".parse(), Ok(Value::Char('a'))); } #[test] @@ -17,14 +17,14 @@ fn map() { let mut map = Map::new(); map.insert(Value::Char('a'), Value::Number(Number::new(1f64))); map.insert(Value::Char('b'), Value::Number(Number::new(2f64))); - assert_eq!(Value::from_str("{ 'a': 1, 'b': 2 }"), Ok(Value::Map(map))); + assert_eq!("{ 'a': 1, 'b': 2 }".parse(), Ok(Value::Map(map))); } #[test] fn number() { - assert_eq!(Value::from_str("42"), Ok(Value::Number(Number::new(42f64)))); + assert_eq!("42".parse(), Ok(Value::Number(Number::new(42f64)))); assert_eq!( - Value::from_str("3.1415"), + "3.1415".parse(), Ok(Value::Number(Number::new(3.1415f64))) ); } @@ -32,32 +32,32 @@ fn number() { #[test] fn option() { let opt = Some(Box::new(Value::Char('c'))); - assert_eq!(Value::from_str("Some('c')"), Ok(Value::Option(opt))); + assert_eq!("Some('c')".parse(), Ok(Value::Option(opt))); } #[test] fn string() { let normal = "\"String\""; - assert_eq!(Value::from_str(normal), Ok(Value::String("String".into()))); + assert_eq!(normal.parse(), Ok(Value::String("String".into()))); let raw = "r\"Raw String\""; - assert_eq!(Value::from_str(raw), Ok(Value::String("Raw String".into()))); + assert_eq!(raw.parse(), Ok(Value::String("Raw String".into()))); let raw_hashes = "r#\"Raw String\"#"; assert_eq!( - Value::from_str(raw_hashes), + raw_hashes.parse(), Ok(Value::String("Raw String".into())) ); let raw_escaped = "r##\"Contains \"#\"##"; assert_eq!( - Value::from_str(raw_escaped), + raw_escaped.parse(), Ok(Value::String("Contains \"#".into())) ); let raw_multi_line = "r\"Multi\nLine\""; assert_eq!( - Value::from_str(raw_multi_line), + raw_multi_line.parse(), Ok(Value::String("Multi\nLine".into())) ); } @@ -68,18 +68,18 @@ fn seq() { Value::Number(Number::new(1f64)), Value::Number(Number::new(2f64)), ]; - assert_eq!(Value::from_str("[1, 2]"), Ok(Value::Seq(seq))); + assert_eq!("[1, 2]".parse(), Ok(Value::Seq(seq))); } #[test] fn unit() { use ron::de::{Error, ParseError, Position}; - assert_eq!(Value::from_str("()"), Ok(Value::Unit)); - assert_eq!(Value::from_str("Foo"), Ok(Value::Unit)); + assert_eq!("()".parse(), Ok(Value::Unit)); + assert_eq!("Foo".parse(), Ok(Value::Unit)); assert_eq!( - Value::from_str(""), + "".parse::(), Err(Error::Parser(ParseError::Eof, Position { col: 1, line: 1 })) ); }