use std::fmt::{Display, Error, Formatter, Result as fResult};
#[derive(Eq, Hash, PartialEq, Debug)]
#[derive(Clone)]
pub enum HttpMethod {
GET,
POST,
PUT,
PATCH,
DELETE,
}
impl HttpMethod {
pub(crate) fn is_valid(method: &str) -> bool {
matches!(method, "GET" | "POST" | "PUT" | "PATCH" | "DELETE")
}
}
impl Default for HttpMethod {
fn default() -> Self {
Self::GET
}
}
impl Display for HttpMethod {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
match self {
Self::GET => write!(f, "GET"),
Self::POST => write!(f, "POST"),
Self::PUT => write!(f, "PUT"),
Self::PATCH => write!(f, "PATCH"),
Self::DELETE => write!(f, "DELETE"),
}
}
}
impl TryFrom<&str> for HttpMethod {
type Error = ParseHttpMethodError;
fn try_from(method: &str) -> Result<Self, Self::Error> {
let method = method.to_uppercase();
match method.as_str() {
"GET" => Ok(Self::GET),
"POST" => Ok(Self::POST),
"PUT" => Ok(Self::PUT),
"PATCH" => Ok(Self::PATCH),
"DELETE" => Ok(Self::DELETE),
_ => Err(ParseHttpMethodError),
}
}
}
#[derive(Debug)]
pub struct ParseHttpMethodError;
impl Display for ParseHttpMethodError {
fn fmt(&self, f: &mut Formatter<'_>) -> fResult {
write!(f, "Invalid method for HTTP request")
}
}