@@ -2,6 +2,7 @@ use std::fmt::{self, Display};
2
2
3
3
/// HTTP response status codes.
4
4
///
5
+ /// As defined by [rfc7231 section 6](https://tools.ietf.org/html/rfc7231#section-6).
5
6
/// [Read more](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
6
7
#[ repr( u16 ) ]
7
8
#[ derive( Debug , Clone , Copy , Eq , PartialEq ) ]
@@ -355,6 +356,50 @@ pub enum StatusCode {
355
356
}
356
357
357
358
impl StatusCode {
359
+ /// Returns `true` if the status code is `1xx` range.
360
+ ///
361
+ /// If this returns `true` it indicates that the request was received, continuing process.
362
+ pub fn is_informational ( & self ) -> bool {
363
+ let num: u16 = self . clone ( ) . into ( ) ;
364
+ num >= 100 && num < 200
365
+ }
366
+
367
+ /// Returns `true` if the status code is the `2xx` range.
368
+ ///
369
+ /// If this returns `true` it indicates that the request was successfully received, understood,
370
+ /// and accepted.
371
+ pub fn is_success ( & self ) -> bool {
372
+ let num: u16 = self . clone ( ) . into ( ) ;
373
+ num >= 200 && num < 300
374
+ }
375
+
376
+ /// Returns `true` if the status code is the `3xx` range.
377
+ ///
378
+ /// If this returns `true` it indicates that further action needs to be taken in order to
379
+ /// complete the request.
380
+ pub fn is_redirection ( & self ) -> bool {
381
+ let num: u16 = self . clone ( ) . into ( ) ;
382
+ num >= 300 && num < 400
383
+ }
384
+
385
+ /// Returns `true` if the status code is the `4xx` range.
386
+ ///
387
+ /// If this returns `true` it indicates that the request contains bad syntax or cannot be
388
+ /// fulfilled.
389
+ pub fn is_client_error ( & self ) -> bool {
390
+ let num: u16 = self . clone ( ) . into ( ) ;
391
+ num >= 400 && num < 500
392
+ }
393
+
394
+ /// Returns `true` if the status code is the `5xx` range.
395
+ ///
396
+ /// If this returns `true` it indicates that the server failed to fulfill an apparently valid
397
+ /// request.
398
+ pub fn is_server_error ( & self ) -> bool {
399
+ let num: u16 = self . clone ( ) . into ( ) ;
400
+ num >= 500 && num < 600
401
+ }
402
+
358
403
/// The canonical reason for a given status code
359
404
pub fn canonical_reason ( & self ) -> & ' static str {
360
405
match self {
0 commit comments