Skip to content

Commit

Permalink
Non-ASCII input will be caught anyway, because it's never a valid cha…
Browse files Browse the repository at this point in the history
…racter
  • Loading branch information
nightkr committed Sep 15, 2023
1 parent 705ed1f commit fe1911e
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/duration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ mod serde_impl;
#[derive(Debug, Snafu, PartialEq)]
#[snafu(module)]
pub enum DurationParseError {
#[snafu(display("invalid input, either empty or contains non-ascii characters"))]
InvalidInput,
#[snafu(display("empty input"))]
EmptyInput,

#[snafu(display("unexpected character {chr:?}"))]
UnexpectedCharacter { chr: char },
Expand All @@ -55,9 +55,8 @@ impl FromStr for Duration {
use duration_parse_error::*;
let input = s.trim();

// An empty or non-ascii input is invalid
if input.is_empty() || !input.is_ascii() {
return Err(DurationParseError::InvalidInput);
if input.is_empty() {
return EmptyInputSnafu.fail();
}

let mut chars = input.char_indices().peekable();
Expand Down Expand Up @@ -247,8 +246,8 @@ mod test {
#[rstest]
#[case("1D", DurationParseError::ParseUnitError{unit: "D".into()})]
#[case("2d2", DurationParseError::NoUnit{value: 2})]
#[case("1ä", DurationParseError::InvalidInput)]
#[case(" ", DurationParseError::InvalidInput)]
#[case("1ä", DurationParseError::EmptyInput)]
#[case(" ", DurationParseError::EmptyInput)]
fn parse_invalid(#[case] input: &str, #[case] expected_err: DurationParseError) {
let err = Duration::from_str(input).unwrap_err();
assert_eq!(err, expected_err)
Expand Down

0 comments on commit fe1911e

Please sign in to comment.