http_type/methods/
impl.rs1use crate::*;
2
3impl Default for Methods {
4 fn default() -> Self {
5 Self::UNKNOWN(String::new())
6 }
7}
8
9impl Display for Methods {
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 Methods {
28 type Err = ();
29
30 fn from_str(methods_str: &str) -> Result<Self, Self::Err> {
31 match methods_str {
32 methods if methods == GET => Ok(Self::GET),
33 methods if methods == POST => Ok(Self::POST),
34 methods if methods == PUT => Ok(Self::PUT),
35 methods if methods == DELETE => Ok(Self::DELETE),
36 methods if methods == PATCH => Ok(Self::PATCH),
37 methods if methods == HEAD => Ok(Self::HEAD),
38 methods if methods == OPTIONS => Ok(Self::OPTIONS),
39 methods if methods == CONNECT => Ok(Self::CONNECT),
40 methods if methods == TRACE => Ok(Self::TRACE),
41 _ => Ok(Self::UNKNOWN(methods_str.to_string())),
42 }
43 }
44}
45
46impl Methods {
47 pub fn new() -> Self {
51 Self::default()
52 }
53
54 pub fn is_get(&self) -> bool {
58 matches!(self, Self::GET)
59 }
60
61 pub fn is_post(&self) -> bool {
65 matches!(self, Self::POST)
66 }
67
68 pub fn is_put(&self) -> bool {
72 matches!(self, Self::PUT)
73 }
74
75 pub fn is_delete(&self) -> bool {
79 matches!(self, Self::DELETE)
80 }
81
82 pub fn is_patch(&self) -> bool {
86 matches!(self, Self::PATCH)
87 }
88
89 pub fn is_head(&self) -> bool {
93 matches!(self, Self::HEAD)
94 }
95
96 pub fn is_options(&self) -> bool {
100 matches!(self, Self::OPTIONS)
101 }
102
103 pub fn is_connect(&self) -> bool {
107 matches!(self, Self::CONNECT)
108 }
109
110 pub fn is_trace(&self) -> bool {
114 matches!(self, Self::TRACE)
115 }
116
117 pub fn is_unknown(&self) -> bool {
121 matches!(self, Self::UNKNOWN(_))
122 }
123}