Skip to content

Commit

Permalink
simplify error variant
Browse files Browse the repository at this point in the history
  • Loading branch information
asmello committed Oct 21, 2024
1 parent c694765 commit d2ee027
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ mod tests {
assign: json!("foo"),
expected: Err(AssignError::FailedToParseIndex {
offset: 0,
source: ParseIndexError::InvalidCharacters("+".into()),
source: ParseIndexError::InvalidCharacter,
}),
expected_data: json!([]),
},
Expand Down
28 changes: 11 additions & 17 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
//! ```

use crate::Token;
use alloc::{string::String, vec::Vec};
use alloc::string::String;
use core::{fmt, num::ParseIntError, str::FromStr};

/// Represents an abstract index into an array.
Expand Down Expand Up @@ -173,13 +173,7 @@ impl FromStr for Index {
// this comes up with the `+` sign which is valid for
// representing a `usize` but not allowed in RFC 6901 array
// indices
let mut invalid: Vec<_> = s.chars().filter(|c| !c.is_ascii_digit()).collect();
// remove duplicate characters
invalid.sort_unstable();
invalid.dedup();
Err(ParseIndexError::InvalidCharacters(
invalid.into_iter().collect(),
))
Err(ParseIndexError::InvalidCharacter)
}
}
}
Expand Down Expand Up @@ -281,8 +275,8 @@ pub enum ParseIndexError {
InvalidInteger(ParseIntError),
/// The Token contains leading zeros.
LeadingZeros,
/// The Token contains non-digit characters.
InvalidCharacters(String),
/// The Token contains a non-digit character.
InvalidCharacter,
}

impl From<ParseIntError> for ParseIndexError {
Expand All @@ -301,10 +295,10 @@ impl fmt::Display for ParseIndexError {
f,
"token contained leading zeros, which are disallowed by RFC 6901"
),
ParseIndexError::InvalidCharacters(chars) => write!(
ParseIndexError::InvalidCharacter => write!(
f,
"token contains non-digit character(s) '{chars}', \
which are disallowed by RFC 6901",
"token contains the non-digit character '+', \
which is disallowed by RFC 6901",
),
}
}
Expand All @@ -315,7 +309,7 @@ impl std::error::Error for ParseIndexError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
ParseIndexError::InvalidInteger(source) => Some(source),
ParseIndexError::LeadingZeros | ParseIndexError::InvalidCharacters(_) => None,
ParseIndexError::LeadingZeros | ParseIndexError::InvalidCharacter => None,
}
}
}
Expand Down Expand Up @@ -435,9 +429,9 @@ mod tests {
"token contained leading zeros, which are disallowed by RFC 6901"
);
assert_eq!(
ParseIndexError::InvalidCharacters("+@".into()).to_string(),
"token contains non-digit character(s) '+@', \
which are disallowed by RFC 6901"
ParseIndexError::InvalidCharacter.to_string(),
"token contains the non-digit character '+', \
which is disallowed by RFC 6901"
);
}

Expand Down

0 comments on commit d2ee027

Please sign in to comment.