use core::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HttpMethod {
GET,
POST,
PUT,
DELETE,
PATCH,
HEAD,
OPTIONS,
}
impl HttpMethod {
pub fn from_bytes(bytes: &[u8]) -> Option<Self> {
match bytes {
b"GET" => Some(HttpMethod::GET),
b"POST" => Some(HttpMethod::POST),
b"PUT" => Some(HttpMethod::PUT),
b"DELETE" => Some(HttpMethod::DELETE),
b"PATCH" => Some(HttpMethod::PATCH),
b"HEAD" => Some(HttpMethod::HEAD),
b"OPTIONS" => Some(HttpMethod::OPTIONS),
_ => None,
}
}
pub fn as_str(&self) -> &'static str {
match self {
HttpMethod::GET => "GET",
HttpMethod::POST => "POST",
HttpMethod::PUT => "PUT",
HttpMethod::DELETE => "DELETE",
HttpMethod::PATCH => "PATCH",
HttpMethod::HEAD => "HEAD",
HttpMethod::OPTIONS => "OPTIONS",
}
}
}
impl fmt::Display for HttpMethod {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}