use std::fmt;
#[derive(PartialEq, Debug)]
#[non_exhaustive]
pub enum Status {
OK,
Created,
Accepted,
NoContent,
BadRequest,
NotFound,
InternalError,
NotImplemented,
}
impl Status {
pub fn new(status: usize) -> Option<Self> {
match status {
200 => Some(Self::OK),
201 => Some(Self::Created),
202 => Some(Self::Accepted),
204 => Some(Self::NoContent),
400 => Some(Self::BadRequest),
404 => Some(Self::NotFound),
500 => Some(Self::InternalError),
501 => Some(Self::NotImplemented),
_ => None,
}
}
}
impl fmt::Display for Status {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{}",
match self {
Self::OK => 200,
Self::Created => 201,
Self::Accepted => 202,
Self::NoContent => 204,
Self::BadRequest => 400,
Self::NotFound => 404,
Self::InternalError => 500,
Self::NotImplemented => 501,
}
)
}
}
#[derive(PartialEq, Clone, Debug)]
#[non_exhaustive]
pub enum Method {
GET,
HEAD,
POST,
PUT,
DELETE,
}
impl Method {
pub fn new<S>(method: S) -> Option<Self>
where
S: Into<String>,
{
match method.into().as_str() {
"GET" => Some(Self::GET),
"HEAD" => Some(Self::HEAD),
"POST" => Some(Self::POST),
"PUT" => Some(Self::PUT),
"DELETE" => Some(Self::DELETE),
_ => None,
}
}
}
impl fmt::Display for Method {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{}",
match self {
Self::GET => "GET",
Self::HEAD => "HEAD",
Self::POST => "POST",
Self::PUT => "PUT",
Self::DELETE => "DELETE",
}
)
}
}