1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use std::fmt;
use wasm_bindgen::JsValue;
#[derive(Clone, Copy, PartialEq, Debug)]
#[allow(clippy::upper_case_acronyms)]
pub enum Method {
GET,
POST,
PUT,
DELETE,
HEAD,
OPTIONS,
}
impl Method {
pub fn from(s: &str) -> Result<Method, JsValue> {
Ok(match s {
"GET" | "get" => Method::GET,
"POST" | "post" => Method::POST,
"PUT" | "put" => Method::PUT,
"DELETE" | "delete" => Method::DELETE,
"HEAD" | "head" => Method::HEAD,
"OPTIONS" | "options" => Method::OPTIONS,
_ => return Err(JsValue::from_str("Unsupported http method")),
})
}
}
impl fmt::Display for Method {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
write!(
f,
"{}",
match self {
Method::GET => "GET",
Method::POST => "POST",
Method::PUT => "PUT",
Method::DELETE => "DELETE",
Method::HEAD => "HEAD",
Method::OPTIONS => "OPTIONS",
}
)
}
}
#[test]
fn method_str() {
assert_eq!(Method::from("GET").unwrap().to_string(), "GET");
assert_eq!(Method::from("POST").unwrap().to_string(), "POST");
assert_eq!(Method::from("PUT").unwrap().to_string(), "PUT");
assert_eq!(Method::from("DELETE").unwrap().to_string(), "DELETE");
assert_eq!(Method::from("HEAD").unwrap().to_string(), "HEAD");
assert_eq!(Method::from("OPTIONS").unwrap().to_string(), "OPTIONS");
assert!(Method::from("GET").unwrap() == Method::GET);
assert_eq!(format!("{:?}", Method::PUT), "PUT");
}