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