Skip to content

Commit

Permalink
Fix from_str errors caused by a clippy fix
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
elrnv committed Apr 21, 2020
1 parent 4ea661f commit 0d2e1a8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
2 changes: 1 addition & 1 deletion examples/transcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
3 changes: 2 additions & 1 deletion src/de/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
32 changes: 16 additions & 16 deletions tests/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,61 +3,61 @@ 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]
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)))
);
}

#[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()))
);
}
Expand All @@ -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::<Value>(),
Err(Error::Parser(ParseError::Eof, Position { col: 1, line: 1 }))
);
}
Expand Down

0 comments on commit 0d2e1a8

Please sign in to comment.