Skip to content

Commit

Permalink
Nicer error message for plain numeric duration
Browse files Browse the repository at this point in the history
Inspired by #6
  • Loading branch information
tailhook committed Jan 19, 2020
1 parent edfa493 commit 8a13b04
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,16 @@ impl fmt::Display for Error {
match self {
Error::InvalidCharacter(offset) => write!(f, "invalid character at {}", offset),
Error::NumberExpected(offset) => write!(f, "expected number at {}", offset),
Error::UnknownUnit(start, end) if start == end => write!(f,
"time unit needed at {}, for example 100ms or 100sec",
start+1,
),
Error::UnknownUnit(start, end) => write!(
f,
"unknown time unit at {}-{}, \
see documentation of `parse_duration` for the list of supported time units",
start, end
supported units: ns, us, ms, sec, min, hours, days, weeks, \
months, years (and few variations)",
start+1, end
),
Error::NumberOverflow => write!(f, "number is too large"),
Error::Empty => write!(f, "value was empty"),
Expand Down Expand Up @@ -410,4 +415,16 @@ mod test {
assert_eq!(parse_duration("10000000000000y"),
Err(Error::NumberOverflow));
}

#[test]
fn test_nice_error_message() {
assert_eq!(parse_duration("10").unwrap_err().to_string(),
"time unit needed at 3, for example 100ms or 100sec");
assert_eq!(parse_duration("10 months 1").unwrap_err().to_string(),
"time unit needed at 12, for example 100ms or 100sec");
assert_eq!(parse_duration("10nights").unwrap_err().to_string(),
"unknown time unit at 3-8, supported units: \
ns, us, ms, sec, min, hours, days, weeks, months, \
years (and few variations)");
}
}

0 comments on commit 8a13b04

Please sign in to comment.