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 /// Creates a new instance of `Method` with the default value of `Self::GET`.
82 ///
83 /// This is a shorthand for using the `default` method.
84 ///
85 /// # Returns
86 ///
87 /// A new `Method` instance.
88 #[inline(always)]
89 pub fn new() -> Self {
90 Self::default()
91 }
92
93 /// Checks if the current method is `GET`.
94 ///
95 /// # Returns
96 ///
97 /// `true` if the method is `GET`, `false` otherwise.
98 #[inline(always)]
99 pub fn is_get(&self) -> bool {
100 matches!(self, Self::GET)
101 }
102
103 /// Checks if the current method is `POST`.
104 ///
105 /// # Returns
106 ///
107 /// `true` if the method is `POST`, `false` otherwise.
108 #[inline(always)]
109 pub fn is_post(&self) -> bool {
110 matches!(self, Self::POST)
111 }
112
113 /// Checks if the current method is `PUT`.
114 ///
115 /// # Returns
116 ///
117 /// `true` if the method is `PUT`, `false` otherwise.
118 #[inline(always)]
119 pub fn is_put(&self) -> bool {
120 matches!(self, Self::PUT)
121 }
122
123 /// Checks if the current method is `DELETE`.
124 ///
125 /// # Returns
126 ///
127 /// `true` if the method is `DELETE`, `false` otherwise.
128 #[inline(always)]
129 pub fn is_delete(&self) -> bool {
130 matches!(self, Self::DELETE)
131 }
132
133 /// Checks if the current method is `PATCH`.
134 ///
135 /// # Returns
136 ///
137 /// `true` if the method is `PATCH`, `false` otherwise.
138 #[inline(always)]
139 pub fn is_patch(&self) -> bool {
140 matches!(self, Self::PATCH)
141 }
142
143 /// Checks if the current method is `HEAD`.
144 ///
145 /// # Returns
146 ///
147 /// `true` if the method is `HEAD`, `false` otherwise.
148 #[inline(always)]
149 pub fn is_head(&self) -> bool {
150 matches!(self, Self::HEAD)
151 }
152
153 /// Checks if the current method is `OPTIONS`.
154 ///
155 /// # Returns
156 ///
157 /// `true` if the method is `OPTIONS`, `false` otherwise.
158 #[inline(always)]
159 pub fn is_options(&self) -> bool {
160 matches!(self, Self::OPTIONS)
161 }
162
163 /// Checks if the current method is `CONNECT`.
164 ///
165 /// # Returns
166 ///
167 /// `true` if the method is `CONNECT`, `false` otherwise.
168 #[inline(always)]
169 pub fn is_connect(&self) -> bool {
170 matches!(self, Self::CONNECT)
171 }
172
173 /// Checks if the current method is `TRACE`.
174 ///
175 /// # Returns
176 ///
177 /// `true` if the method is `TRACE`, `false` otherwise.
178 #[inline(always)]
179 pub fn is_trace(&self) -> bool {
180 matches!(self, Self::TRACE)
181 }
182
183 /// Checks if the current method is `UNKNOWN`.
184 ///
185 /// # Returns
186 ///
187 /// `true` if the method is `UNKNOWN`, `false` otherwise.
188 #[inline(always)]
189 pub fn is_unknown(&self) -> bool {
190 matches!(self, Self::UNKNOWN(_))
191 }
192}