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 pub fn new() -> Self {
86 Self::default()
87 }
88
89 /// Checks if the current method is `GET`.
90 ///
91 /// # Returns
92 ///
93 /// `true` if the method is `GET`, `false` otherwise.
94 pub fn is_get(&self) -> bool {
95 matches!(self, Self::GET)
96 }
97
98 /// Checks if the current method is `POST`.
99 ///
100 /// # Returns
101 ///
102 /// `true` if the method is `POST`, `false` otherwise.
103 pub fn is_post(&self) -> bool {
104 matches!(self, Self::POST)
105 }
106
107 /// Checks if the current method is `PUT`.
108 ///
109 /// # Returns
110 ///
111 /// `true` if the method is `PUT`, `false` otherwise.
112 pub fn is_put(&self) -> bool {
113 matches!(self, Self::PUT)
114 }
115
116 /// Checks if the current method is `DELETE`.
117 ///
118 /// # Returns
119 ///
120 /// `true` if the method is `DELETE`, `false` otherwise.
121 pub fn is_delete(&self) -> bool {
122 matches!(self, Self::DELETE)
123 }
124
125 /// Checks if the current method is `PATCH`.
126 ///
127 /// # Returns
128 ///
129 /// `true` if the method is `PATCH`, `false` otherwise.
130 pub fn is_patch(&self) -> bool {
131 matches!(self, Self::PATCH)
132 }
133
134 /// Checks if the current method is `HEAD`.
135 ///
136 /// # Returns
137 ///
138 /// `true` if the method is `HEAD`, `false` otherwise.
139 pub fn is_head(&self) -> bool {
140 matches!(self, Self::HEAD)
141 }
142
143 /// Checks if the current method is `OPTIONS`.
144 ///
145 /// # Returns
146 ///
147 /// `true` if the method is `OPTIONS`, `false` otherwise.
148 pub fn is_options(&self) -> bool {
149 matches!(self, Self::OPTIONS)
150 }
151
152 /// Checks if the current method is `CONNECT`.
153 ///
154 /// # Returns
155 ///
156 /// `true` if the method is `CONNECT`, `false` otherwise.
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 pub fn is_trace(&self) -> bool {
167 matches!(self, Self::TRACE)
168 }
169
170 /// Checks if the current method is `UNKNOWN`.
171 ///
172 /// # Returns
173 ///
174 /// `true` if the method is `UNKNOWN`, `false` otherwise.
175 pub fn is_unknown(&self) -> bool {
176 matches!(self, Self::UNKNOWN(_))
177 }
178}