http_type/methods/
impl.rs

1use crate::*;
2
3/// Implements the `Default` trait for `Method`.
4impl Default for Method {
5    /// Returns the default `Method` variant, which is `UNKNOWN` with an empty string.
6    ///
7    /// # Returns
8    ///
9    /// The default `Method` variant.
10    #[inline(always)]
11    fn default() -> Self {
12        Self::Unknown(String::new())
13    }
14}
15
16/// Implements the `Display` trait for `Method`, allowing it to be formatted as a string.
17impl Display for Method {
18    /// Formats the `Method` variant into its string representation.
19    ///
20    /// # Arguments
21    ///
22    /// - `f` - The formatter to write the string into.
23    ///
24    /// # Returns
25    ///
26    /// A `fmt::Result` indicating success or failure of the formatting operation.
27    #[inline(always)]
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        let res: &str = match self {
30            Self::Get => GET,
31            Self::Post => POST,
32            Self::Connect => CONNECT,
33            Self::Delete => DELETE,
34            Self::Head => HEAD,
35            Self::Patch => PATCH,
36            Self::Trace => TRACE,
37            Self::Put => PUT,
38            Self::Options => OPTIONS,
39            Self::Unknown(methods) => methods,
40        };
41        write!(f, "{res}")
42    }
43}
44
45/// Implements the `FromStr` trait for `Method`, allowing conversion from a string slice.
46impl FromStr for Method {
47    /// The error type returned when conversion fails.
48    type Err = ();
49
50    /// Converts a string slice into a `Method` variant.
51    ///
52    /// This method attempts to parse the input string into a known `Method` variant.
53    /// If the string does not match any known method, it returns an `UNKNOWN` variant
54    /// containing the original string.
55    ///
56    /// # Arguments
57    ///
58    /// - `methods_str` - The string slice to convert.
59    ///
60    /// # Returns
61    ///
62    /// A `Result` containing the `Method` variant if successful, or `Self::Err` on failure.
63    #[inline(always)]
64    fn from_str(methods_str: &str) -> Result<Self, Self::Err> {
65        match methods_str {
66            GET => Ok(Self::Get),
67            POST => Ok(Self::Post),
68            PUT => Ok(Self::Put),
69            DELETE => Ok(Self::Delete),
70            PATCH => Ok(Self::Patch),
71            HEAD => Ok(Self::Head),
72            OPTIONS => Ok(Self::Options),
73            CONNECT => Ok(Self::Connect),
74            TRACE => Ok(Self::Trace),
75            _ => Ok(Self::Unknown(methods_str.to_string())),
76        }
77    }
78}
79
80impl Method {
81    /// Checks if the current method is `GET`.
82    ///
83    /// # Returns
84    ///
85    /// `true` if the method is `GET`, `false` otherwise.
86    #[inline(always)]
87    pub fn is_get(&self) -> bool {
88        matches!(self, Self::Get)
89    }
90
91    /// Checks if the current method is `POST`.
92    ///
93    /// # Returns
94    ///
95    /// `true` if the method is `POST`, `false` otherwise.
96    #[inline(always)]
97    pub fn is_post(&self) -> bool {
98        matches!(self, Self::Post)
99    }
100
101    /// Checks if the current method is `PUT`.
102    ///
103    /// # Returns
104    ///
105    /// `true` if the method is `PUT`, `false` otherwise.
106    #[inline(always)]
107    pub fn is_put(&self) -> bool {
108        matches!(self, Self::Put)
109    }
110
111    /// Checks if the current method is `DELETE`.
112    ///
113    /// # Returns
114    ///
115    /// `true` if the method is `DELETE`, `false` otherwise.
116    #[inline(always)]
117    pub fn is_delete(&self) -> bool {
118        matches!(self, Self::Delete)
119    }
120
121    /// Checks if the current method is `PATCH`.
122    ///
123    /// # Returns
124    ///
125    /// `true` if the method is `PATCH`, `false` otherwise.
126    #[inline(always)]
127    pub fn is_patch(&self) -> bool {
128        matches!(self, Self::Patch)
129    }
130
131    /// Checks if the current method is `HEAD`.
132    ///
133    /// # Returns
134    ///
135    /// `true` if the method is `HEAD`, `false` otherwise.
136    #[inline(always)]
137    pub fn is_head(&self) -> bool {
138        matches!(self, Self::Head)
139    }
140
141    /// Checks if the current method is `OPTIONS`.
142    ///
143    /// # Returns
144    ///
145    /// `true` if the method is `OPTIONS`, `false` otherwise.
146    #[inline(always)]
147    pub fn is_options(&self) -> bool {
148        matches!(self, Self::Options)
149    }
150
151    /// Checks if the current method is `CONNECT`.
152    ///
153    /// # Returns
154    ///
155    /// `true` if the method is `CONNECT`, `false` otherwise.
156    #[inline(always)]
157    pub fn is_connect(&self) -> bool {
158        matches!(self, Self::Connect)
159    }
160
161    /// Checks if the current method is `TRACE`.
162    ///
163    /// # Returns
164    ///
165    /// `true` if the method is `TRACE`, `false` otherwise.
166    #[inline(always)]
167    pub fn is_trace(&self) -> bool {
168        matches!(self, Self::Trace)
169    }
170
171    /// Checks if the current method is `UNKNOWN`.
172    ///
173    /// # Returns
174    ///
175    /// `true` if the method is `UNKNOWN`, `false` otherwise.
176    #[inline(always)]
177    pub fn is_unknown(&self) -> bool {
178        matches!(self, Self::Unknown(_))
179    }
180}