stream_httparse/
method.rs1#[derive(Debug, PartialEq, Clone)]
4pub enum Method {
5 OPTIONS,
8 GET,
10 HEAD,
13 POST,
15 PUT,
18 DELETE,
21 TRACE,
23 CONNECT,
25}
26
27impl Method {
28 pub fn parse(raw_method: &str) -> Option<Method> {
31 match raw_method {
32 "OPTIONS" => Some(Method::OPTIONS),
33 "GET" => Some(Method::GET),
34 "HEAD" => Some(Method::HEAD),
35 "POST" => Some(Method::POST),
36 "PUT" => Some(Method::PUT),
37 "DELETE" => Some(Method::DELETE),
38 "TRACE" => Some(Method::TRACE),
39 "CONNECT" => Some(Method::CONNECT),
40 _ => None,
41 }
42 }
43
44 pub fn serialize(&self) -> &'static str {
47 match *self {
48 Method::OPTIONS => "OPTIONS",
49 Method::GET => "GET",
50 Method::HEAD => "HEAD",
51 Method::POST => "POST",
52 Method::PUT => "PUT",
53 Method::DELETE => "DELETE",
54 Method::TRACE => "TRACE",
55 Method::CONNECT => "CONNECT",
56 }
57 }
58}
59
60impl std::fmt::Display for Method {
61 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
62 write!(f, "{}", self.serialize())
63 }
64}
65
66#[cfg(feature = "wasm_serialize")]
67impl Method {
68 pub fn wasm_serialize(&self) -> i32 {
70 match *self {
71 Self::OPTIONS => 0,
72 Self::GET => 1,
73 Self::HEAD => 2,
74 Self::POST => 3,
75 Self::PUT => 4,
76 Self::DELETE => 5,
77 Self::TRACE => 6,
78 Self::CONNECT => 7,
79 }
80 }
81
82 pub fn wasm_deserialize(tmp: i32) -> Option<Self> {
85 match tmp {
86 0 => Some(Self::OPTIONS),
87 1 => Some(Self::GET),
88 2 => Some(Self::HEAD),
89 3 => Some(Self::POST),
90 4 => Some(Self::PUT),
91 5 => Some(Self::DELETE),
92 6 => Some(Self::TRACE),
93 7 => Some(Self::CONNECT),
94 _ => None,
95 }
96 }
97}
98
99#[cfg(test)]
100mod tests {
101 use super::*;
102
103 #[test]
104 fn parse_method_options() {
105 assert_eq!(Some(Method::OPTIONS), Method::parse("OPTIONS"));
106 }
107 #[test]
108 fn parse_method_get() {
109 assert_eq!(Some(Method::GET), Method::parse("GET"));
110 }
111 #[test]
112 fn parse_method_head() {
113 assert_eq!(Some(Method::HEAD), Method::parse("HEAD"));
114 }
115 #[test]
116 fn parse_method_post() {
117 assert_eq!(Some(Method::POST), Method::parse("POST"));
118 }
119 #[test]
120 fn parse_method_put() {
121 assert_eq!(Some(Method::PUT), Method::parse("PUT"));
122 }
123 #[test]
124 fn parse_method_delete() {
125 assert_eq!(Some(Method::DELETE), Method::parse("DELETE"));
126 }
127 #[test]
128 fn parse_method_trace() {
129 assert_eq!(Some(Method::TRACE), Method::parse("TRACE"));
130 }
131 #[test]
132 fn parse_method_connect() {
133 assert_eq!(Some(Method::CONNECT), Method::parse("CONNECT"));
134 }
135 #[test]
136 fn parse_method_invalid() {
137 assert_eq!(None, Method::parse("DIFFERENT"));
138 }
139}