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