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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use core::convert::{TryFrom, From};
use crate::error::HttpError;
#[derive(Debug, PartialEq)]
pub enum Method {
Get,
Post,
Put,
Delete,
Head,
Options,
Connect,
Patch,
Trace,
}
impl TryFrom<&str> for Method {
type Error = HttpError;
fn try_from(src: &str) -> Result<Self, Self::Error> {
match src {
"GET" => Ok(Method::Get),
"POST" => Ok(Method::Post),
"PUT" => Ok(Method::Put),
"DELETE" => Ok(Method::Delete),
"HEAD" => Ok(Method::Head),
"OPTIONS" => Ok(Method::Options),
"CONNECT" => Ok(Method::Connect),
"PATCH" => Ok(Method::Patch),
"TRACE" => Ok(Method::Trace),
_ => Err(HttpError::InvalidMethod),
}
}
}
impl From<Method> for Vec<u8> {
fn from(method: Method) -> Self {
match method {
Method::Get => b"GET".to_vec(),
Method::Post => b"POST".to_vec(),
Method::Put => b"PUT".to_vec(),
Method::Delete => b"DELETE".to_vec(),
Method::Head => b"HEAD".to_vec(),
Method::Options => b"OPTIONS".to_vec(),
Method::Connect => b"CONNECT".to_vec(),
Method::Patch => b"PATCH".to_vec(),
Method::Trace => b"TRACE".to_vec(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn from_str_get_test() {
assert_eq!(Method::try_from("GET"), Ok(Method::Get));
}
#[test]
fn to_bytes_get_test() {
assert_eq!(Vec::<u8>::from(Method::Get), b"GET".to_vec());
}
#[test]
fn from_str_post_test() {
assert_eq!(Method::try_from("POST"), Ok(Method::Post));
}
#[test]
fn to_bytes_post_test() {
assert_eq!(Vec::<u8>::from(Method::Post), b"POST".to_vec());
}
#[test]
fn from_str_put_test() {
assert_eq!(Method::try_from("PUT"), Ok(Method::Put));
}
#[test]
fn to_bytes_put_test() {
assert_eq!(Vec::<u8>::from(Method::Put), b"PUT".to_vec());
}
#[test]
fn from_str_delete_test() {
assert_eq!(Method::try_from("DELETE"), Ok(Method::Delete));
}
#[test]
fn to_bytes_delete_test() {
assert_eq!(Vec::<u8>::from(Method::Delete), b"DELETE".to_vec());
}
#[test]
fn from_str_head_test() {
assert_eq!(Method::try_from("HEAD"), Ok(Method::Head));
}
#[test]
fn to_bytes_head_test() {
assert_eq!(Vec::<u8>::from(Method::Head), b"HEAD".to_vec());
}
#[test]
fn from_str_options_test() {
assert_eq!(Method::try_from("OPTIONS"), Ok(Method::Options));
}
#[test]
fn to_bytes_options_test() {
assert_eq!(Vec::<u8>::from(Method::Options), b"OPTIONS".to_vec());
}
#[test]
fn from_str_connect_test() {
assert_eq!(Method::try_from("CONNECT"), Ok(Method::Connect));
}
#[test]
fn to_bytes_connect_test() {
assert_eq!(Vec::<u8>::from(Method::Connect), b"CONNECT".to_vec());
}
#[test]
fn from_str_patch_test() {
assert_eq!(Method::try_from("PATCH"), Ok(Method::Patch));
}
#[test]
fn to_bytes_patch_test() {
assert_eq!(Vec::<u8>::from(Method::Patch), b"PATCH".to_vec());
}
#[test]
fn from_str_trace_test() {
assert_eq!(Method::try_from("TRACE"), Ok(Method::Trace));
}
#[test]
fn to_bytes_trace_test() {
assert_eq!(Vec::<u8>::from(Method::Trace), b"TRACE".to_vec());
}
#[test]
fn from_str_invalid_method_test() {
assert_eq!(Method::try_from("METHOD"), Err(HttpError::InvalidMethod));
}
}