@@ -60,14 +60,16 @@ impl BytesContentRange {
6060 /// Update BytesContentRange with range.
6161 ///
6262 /// The range is inclusive: `[start..=end]` as described in `content-range`.
63- pub fn with_range ( mut self , start : u64 , end : u64 ) -> Self {
63+ pub fn with_range ( mut self , start : u64 , end : u64 ) -> Result < Self > {
64+ if end < start {
65+ return Err ( Error :: new (
66+ ErrorKind :: Unexpected ,
67+ format ! ( "invalid BytesContentRange: end ({end}) < start ({start})" ) ,
68+ ) ) ;
69+ }
6470 self . 0 = Some ( start) ;
6571 self . 1 = Some ( end) ;
66- assert ! (
67- end >= start,
68- "invalid BytesContentRange: end ({end}) < start ({start})"
69- ) ;
70- self
72+ Ok ( self )
7173 }
7274
7375 /// Update BytesContentRange with size.
@@ -175,7 +177,7 @@ impl FromStr for BytesContentRange {
175177 . with_operation ( "BytesContentRange::from_str" )
176178 . with_context ( "value" , value) ) ;
177179 }
178- let mut bcr = BytesContentRange :: default ( ) . with_range ( start, end) ;
180+ let mut bcr = BytesContentRange :: default ( ) . with_range ( start, end) ? ;
179181
180182 // Handle size part first.
181183 if s[ 1 ] != "*" {
@@ -196,13 +198,13 @@ mod tests {
196198 (
197199 "range start with unknown size" ,
198200 "bytes 123-123/*" ,
199- BytesContentRange :: default ( ) . with_range( 123 , 123 ) ,
201+ BytesContentRange :: default ( ) . with_range( 123 , 123 ) ? ,
200202 ) ,
201203 (
202204 "range start with known size" ,
203205 "bytes 123-123/1024" ,
204206 BytesContentRange :: default ( )
205- . with_range( 123 , 123 )
207+ . with_range( 123 , 123 ) ?
206208 . with_size( 1024 ) ,
207209 ) ,
208210 (
@@ -222,36 +224,45 @@ mod tests {
222224 }
223225
224226 #[ test]
225- fn test_bytes_content_range_to_string ( ) {
227+ fn test_bytes_content_range_to_string ( ) -> Result < ( ) > {
226228 let h = BytesContentRange :: default ( ) . with_size ( 1024 ) ;
227229 assert_eq ! ( h. to_string( ) , "*/1024" ) ;
228230
229- let h = BytesContentRange :: default ( ) . with_range ( 0 , 1023 ) ;
231+ let h = BytesContentRange :: default ( ) . with_range ( 0 , 1023 ) ? ;
230232 assert_eq ! ( h. to_string( ) , "0-1023/*" ) ;
231233
232234 let h = BytesContentRange :: default ( )
233- . with_range ( 0 , 1023 )
235+ . with_range ( 0 , 1023 ) ?
234236 . with_size ( 1024 ) ;
235237 assert_eq ! ( h. to_string( ) , "0-1023/1024" ) ;
238+
239+ Ok ( ( ) )
236240 }
237241
238242 #[ test]
239- fn test_bytes_content_range_to_header ( ) {
243+ fn test_bytes_content_range_to_header ( ) -> Result < ( ) > {
240244 let h = BytesContentRange :: default ( ) . with_size ( 1024 ) ;
241245 assert_eq ! ( h. to_header( ) , "bytes */1024" ) ;
242246
243- let h = BytesContentRange :: default ( ) . with_range ( 0 , 1023 ) ;
247+ let h = BytesContentRange :: default ( ) . with_range ( 0 , 1023 ) ? ;
244248 assert_eq ! ( h. to_header( ) , "bytes 0-1023/*" ) ;
245249
246250 let h = BytesContentRange :: default ( )
247- . with_range ( 0 , 1023 )
251+ . with_range ( 0 , 1023 ) ?
248252 . with_size ( 1024 ) ;
249253 assert_eq ! ( h. to_header( ) , "bytes 0-1023/1024" ) ;
254+
255+ Ok ( ( ) )
250256 }
251257
252258 #[ test]
253- #[ should_panic( expected = "invalid BytesContentRange: end (50) < start (100)" ) ]
254- fn test_bytes_content_range_len_panics_on_inverted_range ( ) {
255- let _ = BytesContentRange :: default ( ) . with_range ( 100 , 50 ) ;
259+ fn test_bytes_content_range_inverted_range_returns_error ( ) {
260+ let err = BytesContentRange :: default ( )
261+ . with_range ( 100 , 50 )
262+ . unwrap_err ( ) ;
263+ assert ! (
264+ err. to_string( )
265+ . contains( "invalid BytesContentRange: end (50) < start (100)" )
266+ ) ;
256267 }
257268}
0 commit comments