diff --git a/src/de.rs b/src/de.rs index 8b2a9f9..1269682 100644 --- a/src/de.rs +++ b/src/de.rs @@ -23,6 +23,7 @@ where T::deserialize(&mut deserializer) } +/// A deserializer from JSON5 into Rust values. pub struct Deserializer<'de> { pair: Option>, } @@ -30,6 +31,7 @@ pub struct Deserializer<'de> { impl<'de> Deserializer<'de> { /// Creates a JSON5 deserializer from a `&str`. This parses the input at construction time, so /// can fail if the input is not valid JSON5. + #[allow(clippy::should_implement_trait)] // This is not the same as `FromStr::from_str` pub fn from_str(input: &'de str) -> Result { let pair = Parser::parse(Rule::text, input)?.next().unwrap(); Ok(Deserializer::from_pair(pair)) @@ -79,7 +81,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> { { let pair = self.pair.take().unwrap(); let span = pair.as_span(); - let mut res = (move || visitor.visit_enum(Enum { pair }))(); + let mut res = visitor.visit_enum(Enum { pair }); error::set_location(&mut res, &span); res } @@ -224,10 +226,10 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> { { let pair = self.pair.take().unwrap(); let span = pair.as_span(); - let mut res = (move || match pair.as_rule() { + let mut res = match pair.as_rule() { Rule::null => visitor.visit_none(), _ => visitor.visit_some(&mut Deserializer::from_pair(pair)), - })(); + }; error::set_location(&mut res, &span); res } @@ -237,7 +239,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> { V: de::Visitor<'de>, { let span = self.pair.as_ref().unwrap().as_span(); - let mut res = (move || visitor.visit_newtype_struct(self))(); + let mut res = visitor.visit_newtype_struct(self); error::set_location(&mut res, &span); res } @@ -264,7 +266,7 @@ fn parse_string_component(pair: Pair<'_, Rule>) -> Result { match component.as_rule() { Rule::char_literal => result.push_str(component.as_str()), Rule::char_escape_sequence => result.push_str(parse_char_escape_sequence(&component)), - Rule::nul_escape_sequence => result.push_str("\u{0000}"), + Rule::nul_escape_sequence => result.push('\u{0000}'), Rule::hex_escape_sequence => { let hex_escape = parse_hex(component.as_str())?; match char::from_u32(hex_escape) { @@ -298,7 +300,7 @@ fn parse_string_component(pair: Pair<'_, Rule>) -> Result { }; // Join together - let rc = ((rc1 - 0xD800) << 10) | (rc2 - 0xDC00) + 0x1_0000; + let rc = ((rc1 - 0xD800) << 10) | (rc2 - 0xDC00 + 0x1_0000); match char::from_u32(rc) { Some(c) => { result.push(c); @@ -371,7 +373,7 @@ fn parse_integer(pair: &Pair<'_, Rule>) -> Result { s if is_hex_literal(s) => Ok(parse_hex(&s[2..])? as i64), s => s .parse() - .or_else(|_| Err(de::Error::custom("error parsing integer"))), + .map_err(|_| de::Error::custom("error parsing integer")), } } @@ -383,7 +385,7 @@ fn is_int(s: &str) -> bool { } fn parse_hex(s: &str) -> Result { - u32::from_str_radix(s, 16).or_else(|_| Err(de::Error::custom("error parsing hex"))) + u32::from_str_radix(s, 16).map_err(|_| de::Error::custom("error parsing hex")) } fn is_hex_literal(s: &str) -> bool { diff --git a/tests/common.rs b/tests/common.rs index 7572914..596d50d 100644 --- a/tests/common.rs +++ b/tests/common.rs @@ -25,12 +25,12 @@ where } #[allow(unused)] -pub fn deserializes_to_nan_f32<'a>(s: &'a str) { +pub fn deserializes_to_nan_f32(s: &str) { assert_matches!(json5::from_str::(s), Ok(value) if value.is_nan()); } #[allow(unused)] -pub fn deserializes_to_nan_f64<'a>(s: &'a str) { +pub fn deserializes_to_nan_f64(s: &str) { assert_matches!(json5::from_str::(s), Ok(value) if value.is_nan()); } diff --git a/tests/examples.rs b/tests/examples.rs index 831549f..4ceeff2 100644 --- a/tests/examples.rs +++ b/tests/examples.rs @@ -104,7 +104,7 @@ fn deserializes_example_nan() { match json5::from_str::(&contents) { Ok(value) => assert!(value.nan.is_nan() && value.neg_nan.is_nan()), - Err(err) => panic!(format!("{}", err)), + Err(err) => panic!("{}", err), } #[derive(Deserialize, PartialEq, Debug)] @@ -115,7 +115,7 @@ fn deserializes_example_nan() { match json5::from_str::(&contents) { Ok(value) => assert!(value.nan.is_nan() && value.neg_nan.is_nan()), - Err(err) => panic!(format!("{}", err)), + Err(err) => panic!("{}", err), } let mut map = HashMap::new(); @@ -136,7 +136,7 @@ fn deserializes_example_nan() { } _ => panic!("not NaN"), }, - Err(err) => panic!(format!("{}", err)), + Err(err) => panic!("{}", err), } deserializes_to::(