axol_http/
method.rs

1use std::fmt;
2use std::str::FromStr;
3
4use thiserror::Error;
5
6#[derive(Clone, Copy, PartialEq, Eq, Hash, Default, strum::Display, strum::IntoStaticStr)]
7#[strum(serialize_all = "SCREAMING_SNAKE_CASE")]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9#[cfg_attr(feature = "serde", serde(rename_all = "SCREAMING_SNAKE_CASE"))]
10pub enum Method {
11    #[default]
12    Get,
13    Post,
14    Put,
15    Delete,
16    Head,
17    Options,
18    Connect,
19    Patch,
20    Trace,
21}
22
23#[derive(Error, Debug)]
24pub enum MethodParseError {
25    #[error("unknown method: '{0}'")]
26    UnknownMethod(String),
27}
28
29// we cannot use strum because it won't use our error type
30impl FromStr for Method {
31    type Err = MethodParseError;
32
33    fn from_str(s: &str) -> Result<Self, Self::Err> {
34        match s {
35            "GET" => Ok(Method::Get),
36            "POST" => Ok(Method::Post),
37            "PUT" => Ok(Method::Put),
38            "DELETE" => Ok(Method::Delete),
39            "HEAD" => Ok(Method::Head),
40            "OPTIONS" => Ok(Method::Options),
41            "CONNECT" => Ok(Method::Connect),
42            "PATCH" => Ok(Method::Patch),
43            "TRACE" => Ok(Method::Trace),
44            _ => Err(MethodParseError::UnknownMethod(s.to_string())),
45        }
46    }
47}
48
49impl TryFrom<&str> for Method {
50    type Error = MethodParseError;
51
52    fn try_from(value: &str) -> Result<Self, Self::Error> {
53        Self::from_str(value)
54    }
55}
56
57impl TryFrom<http::Method> for Method {
58    type Error = MethodParseError;
59    fn try_from(value: http::Method) -> Result<Self, MethodParseError> {
60        Method::from_str(value.as_str())
61            .map_err(|_| MethodParseError::UnknownMethod(value.as_str().to_string()))
62    }
63}
64
65impl Into<http::Method> for Method {
66    fn into(self) -> http::Method {
67        match self {
68            Method::Get => http::Method::GET,
69            Method::Post => http::Method::POST,
70            Method::Put => http::Method::PUT,
71            Method::Delete => http::Method::DELETE,
72            Method::Head => http::Method::HEAD,
73            Method::Options => http::Method::OPTIONS,
74            Method::Connect => http::Method::CONNECT,
75            Method::Patch => http::Method::PATCH,
76            Method::Trace => http::Method::TRACE,
77        }
78    }
79}
80
81impl Method {
82    pub fn as_str(&self) -> &'static str {
83        self.into()
84    }
85}
86
87impl AsRef<str> for Method {
88    fn as_ref(&self) -> &str {
89        self.as_str()
90    }
91}
92
93impl fmt::Debug for Method {
94    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
95        write!(f, "{}", self.as_str())
96    }
97}
98
99impl PartialEq<str> for Method {
100    #[inline]
101    fn eq(&self, other: &str) -> bool {
102        self.as_ref() == other
103    }
104}
105
106impl PartialEq<Method> for str {
107    #[inline]
108    fn eq(&self, other: &Method) -> bool {
109        self == other.as_ref()
110    }
111}