@@ -46,11 +46,16 @@ impl fmt::Display for Error {
46
46
match self {
47
47
Error :: InvalidCharacter ( offset) => write ! ( f, "invalid character at {}" , offset) ,
48
48
Error :: NumberExpected ( offset) => write ! ( f, "expected number at {}" , offset) ,
49
+ Error :: UnknownUnit ( start, end) if start == end => write ! ( f,
50
+ "time unit needed at {}, for example 100ms or 100sec" ,
51
+ start+1 ,
52
+ ) ,
49
53
Error :: UnknownUnit ( start, end) => write ! (
50
54
f,
51
55
"unknown time unit at {}-{}, \
52
- see documentation of `parse_duration` for the list of supported time units",
53
- start, end
56
+ supported units: ns, us, ms, sec, min, hours, days, weeks, \
57
+ months, years (and few variations)",
58
+ start+1 , end
54
59
) ,
55
60
Error :: NumberOverflow => write ! ( f, "number is too large" ) ,
56
61
Error :: Empty => write ! ( f, "value was empty" ) ,
@@ -410,4 +415,16 @@ mod test {
410
415
assert_eq ! ( parse_duration( "10000000000000y" ) ,
411
416
Err ( Error :: NumberOverflow ) ) ;
412
417
}
418
+
419
+ #[ test]
420
+ fn test_nice_error_message ( ) {
421
+ assert_eq ! ( parse_duration( "10" ) . unwrap_err( ) . to_string( ) ,
422
+ "time unit needed at 3, for example 100ms or 100sec" ) ;
423
+ assert_eq ! ( parse_duration( "10 months 1" ) . unwrap_err( ) . to_string( ) ,
424
+ "time unit needed at 12, for example 100ms or 100sec" ) ;
425
+ assert_eq ! ( parse_duration( "10nights" ) . unwrap_err( ) . to_string( ) ,
426
+ "unknown time unit at 3-8, supported units: \
427
+ ns, us, ms, sec, min, hours, days, weeks, months, \
428
+ years (and few variations)") ;
429
+ }
413
430
}
0 commit comments