elif_http/request/
method.rs1use std::fmt;
4use std::str::FromStr;
5use crate::errors::ParseError;
6
7#[derive(Debug, Clone, PartialEq, Eq, Hash)]
9pub struct ElifMethod(axum::http::Method);
10
11impl ElifMethod {
12 pub const GET: Self = Self(axum::http::Method::GET);
14 pub const POST: Self = Self(axum::http::Method::POST);
15 pub const PUT: Self = Self(axum::http::Method::PUT);
16 pub const DELETE: Self = Self(axum::http::Method::DELETE);
17 pub const PATCH: Self = Self(axum::http::Method::PATCH);
18 pub const HEAD: Self = Self(axum::http::Method::HEAD);
19 pub const OPTIONS: Self = Self(axum::http::Method::OPTIONS);
20 pub const TRACE: Self = Self(axum::http::Method::TRACE);
21 pub const CONNECT: Self = Self(axum::http::Method::CONNECT);
22
23 pub fn from_str(method: &str) -> Result<Self, ParseError> {
25 axum::http::Method::from_str(method)
26 .map(Self)
27 .map_err(ParseError::from)
28 }
29
30 pub fn as_str(&self) -> &str {
32 self.0.as_str()
33 }
34
35 pub fn is_safe(&self) -> bool {
37 matches!(self.0, axum::http::Method::GET | axum::http::Method::HEAD |
38 axum::http::Method::OPTIONS | axum::http::Method::TRACE)
39 }
40
41 pub fn is_idempotent(&self) -> bool {
43 matches!(self.0, axum::http::Method::GET | axum::http::Method::HEAD |
44 axum::http::Method::PUT | axum::http::Method::DELETE |
45 axum::http::Method::OPTIONS | axum::http::Method::TRACE)
46 }
47
48 pub(crate) fn to_axum(&self) -> &axum::http::Method {
50 &self.0
51 }
52
53 pub(crate) fn from_axum(method: axum::http::Method) -> Self {
55 Self(method)
56 }
57}
58
59impl FromStr for ElifMethod {
60 type Err = ParseError;
61
62 fn from_str(s: &str) -> Result<Self, Self::Err> {
63 axum::http::Method::from_str(s)
64 .map(Self)
65 .map_err(ParseError::from)
66 }
67}
68
69impl fmt::Display for ElifMethod {
70 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71 write!(f, "{}", self.0)
72 }
73}