Skip to content

Commit 69533ce

Browse files
authored
Merge pull request #26 from yoshuawuyts/status-code-methods
add status code classification methods
2 parents 692573f + b35b975 commit 69533ce

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/status_code.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::fmt::{self, Display};
22

33
/// HTTP response status codes.
44
///
5+
/// As defined by [rfc7231 section 6](https://tools.ietf.org/html/rfc7231#section-6).
56
/// [Read more](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
67
#[repr(u16)]
78
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
@@ -355,6 +356,50 @@ pub enum StatusCode {
355356
}
356357

357358
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+
358403
/// The canonical reason for a given status code
359404
pub fn canonical_reason(&self) -> &'static str {
360405
match self {

0 commit comments

Comments
 (0)