Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support accessing the response code as an integer #70

Merged
merged 3 commits into from
May 14, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 57 additions & 11 deletions src/response/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,9 @@ where
}

/// HTTP status types
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
#[derive(Clone, Copy, Debug, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[repr(u16)]
pub enum Status {
Ok = 200,
Created = 201,
Expand All @@ -411,34 +412,64 @@ pub enum Status {
BadGateway = 502,
ServiceUnavailable = 503,
GatewayTimeout = 504,
Unknown = 0,
Other(u16),
}

impl Status {
pub fn is_informational(&self) -> bool {
let status = *self as u16;
let status = self.as_u16();
(100..=199).contains(&status)
}

pub fn is_successful(&self) -> bool {
let status = *self as u16;
let status = self.as_u16();
(200..=299).contains(&status)
}

pub fn is_redirection(&self) -> bool {
let status = *self as u16;
let status = self.as_u16();
(300..=399).contains(&status)
}

pub fn is_client_error(&self) -> bool {
let status = *self as u16;
let status = self.as_u16();
(400..=499).contains(&status)
}

pub fn is_server_error(&self) -> bool {
let status = *self as u16;
let status = self.as_u16();
(500..=599).contains(&status)
}

fn as_u16(&self) -> u16 {
match self {
Status::Ok => 200,
Status::Created => 201,
Status::Accepted => 202,
Status::NoContent => 204,
Status::PartialContent => 206,
Status::MovedPermanently => 301,
Status::Found => 302,
Status::SeeOther => 303,
Status::NotModified => 304,
Status::TemporaryRedirect => 307,
Status::PermanentRedirect => 308,
Status::BadRequest => 400,
Status::Unauthorized => 401,
Status::Forbidden => 403,
Status::NotFound => 404,
Status::MethodNotAllowed => 405,
Status::Conflict => 409,
Status::UnsupportedMediaType => 415,
Status::RangeNotSatisfiable => 416,
Status::TooManyRequests => 429,
Status::InternalServerError => 500,
Status::BadGateway => 502,
Status::ServiceUnavailable => 503,
Status::GatewayTimeout => 504,
Status::Other(n) => *n,
}
}
}

impl From<u16> for Status {
Expand Down Expand Up @@ -468,14 +499,29 @@ impl From<u16> for Status {
502 => Status::BadGateway,
503 => Status::ServiceUnavailable,
504 => Status::GatewayTimeout,
n => {
warn!("Unknown status code: {:?}", n);
Status::Unknown
}
n => Status::Other(n),
}
}
}

impl Into<u16> for Status {
fn into(self) -> u16 {
self.as_u16()
}
}

impl PartialEq<Self> for Status {
fn eq(&self, rhs: &Self) -> bool {
self.as_u16() == rhs.as_u16()
}
}

impl PartialOrd<Self> for Status {
fn partial_cmp(&self, rhs: &Self) -> Option<core::cmp::Ordering> {
self.as_u16().partial_cmp(&rhs.as_u16())
}
}

#[cfg(test)]
mod tests {
use core::convert::Infallible;
Expand Down
Loading