Skip to content

Fix Clippy warnings #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ where
T::deserialize(&mut deserializer)
}

/// A deserializer from JSON5 into Rust values.
pub struct Deserializer<'de> {
pair: Option<Pair<'de, Rule>>,
}

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<Self> {
let pair = Parser::parse(Rule::text, input)?.next().unwrap();
Ok(Deserializer::from_pair(pair))
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -264,7 +266,7 @@ fn parse_string_component(pair: Pair<'_, Rule>) -> Result<String> {
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) {
Expand Down Expand Up @@ -298,7 +300,7 @@ fn parse_string_component(pair: Pair<'_, Rule>) -> Result<String> {
};

// 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);
Expand Down Expand Up @@ -371,7 +373,7 @@ fn parse_integer(pair: &Pair<'_, Rule>) -> Result<i64> {
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")),
}
}

Expand All @@ -383,7 +385,7 @@ fn is_int(s: &str) -> bool {
}

fn parse_hex(s: &str) -> Result<u32> {
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 {
Expand Down
4 changes: 2 additions & 2 deletions tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<f32>(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::<f64>(s), Ok(value) if value.is_nan());
}

Expand Down
6 changes: 3 additions & 3 deletions tests/examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ fn deserializes_example_nan() {

match json5::from_str::<NanF64>(&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)]
Expand All @@ -115,7 +115,7 @@ fn deserializes_example_nan() {

match json5::from_str::<NanF32>(&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();
Expand All @@ -136,7 +136,7 @@ fn deserializes_example_nan() {
}
_ => panic!("not NaN"),
},
Err(err) => panic!(format!("{}", err)),
Err(err) => panic!("{}", err),
}

deserializes_to::<Value>(
Expand Down