@@ -20,7 +20,7 @@ fn parse_components(
2020 }
2121 }
2222 if num_str. is_empty ( ) {
23- return Err ( anyhow ! ( "Expected number in: {}" , original_input) ) ;
23+ bail ! ( "Expected number in: {}" , original_input) ;
2424 }
2525 let num = num_str
2626 . parse ( )
@@ -33,11 +33,11 @@ fn parse_components(
3333 bail ! ( "Invalid unit '{}' in: {}" , unit, original_input) ;
3434 }
3535 } else {
36- return Err ( anyhow ! (
36+ bail ! (
3737 "Missing unit after number '{}' in: {}" ,
3838 num_str,
3939 original_input
40- ) ) ;
40+ ) ;
4141 }
4242 }
4343 Ok ( result)
@@ -52,10 +52,7 @@ fn parse_iso8601_duration(s: &str, original_input: &str) -> Result<Duration> {
5252 } ;
5353
5454 if !s_after_sign. starts_with ( 'P' ) {
55- return Err ( anyhow ! (
56- "Duration must start with 'P' in: {}" ,
57- original_input
58- ) ) ;
55+ bail ! ( "Duration must start with 'P' in: {}" , original_input) ;
5956 }
6057 let s_after_p = & s_after_sign[ 1 ..] ;
6158
@@ -72,18 +69,18 @@ fn parse_iso8601_duration(s: &str, original_input: &str) -> Result<Duration> {
7269 let time_components = if let Some ( time_str) = time_part {
7370 let comps = parse_components ( time_str, & [ 'H' , 'M' , 'S' ] , original_input) ?;
7471 if comps. is_empty ( ) {
75- return Err ( anyhow ! (
72+ bail ! (
7673 "Time part present but no time components in: {}" ,
7774 original_input
78- ) ) ;
75+ ) ;
7976 }
8077 comps
8178 } else {
8279 vec ! [ ]
8380 } ;
8481
8582 if date_components. is_empty ( ) && time_components. is_empty ( ) {
86- return Err ( anyhow ! ( "No components in duration: {}" , original_input) ) ;
83+ bail ! ( "No components in duration: {}" , original_input) ;
8784 }
8885
8986 // Accumulate date duration
@@ -121,10 +118,10 @@ fn parse_iso8601_duration(s: &str, original_input: &str) -> Result<Duration> {
121118fn parse_human_readable_duration ( s : & str , original_input : & str ) -> Result < Duration > {
122119 let parts: Vec < & str > = s. split_whitespace ( ) . collect ( ) ;
123120 if parts. is_empty ( ) || parts. len ( ) % 2 != 0 {
124- return Err ( anyhow ! (
121+ bail ! (
125122 "Invalid human-readable duration format in: {}" ,
126123 original_input
127- ) ) ;
124+ ) ;
128125 }
129126
130127 let durations: Result < Vec < Duration > > = parts
@@ -154,18 +151,16 @@ pub fn parse_duration(s: &str) -> Result<Duration> {
154151 let original_input = s;
155152 let s = s. trim ( ) ;
156153 if s. is_empty ( ) {
157- return Err ( anyhow ! ( "Empty duration string" ) ) ;
154+ bail ! ( "Empty duration string" ) ;
158155 }
159156
160- fn is_likely_iso8601 ( s : & str ) -> bool {
161- match s. as_bytes ( ) {
162- [ c, ..] if c. eq_ignore_ascii_case ( & b'P' ) => true ,
163- [ b'-' , c, ..] if c. eq_ignore_ascii_case ( & b'P' ) => true ,
164- _ => false ,
165- }
166- }
157+ let is_likely_iso8601 = match s. as_bytes ( ) {
158+ [ c, ..] if c. eq_ignore_ascii_case ( & b'P' ) => true ,
159+ [ b'-' , c, ..] if c. eq_ignore_ascii_case ( & b'P' ) => true ,
160+ _ => false ,
161+ } ;
167162
168- if is_likely_iso8601 ( s ) {
163+ if is_likely_iso8601 {
169164 parse_iso8601_duration ( s, original_input)
170165 } else {
171166 parse_human_readable_duration ( s, original_input)
0 commit comments