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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
use itertools::Itertools;
use std::collections::HashMap;
pub enum StatusCode {
// 1xx Informational
Continue, // 100
SwitchingProtocols, // 101
Processing, // 102
// 2xx Success
Ok, // 200
Created, // 201
Accepted, // 202
NonAuthoritativeInformation, // 203
NoContent, // 204
ResetContent, // 205
PartialContent, // 206
MultiStatus, // 207
AlreadyReported, // 208
ImUsed, // 226
// 3xx Redirection
MultipleChoices, // 300
MovedPermanently, // 301
Found, // 302
SeeOther, // 303
NotModified, // 304
UseProxy, // 305
TemporaryRedirect, // 307
PermanentRedirect, // 308
// 4xx Client Error
BadRequest, // 400
Unauthorized, // 401
PaymentRequired, // 402
Forbidden, // 403
NotFound, // 404
MethodNotAllowed, // 405
NotAcceptable, // 406
ProxyAuthenticationRequired, // 407
RequestTimeout, // 408
Conflict, // 409
Gone, // 410
LengthRequired, // 411
PreconditionFailed, // 412
PayloadTooLarge, // 413
UriTooLong, // 414
UnsupportedMediaType, // 415
RangeNotSatisfiable, // 416
ExpectationFailed, // 417
ImaTeapot, // 418
MisdirectedRequest, // 421
UnprocessableEntity, // 422
Locked, // 423
FailedDependency, // 424
TooEarly, // 425
UpgradeRequired, // 426
PreconditionRequired, // 428
TooManyRequests, // 429
RequestHeaderFieldsTooLarge, // 431
UnavailableForLegalReasons, // 451
// 5xx Server Error
InternalServerError, // 500
NotImplemented, // 501
BadGateway, // 502
ServiceUnavailable, // 503
GatewayTimeout, // 504
HttpVersionNotSupported, // 505
VariantAlsoNegotiates, // 506
InsufficientStorage, // 507
LoopDetected, // 508
NotExtended, // 510
NetworkAuthenticationRequired, // 511
}
type Route = String;
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub enum Method {
GET,
POST,
PUT,
}
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct Endpoint {
pub method: Method,
pub route: Route,
}
impl Endpoint {
fn get(route: &str) -> Self {
Self {
method: Method::GET,
route: route.to_string(),
}
}
fn post(route: &str) -> Self {
Self {
method: Method::POST,
route: route.to_string(),
}
}
fn put(route: &str) -> Self {
Self {
method: Method::PUT,
route: route.to_string(),
}
}
}
pub fn get(route: &str) -> Endpoint {
Endpoint::get(route)
}
pub fn post(route: &str) -> Endpoint {
Endpoint::post(route)
}
pub fn put(route: &str) -> Endpoint {
Endpoint::put(route)
}
impl From<StatusCode> for String {
fn from(val: StatusCode) -> Self {
use StatusCode::*;
match val {
// 1xx Informational
Continue => "100 Continue".to_string(),
SwitchingProtocols => "101 Switching Protocols".to_string(),
Processing => "102 Processing".to_string(),
// 2xx Success
Ok => "200 OK".to_string(),
Created => "201 Created".to_string(),
Accepted => "202 Accepted".to_string(),
NonAuthoritativeInformation => "203 Non-Authoritative Information".to_string(),
NoContent => "204 No Content".to_string(),
ResetContent => "205 Reset Content".to_string(),
PartialContent => "206 Partial Content".to_string(),
MultiStatus => "207 Multi-Status".to_string(),
AlreadyReported => "208 Already Reported".to_string(),
ImUsed => "226 IM Used".to_string(),
// 3xx Redirection
MultipleChoices => "300 Multiple Choices".to_string(),
MovedPermanently => "301 Moved Permanently".to_string(),
Found => "302 Found".to_string(),
SeeOther => "303 See Other".to_string(),
NotModified => "304 Not Modified".to_string(),
UseProxy => "305 Use Proxy".to_string(),
TemporaryRedirect => "307 Temporary Redirect".to_string(),
PermanentRedirect => "308 Permanent Redirect".to_string(),
// 4xx Client Error
BadRequest => "400 Bad Request".to_string(),
Unauthorized => "401 Unauthorized".to_string(),
PaymentRequired => "402 Payment Required".to_string(),
Forbidden => "403 Forbidden".to_string(),
NotFound => "404 Not Found".to_string(),
MethodNotAllowed => "405 Method Not Allowed".to_string(),
NotAcceptable => "406 Not Acceptable".to_string(),
ProxyAuthenticationRequired => "407 Proxy Authentication Required".to_string(),
RequestTimeout => "408 Request Timeout".to_string(),
Conflict => "409 Conflict".to_string(),
Gone => "410 Gone".to_string(),
LengthRequired => "411 Length Required".to_string(),
PreconditionFailed => "412 Precondition Failed".to_string(),
PayloadTooLarge => "413 Payload Too Large".to_string(),
UriTooLong => "414 URI Too Long".to_string(),
UnsupportedMediaType => "415 Unsupported Media Type".to_string(),
RangeNotSatisfiable => "416 Range Not Satisfiable".to_string(),
ExpectationFailed => "417 Expectation Failed".to_string(),
ImaTeapot => "418 I'm a teapot".to_string(),
MisdirectedRequest => "421 Misdirected Request".to_string(),
UnprocessableEntity => "422 Unprocessable Entity".to_string(),
Locked => "423 Locked".to_string(),
FailedDependency => "424 Failed Dependency".to_string(),
TooEarly => "425 Too Early".to_string(),
UpgradeRequired => "426 Upgrade Required".to_string(),
PreconditionRequired => "428 Precondition Required".to_string(),
TooManyRequests => "429 Too Many Requests".to_string(),
RequestHeaderFieldsTooLarge => "431 Request Header Fields Too Large".to_string(),
UnavailableForLegalReasons => "451 Unavailable For Legal Reasons".to_string(),
// 5xx Server Error
InternalServerError => "500 Internal Server Error".to_string(),
NotImplemented => "501 Not Implemented".to_string(),
BadGateway => "502 Bad Gateway".to_string(),
ServiceUnavailable => "503 Service Unavailable".to_string(),
GatewayTimeout => "504 Gateway Timeout".to_string(),
HttpVersionNotSupported => "505 HTTP Version Not Supported".to_string(),
VariantAlsoNegotiates => "506 Variant Also Negotiates".to_string(),
InsufficientStorage => "507 Insufficient Storage".to_string(),
LoopDetected => "508 Loop Detected".to_string(),
NotExtended => "510 Not Extended".to_string(),
NetworkAuthenticationRequired => "511 Network Authentication Required".to_string(),
}
}
}
impl From<Endpoint> for String {
fn from(val: Endpoint) -> Self {
use Method as M;
match (val.method, val.route) {
(M::GET, route) => "GET ".to_string() + &route,
(M::POST, route) => "GET ".to_string() + &route,
(M::PUT, route) => "GET ".to_string() + &route,
}
}
}
impl From<&Endpoint> for String {
fn from(val: &Endpoint) -> Self {
use Method as M;
match (&val.method, &val.route) {
(M::GET, route) => "GET ".to_string() + &route,
(M::POST, route) => "GET ".to_string() + &route,
(M::PUT, route) => "GET ".to_string() + &route,
}
}
}
#[derive(Debug, Clone)]
pub struct Headers(pub HashMap<String, String>);
impl From<Vec<String>> for Headers {
fn from(value: Vec<String>) -> Self {
let mut header_map = HashMap::new();
for header in value.iter().filter(|val| !val.is_empty()) {
let (key, val) = header
.split_once(": ")
.expect("Should be splitable by :<space>");
header_map.insert(key.to_string(), val.to_string());
}
Headers(header_map)
}
}
impl From<&[&str]> for Headers {
fn from(value: &[&str]) -> Self {
let mut header_map = HashMap::new();
for header in value.iter
|