use axum::http::Method as HttpMethod;
pub use axum::http::method::InvalidMethod;
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct Method(Inner);
#[derive(Clone, PartialEq, Eq, Hash)]
enum Inner {
Head,
Get,
Put,
Patch,
Delete,
Options,
Propfind,
Proppatch,
Mkcol,
Copy,
Move,
Lock,
Unlock,
Report,
}
impl Method {
pub const GET: Self = Self(Inner::Get);
pub const HEAD: Self = Self(Inner::Head);
pub const PUT: Self = Self(Inner::Put);
pub const PATCH: Self = Self(Inner::Patch);
pub const DELETE: Self = Self(Inner::Delete);
pub const OPTIONS: Self = Self(Inner::Options);
pub const PROPFIND: Self = Self(Inner::Propfind);
pub const PROPPATCH: Self = Self(Inner::Proppatch);
pub const MKCOL: Self = Self(Inner::Mkcol);
pub const COPY: Self = Self(Inner::Copy);
pub const MOVE: Self = Self(Inner::Move);
pub const LOCK: Self = Self(Inner::Lock);
pub const UNLOCK: Self = Self(Inner::Unlock);
pub const REPORT: Self = Self(Inner::Report);
pub(crate) fn from_http_method(method: &HttpMethod) -> Result<Self, InvalidMethod> {
match *method {
HttpMethod::GET => Ok(Self::GET),
HttpMethod::HEAD => Ok(Self::HEAD),
HttpMethod::PUT => Ok(Self::PUT),
HttpMethod::PATCH => Ok(Self::PATCH),
HttpMethod::DELETE => Ok(Self::DELETE),
HttpMethod::OPTIONS => Ok(Self::OPTIONS),
_ => match method.as_str() {
"PROPFIND" => Ok(Self::PROPFIND),
"PROPPATCH" => Ok(Self::PROPPATCH),
"MKCOL" => Ok(Self::MKCOL),
"COPY" => Ok(Self::COPY),
"MOVE" => Ok(Self::MOVE),
"LOCK" => Ok(Self::LOCK),
"UNLOCK" => Ok(Self::UNLOCK),
"REPORT" => Ok(Self::REPORT),
_ => Err(HttpMethod::from_bytes(b"").unwrap_err()),
},
}
}
#[inline]
pub(crate) fn as_str(&self) -> &'static str {
match self.0 {
Inner::Head => "HEAD",
Inner::Get => "GET",
Inner::Put => "PUT",
Inner::Patch => "PATCH",
Inner::Delete => "DELETE",
Inner::Options => "OPTIONS",
Inner::Propfind => "PROPFIND",
Inner::Proppatch => "PROPPATCH",
Inner::Mkcol => "MKCOL",
Inner::Copy => "COPY",
Inner::Move => "MOVE",
Inner::Lock => "LOCK",
Inner::Unlock => "UNLOCK",
Inner::Report => "REPORT",
}
}
}
impl std::fmt::Display for Method {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.as_str().fmt(f)
}
}
impl std::convert::TryFrom<&HttpMethod> for Method {
type Error = InvalidMethod;
fn try_from(value: &HttpMethod) -> Result<Self, Self::Error> {
Self::from_http_method(value)
}
}