use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum Method {
Get,
Head,
Post,
Put,
Patch,
Delete,
Options,
}
impl Method {
pub fn as_str(self) -> &'static str {
match self {
Method::Get => "GET",
Method::Head => "HEAD",
Method::Post => "POST",
Method::Put => "PUT",
Method::Patch => "PATCH",
Method::Delete => "DELETE",
Method::Options => "OPTIONS",
}
}
pub fn parse(value: &str) -> Option<Method> {
match value {
"GET" => Some(Method::Get),
"HEAD" => Some(Method::Head),
"POST" => Some(Method::Post),
"PUT" => Some(Method::Put),
"PATCH" => Some(Method::Patch),
"DELETE" => Some(Method::Delete),
"OPTIONS" => Some(Method::Options),
_ => None,
}
}
pub fn takes_body(self) -> bool {
matches!(self, Method::Post | Method::Put | Method::Patch)
}
}
impl fmt::Display for Method {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}