use serde::{Serialize, Deserialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Status {
OK = 200,
Created = 201,
Accepted = 202,
NoContent = 204,
MovedPermanently = 301,
Found = 302,
SeeOther = 303,
NotModified = 304,
TemporaryRedirect = 307,
PermanentRedirect = 308,
BadRequest = 400,
Unauthorized = 401,
Forbidden = 403,
NotFound = 404,
MethodNotAllowed = 405,
NotAcceptable = 406,
Conflict = 409,
Gone = 410,
PreconditionFailed = 412,
UnsupportedMediaType = 415,
UnprocessableEntity = 422,
TooManyRequests = 429,
InternalServerError = 500,
NotImplemented = 501,
BadGateway = 502,
ServiceUnavailable = 503,
GatewayTimeout = 504,
}
impl Status {
pub fn is_success(&self) -> bool {
(200..300).contains(&(*self as u16))
}
pub fn is_redirection(&self) -> bool {
(300..400).contains(&(*self as u16))
}
pub fn is_client_error(&self) -> bool {
(400..500).contains(&(*self as u16))
}
pub fn is_server_error(&self) -> bool {
(500..600).contains(&(*self as u16))
}
pub fn to_u16(&self) -> u16 {
*self as u16
}
}
impl From<u16> for Status {
fn from(code: u16) -> Self {
match code {
200 => Status::OK,
201 => Status::Created,
202 => Status::Accepted,
204 => Status::NoContent,
301 => Status::MovedPermanently,
302 => Status::Found,
303 => Status::SeeOther,
304 => Status::NotModified,
307 => Status::TemporaryRedirect,
308 => Status::PermanentRedirect,
400 => Status::BadRequest,
401 => Status::Unauthorized,
403 => Status::Forbidden,
404 => Status::NotFound,
405 => Status::MethodNotAllowed,
406 => Status::NotAcceptable,
409 => Status::Conflict,
410 => Status::Gone,
412 => Status::PreconditionFailed,
415 => Status::UnsupportedMediaType,
422 => Status::UnprocessableEntity,
429 => Status::TooManyRequests,
500 => Status::InternalServerError,
501 => Status::NotImplemented,
502 => Status::BadGateway,
503 => Status::ServiceUnavailable,
504 => Status::GatewayTimeout,
_ => Status::InternalServerError, }
}
}