use crate::types::http::*; use std::fmt; impl fmt::Display for Method { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Method::ALL => write!(f, "ALL"), Method::GET => write!(f, "GET"), Method::POST => write!(f, "POST"), Method::PUT => write!(f, "PUT"), Method::DELETE => write!(f, "DELETE"), } } } impl From for Method { fn from(s: String) -> Self { match s.to_uppercase().as_str() { "GET" => Method::GET, "POST" => Method::POST, "PUT" => Method::PUT, "DELETE" => Method::DELETE, _ => Method::ALL, } } } impl StatusCode { pub fn to_code(&self) -> u16 { *self as u16 } pub fn reason_phrase(&self) -> &'static str { match self { StatusCode::Ok => "OK", StatusCode::Created => "Created", StatusCode::NoContent => "No Content", StatusCode::MovedPermanently => "Moved Permanently", StatusCode::Found => "Found", StatusCode::SeeOther => "See Other", StatusCode::NotModified => "Not Modified", StatusCode::TemporaryRedirect => "Temporary Redirect", StatusCode::BadRequest => "Bad Request", StatusCode::Unauthorized => "Unauthorized", StatusCode::Forbidden => "Forbidden", StatusCode::NotFound => "Not Found", StatusCode::MethodNotAllowed => "Method Not Allowed", StatusCode::InternalServerError => "Internal Server Error", } } } impl From for StatusCode { fn from(code: u16) -> Self { match code { 200 => StatusCode::Ok, 201 => StatusCode::Created, 204 => StatusCode::NoContent, 301 => StatusCode::MovedPermanently, 302 => StatusCode::Found, 303 => StatusCode::SeeOther, 304 => StatusCode::NotModified, 307 => StatusCode::TemporaryRedirect, 400 => StatusCode::BadRequest, 401 => StatusCode::Unauthorized, 403 => StatusCode::Forbidden, 404 => StatusCode::NotFound, 405 => StatusCode::MethodNotAllowed, 500 => StatusCode::InternalServerError, _ => panic!("Unsupported status code"), } } } impl Default for Response { fn default() -> Self { Self::new() } } impl From<&mut Response> for Response { fn from(response: &mut Response) -> Self { std::mem::replace(response, Response::default()) } }