http_type/methods/
impl.rs

1use crate::*;
2
3impl Default for Methods {
4    #[inline]
5    fn default() -> Self {
6        Self::UNKNOWN(String::new())
7    }
8}
9
10impl Display for Methods {
11    #[inline]
12    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
13        let res: &str = match self {
14            Self::GET => GET,
15            Self::POST => POST,
16            Self::CONNECT => CONNECT,
17            Self::DELETE => DELETE,
18            Self::HEAD => HEAD,
19            Self::PATCH => PATCH,
20            Self::TRACE => TRACE,
21            Self::PUT => PUT,
22            Self::OPTIONS => OPTIONS,
23            Self::UNKNOWN(methods) => methods,
24        };
25        write!(f, "{}", res)
26    }
27}
28
29impl FromStr for Methods {
30    type Err = ();
31
32    #[inline]
33    fn from_str(methods_str: &str) -> Result<Self, Self::Err> {
34        match methods_str {
35            methods if methods == GET => Ok(Self::GET),
36            methods if methods == POST => Ok(Self::POST),
37            methods if methods == PUT => Ok(Self::PUT),
38            methods if methods == DELETE => Ok(Self::DELETE),
39            methods if methods == PATCH => Ok(Self::PATCH),
40            methods if methods == HEAD => Ok(Self::HEAD),
41            methods if methods == OPTIONS => Ok(Self::OPTIONS),
42            methods if methods == CONNECT => Ok(Self::CONNECT),
43            methods if methods == TRACE => Ok(Self::TRACE),
44            _ => Ok(Self::UNKNOWN(methods_str.to_string())),
45        }
46    }
47}
48
49impl Methods {
50    /// Creates a new instance of `Methods` with the default value of `Self::GET`.
51    ///
52    /// This is a shorthand for using the `default` method.
53    #[inline]
54    pub fn new() -> Self {
55        Self::default()
56    }
57
58    /// Checks if the current method is `GET`.
59    ///
60    /// Returns `true` if the method is `GET`, otherwise returns `false`.
61    #[inline]
62    pub fn is_get(&self) -> bool {
63        matches!(self, Self::GET)
64    }
65
66    /// Checks if the current method is `POST`.
67    ///
68    /// Returns `true` if the method is `POST`, otherwise returns `false`.
69    #[inline]
70    pub fn is_post(&self) -> bool {
71        matches!(self, Self::POST)
72    }
73
74    /// Checks if the current method is `PUT`.
75    ///
76    /// Returns `true` if the method is `PUT`, otherwise returns `false`.
77    #[inline]
78    pub fn is_put(&self) -> bool {
79        matches!(self, Self::PUT)
80    }
81
82    /// Checks if the current method is `DELETE`.
83    ///
84    /// Returns `true` if the method is `DELETE`, otherwise returns `false`.
85    #[inline]
86    pub fn is_delete(&self) -> bool {
87        matches!(self, Self::DELETE)
88    }
89
90    /// Checks if the current method is `PATCH`.
91    ///
92    /// Returns `true` if the method is `PATCH`, otherwise returns `false`.
93    #[inline]
94    pub fn is_patch(&self) -> bool {
95        matches!(self, Self::PATCH)
96    }
97
98    /// Checks if the current method is `HEAD`.
99    ///
100    /// Returns `true` if the method is `HEAD`, otherwise returns `false`.
101    #[inline]
102    pub fn is_head(&self) -> bool {
103        matches!(self, Self::HEAD)
104    }
105
106    /// Checks if the current method is `OPTIONS`.
107    ///
108    /// Returns `true` if the method is `OPTIONS`, otherwise returns `false`.
109    #[inline]
110    pub fn is_options(&self) -> bool {
111        matches!(self, Self::OPTIONS)
112    }
113
114    /// Checks if the current method is `CONNECT`.
115    ///
116    /// Returns `true` if the method is `CONNECT`, otherwise returns `false`.
117    #[inline]
118    pub fn is_connect(&self) -> bool {
119        matches!(self, Self::CONNECT)
120    }
121
122    /// Checks if the current method is `TRACE`.
123    ///
124    /// Returns `true` if the method is `TRACE`, otherwise returns `false`.
125    #[inline]
126    pub fn is_trace(&self) -> bool {
127        matches!(self, Self::TRACE)
128    }
129
130    /// Checks if the current method is `UNKNOWN`.
131    ///
132    /// Returns `true` if the method is `UNKNOWN`, otherwise returns `false`.
133    #[inline]
134    pub fn is_unknown(&self) -> bool {
135        matches!(self, Self::UNKNOWN(_))
136    }
137}