@@ -30,8 +30,8 @@ mod serde_impl;
30
30
#[ derive( Debug , Snafu , PartialEq ) ]
31
31
#[ snafu( module) ]
32
32
pub enum DurationParseError {
33
- #[ snafu( display( "empty input" ) ) ]
34
- EmptyInput ,
33
+ #[ snafu( display( "invalid input, either empty or contains non-ascii characters " ) ) ]
34
+ InvalidInput ,
35
35
36
36
#[ snafu( display( "unexpected character {chr:?}" ) ) ]
37
37
UnexpectedCharacter { chr : char } ,
@@ -65,8 +65,9 @@ impl FromStr for Duration {
65
65
use duration_parse_error:: * ;
66
66
let input = s. trim ( ) ;
67
67
68
- if input. is_empty ( ) {
69
- return EmptyInputSnafu . fail ( ) ;
68
+ // An empty or non-ascii input is invalid
69
+ if input. is_empty ( ) || !input. is_ascii ( ) {
70
+ return Err ( DurationParseError :: InvalidInput ) ;
70
71
}
71
72
72
73
let mut chars = input. char_indices ( ) . peekable ( ) ;
@@ -284,8 +285,8 @@ mod test {
284
285
#[ rstest]
285
286
#[ case( "1D" , DurationParseError :: ParseUnitError { unit: "D" . into( ) } ) ]
286
287
#[ case( "2d2" , DurationParseError :: NoUnit { value: 2 } ) ]
287
- #[ case( "1ä" , DurationParseError :: EmptyInput ) ]
288
- #[ case( " " , DurationParseError :: EmptyInput ) ]
288
+ #[ case( "1ä" , DurationParseError :: InvalidInput ) ]
289
+ #[ case( " " , DurationParseError :: InvalidInput ) ]
289
290
fn parse_invalid ( #[ case] input : & str , #[ case] expected_err : DurationParseError ) {
290
291
let err = Duration :: from_str ( input) . unwrap_err ( ) ;
291
292
assert_eq ! ( err, expected_err)
0 commit comments