use crate::errors::ParseError;
use std::fmt;
use std::str::FromStr;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ElifMethod(axum::http::Method);
impl ElifMethod {
pub const GET: Self = Self(axum::http::Method::GET);
pub const POST: Self = Self(axum::http::Method::POST);
pub const PUT: Self = Self(axum::http::Method::PUT);
pub const DELETE: Self = Self(axum::http::Method::DELETE);
pub const PATCH: Self = Self(axum::http::Method::PATCH);
pub const HEAD: Self = Self(axum::http::Method::HEAD);
pub const OPTIONS: Self = Self(axum::http::Method::OPTIONS);
pub const TRACE: Self = Self(axum::http::Method::TRACE);
pub const CONNECT: Self = Self(axum::http::Method::CONNECT);
pub fn from_str(method: &str) -> Result<Self, ParseError> {
axum::http::Method::from_str(method)
.map(Self)
.map_err(ParseError::from)
}
pub fn as_str(&self) -> &str {
self.0.as_str()
}
pub fn is_safe(&self) -> bool {
matches!(
self.0,
axum::http::Method::GET
| axum::http::Method::HEAD
| axum::http::Method::OPTIONS
| axum::http::Method::TRACE
)
}
pub fn is_idempotent(&self) -> bool {
matches!(
self.0,
axum::http::Method::GET
| axum::http::Method::HEAD
| axum::http::Method::PUT
| axum::http::Method::DELETE
| axum::http::Method::OPTIONS
| axum::http::Method::TRACE
)
}
pub(crate) fn to_axum(&self) -> &axum::http::Method {
&self.0
}
pub(crate) fn from_axum(method: axum::http::Method) -> Self {
Self(method)
}
}
impl FromStr for ElifMethod {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
axum::http::Method::from_str(s)
.map(Self)
.map_err(ParseError::from)
}
}
impl fmt::Display for ElifMethod {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}