1#[derive(Debug, PartialEq, Clone)]
3pub enum StatusCode {
4 Continue,
6 SwitchingProtocols,
9 OK,
11 Created,
13 Accepted,
16 NonAuthoritativeInformation,
19 NoContent,
21 ResetContent,
24 PartialContent,
26 MultipleChoices,
28 MovedPermanently,
30 Found,
33 SeeOther,
36 NotModified,
39 UseProxy,
41 TemporaryRedirect,
44 BadRequest,
46 Unauthorized,
49 PaymentRequired,
51 Forbidden,
53 NotFound,
55 MethodNotAllowed,
58 NotAcceptable,
60 ProxyAuthenticationRequired,
63 RequestTimeOut,
66 Conflict,
69 Gone,
71 LengthRequired,
74 PreconditionFailed,
76 RequestEntityTooLarge,
78 RequestURITooLarge,
80 UnsupportedMediaType,
82 RequestedRangeNotSatisfiable,
84 ExpectationFailed,
86 ImATeapot,
89 InternalServerError,
92 NotImplemented,
94 BadGateway,
97 ServiceUnavailable,
99 GatewayTimeout,
101 HTTPVersionNotSupported,
103}
104
105impl StatusCode {
106 pub fn parse(raw: &str) -> Option<Self> {
108 if raw.len() < 3 {
109 return None;
110 }
111
112 let key = &raw[0..3];
113
114 match key {
115 "100" => Some(StatusCode::Continue),
116 "101" => Some(StatusCode::SwitchingProtocols),
117 "200" => Some(StatusCode::OK),
118 "201" => Some(StatusCode::Created),
119 "202" => Some(StatusCode::Accepted),
120 "203" => Some(StatusCode::NonAuthoritativeInformation),
121 "204" => Some(StatusCode::NoContent),
122 "205" => Some(StatusCode::ResetContent),
123 "206" => Some(StatusCode::PartialContent),
124 "300" => Some(StatusCode::MultipleChoices),
125 "301" => Some(StatusCode::MovedPermanently),
126 "302" => Some(StatusCode::Found),
127 "303" => Some(StatusCode::SeeOther),
128 "304" => Some(StatusCode::NotModified),
129 "305" => Some(StatusCode::UseProxy),
130 "307" => Some(StatusCode::TemporaryRedirect),
131 "400" => Some(StatusCode::BadRequest),
132 "401" => Some(StatusCode::Unauthorized),
133 "402" => Some(StatusCode::PaymentRequired),
134 "403" => Some(StatusCode::Forbidden),
135 "404" => Some(StatusCode::NotFound),
136 "405" => Some(StatusCode::MethodNotAllowed),
137 "406" => Some(StatusCode::NotAcceptable),
138 "407" => Some(StatusCode::ProxyAuthenticationRequired),
139 "408" => Some(StatusCode::RequestTimeOut),
140 "409" => Some(StatusCode::Conflict),
141 "410" => Some(StatusCode::Gone),
142 "411" => Some(StatusCode::LengthRequired),
143 "412" => Some(StatusCode::PreconditionFailed),
144 "413" => Some(StatusCode::RequestEntityTooLarge),
145 "414" => Some(StatusCode::RequestURITooLarge),
146 "415" => Some(StatusCode::UnsupportedMediaType),
147 "416" => Some(StatusCode::RequestedRangeNotSatisfiable),
148 "417" => Some(StatusCode::ExpectationFailed),
149 "418" => Some(StatusCode::ImATeapot),
150 "500" => Some(StatusCode::InternalServerError),
151 "501" => Some(StatusCode::NotImplemented),
152 "502" => Some(StatusCode::BadGateway),
153 "503" => Some(StatusCode::ServiceUnavailable),
154 "504" => Some(StatusCode::GatewayTimeout),
155 "505" => Some(StatusCode::HTTPVersionNotSupported),
156 _ => None,
157 }
158 }
159
160 pub fn serialize(&self) -> &'static str {
162 match *self {
163 Self::Continue => "100 Continue",
164 Self::SwitchingProtocols => "101 Switching Protocols",
165 Self::OK => "200 OK",
166 Self::Created => "201 Created",
167 Self::Accepted => "202 Accepted",
168 Self::NonAuthoritativeInformation => "203 Non-Authoritative Information",
169 Self::NoContent => "204 No Content",
170 Self::ResetContent => "205 Reset Content",
171 Self::PartialContent => "206 Partial Content",
172 Self::MultipleChoices => "300 Multiple Choices",
173 Self::MovedPermanently => "301 Moved Permanently",
174 Self::Found => "302 Found",
175 Self::SeeOther => "303 See Other",
176 Self::NotModified => "304 Not Modified",
177 Self::UseProxy => "305 Use Proxy",
178 Self::TemporaryRedirect => "307 Temporary Redirect",
179 Self::BadRequest => "400 Bad Request",
180 Self::Unauthorized => "401 Unauthorized",
181 Self::PaymentRequired => "402 Payment Required",
182 Self::Forbidden => "403 Forbidden",
183 Self::NotFound => "404 Not Found",
184 Self::MethodNotAllowed => "405 Method Not Allowed",
185 Self::NotAcceptable => "406 Not Acceptable",
186 Self::ProxyAuthenticationRequired => "407 Proxy Authentication Required",
187 Self::RequestTimeOut => "408 Request Time-out",
188 Self::Conflict => "409 Conflict",
189 Self::Gone => "410 Gone",
190 Self::LengthRequired => "411 Length Required",
191 Self::PreconditionFailed => "412 Precondition Failed",
192 Self::RequestEntityTooLarge => "413 Request Entity Too Large",
193 Self::RequestURITooLarge => "414 Request-URI Too Large",
194 Self::UnsupportedMediaType => "415 Unsupported Media Type",
195 Self::RequestedRangeNotSatisfiable => "416 Requested Range Not Satisfiable",
196 Self::ExpectationFailed => "417 Expectation Failed",
197 Self::ImATeapot => "418 I'm a Teapot",
198 Self::InternalServerError => "500 Internal Server Error",
199 Self::NotImplemented => "501 Not Implemented",
200 Self::BadGateway => "502 Bad Gateway",
201 Self::ServiceUnavailable => "503 Service Unavailable",
202 Self::GatewayTimeout => "504 Gateway Time-out",
203 Self::HTTPVersionNotSupported => "505 HTTP Version Not Supported",
204 }
205 }
206}
207
208#[cfg(feature = "wasm_serialize")]
209impl StatusCode {
210 pub fn wasm_deserialize(key: i32) -> Option<Self> {
213 match key {
214 100 => Some(StatusCode::Continue),
215 101 => Some(StatusCode::SwitchingProtocols),
216 200 => Some(StatusCode::OK),
217 201 => Some(StatusCode::Created),
218 202 => Some(StatusCode::Accepted),
219 203 => Some(StatusCode::NonAuthoritativeInformation),
220 204 => Some(StatusCode::NoContent),
221 205 => Some(StatusCode::ResetContent),
222 206 => Some(StatusCode::PartialContent),
223 300 => Some(StatusCode::MultipleChoices),
224 301 => Some(StatusCode::MovedPermanently),
225 302 => Some(StatusCode::Found),
226 303 => Some(StatusCode::SeeOther),
227 304 => Some(StatusCode::NotModified),
228 305 => Some(StatusCode::UseProxy),
229 307 => Some(StatusCode::TemporaryRedirect),
230 400 => Some(StatusCode::BadRequest),
231 401 => Some(StatusCode::Unauthorized),
232 402 => Some(StatusCode::PaymentRequired),
233 403 => Some(StatusCode::Forbidden),
234 404 => Some(StatusCode::NotFound),
235 405 => Some(StatusCode::MethodNotAllowed),
236 406 => Some(StatusCode::NotAcceptable),
237 407 => Some(StatusCode::ProxyAuthenticationRequired),
238 408 => Some(StatusCode::RequestTimeOut),
239 409 => Some(StatusCode::Conflict),
240 410 => Some(StatusCode::Gone),
241 411 => Some(StatusCode::LengthRequired),
242 412 => Some(StatusCode::PreconditionFailed),
243 413 => Some(StatusCode::RequestEntityTooLarge),
244 414 => Some(StatusCode::RequestURITooLarge),
245 415 => Some(StatusCode::UnsupportedMediaType),
246 416 => Some(StatusCode::RequestedRangeNotSatisfiable),
247 417 => Some(StatusCode::ExpectationFailed),
248 418 => Some(StatusCode::ImATeapot),
249 500 => Some(StatusCode::InternalServerError),
250 501 => Some(StatusCode::NotImplemented),
251 502 => Some(StatusCode::BadGateway),
252 503 => Some(StatusCode::ServiceUnavailable),
253 504 => Some(StatusCode::GatewayTimeout),
254 505 => Some(StatusCode::HTTPVersionNotSupported),
255 _ => None,
256 }
257 }
258
259 pub fn wasm_serialize(&self) -> i32 {
263 match *self {
264 Self::Continue => 100,
265 Self::SwitchingProtocols => 101,
266 Self::OK => 200,
267 Self::Created => 201,
268 Self::Accepted => 202,
269 Self::NonAuthoritativeInformation => 203,
270 Self::NoContent => 204,
271 Self::ResetContent => 205,
272 Self::PartialContent => 206,
273 Self::MultipleChoices => 300,
274 Self::MovedPermanently => 301,
275 Self::Found => 302,
276 Self::SeeOther => 303,
277 Self::NotModified => 304,
278 Self::UseProxy => 305,
279 Self::TemporaryRedirect => 307,
280 Self::BadRequest => 400,
281 Self::Unauthorized => 401,
282 Self::PaymentRequired => 402,
283 Self::Forbidden => 403,
284 Self::NotFound => 404,
285 Self::MethodNotAllowed => 405,
286 Self::NotAcceptable => 406,
287 Self::ProxyAuthenticationRequired => 407,
288 Self::RequestTimeOut => 408,
289 Self::Conflict => 409,
290 Self::Gone => 410,
291 Self::LengthRequired => 411,
292 Self::PreconditionFailed => 412,
293 Self::RequestEntityTooLarge => 413,
294 Self::RequestURITooLarge => 414,
295 Self::UnsupportedMediaType => 415,
296 Self::RequestedRangeNotSatisfiable => 416,
297 Self::ExpectationFailed => 417,
298 Self::ImATeapot => 418,
299 Self::InternalServerError => 500,
300 Self::NotImplemented => 501,
301 Self::BadGateway => 502,
302 Self::ServiceUnavailable => 503,
303 Self::GatewayTimeout => 504,
304 Self::HTTPVersionNotSupported => 505,
305 }
306 }
307}
308
309#[cfg(test)]
310mod tests {
311 use super::*;
312
313 #[test]
314 fn parse_invalid() {
315 assert_eq!(None, StatusCode::parse("1"));
316 assert_eq!(None, StatusCode::parse("123"));
317 }
318
319 #[test]
320 fn parse_all() {
321 assert_eq!(Some(StatusCode::Continue), StatusCode::parse("100"));
322 assert_eq!(
323 Some(StatusCode::SwitchingProtocols),
324 StatusCode::parse("101")
325 );
326 assert_eq!(Some(StatusCode::OK), StatusCode::parse("200"));
327 assert_eq!(Some(StatusCode::Created), StatusCode::parse("201"));
328 assert_eq!(Some(StatusCode::Accepted), StatusCode::parse("202"));
329 assert_eq!(
330 Some(StatusCode::NonAuthoritativeInformation),
331 StatusCode::parse("203")
332 );
333 assert_eq!(Some(StatusCode::NoContent), StatusCode::parse("204"));
334 assert_eq!(Some(StatusCode::ResetContent), StatusCode::parse("205"));
335 assert_eq!(Some(StatusCode::PartialContent), StatusCode::parse("206"));
336 assert_eq!(Some(StatusCode::MultipleChoices), StatusCode::parse("300"));
337 assert_eq!(Some(StatusCode::MovedPermanently), StatusCode::parse("301"));
338 assert_eq!(Some(StatusCode::Found), StatusCode::parse("302"));
339 assert_eq!(Some(StatusCode::SeeOther), StatusCode::parse("303"));
340 assert_eq!(Some(StatusCode::NotModified), StatusCode::parse("304"));
341 assert_eq!(Some(StatusCode::UseProxy), StatusCode::parse("305"));
342 assert_eq!(
343 Some(StatusCode::TemporaryRedirect),
344 StatusCode::parse("307")
345 );
346 assert_eq!(Some(StatusCode::BadRequest), StatusCode::parse("400"));
347 assert_eq!(Some(StatusCode::Unauthorized), StatusCode::parse("401"));
348 assert_eq!(Some(StatusCode::PaymentRequired), StatusCode::parse("402"));
349 assert_eq!(Some(StatusCode::Forbidden), StatusCode::parse("403"));
350 assert_eq!(Some(StatusCode::NotFound), StatusCode::parse("404"));
351 assert_eq!(Some(StatusCode::MethodNotAllowed), StatusCode::parse("405"));
352 assert_eq!(Some(StatusCode::NotAcceptable), StatusCode::parse("406"));
353 assert_eq!(
354 Some(StatusCode::ProxyAuthenticationRequired),
355 StatusCode::parse("407")
356 );
357 assert_eq!(Some(StatusCode::RequestTimeOut), StatusCode::parse("408"));
358 assert_eq!(Some(StatusCode::Conflict), StatusCode::parse("409"));
359 assert_eq!(Some(StatusCode::Gone), StatusCode::parse("410"));
360 assert_eq!(Some(StatusCode::LengthRequired), StatusCode::parse("411"));
361 assert_eq!(
362 Some(StatusCode::PreconditionFailed),
363 StatusCode::parse("412")
364 );
365 assert_eq!(
366 Some(StatusCode::RequestEntityTooLarge),
367 StatusCode::parse("413")
368 );
369 assert_eq!(
370 Some(StatusCode::RequestURITooLarge),
371 StatusCode::parse("414")
372 );
373 assert_eq!(
374 Some(StatusCode::UnsupportedMediaType),
375 StatusCode::parse("415")
376 );
377 assert_eq!(
378 Some(StatusCode::RequestedRangeNotSatisfiable),
379 StatusCode::parse("416")
380 );
381 assert_eq!(
382 Some(StatusCode::ExpectationFailed),
383 StatusCode::parse("417")
384 );
385 assert_eq!(Some(StatusCode::ImATeapot), StatusCode::parse("418"));
386 assert_eq!(
387 Some(StatusCode::InternalServerError),
388 StatusCode::parse("500")
389 );
390 assert_eq!(Some(StatusCode::NotImplemented), StatusCode::parse("501"));
391 assert_eq!(Some(StatusCode::BadGateway), StatusCode::parse("502"));
392 assert_eq!(
393 Some(StatusCode::ServiceUnavailable),
394 StatusCode::parse("503")
395 );
396 assert_eq!(Some(StatusCode::GatewayTimeout), StatusCode::parse("504"));
397 assert_eq!(
398 Some(StatusCode::HTTPVersionNotSupported),
399 StatusCode::parse("505")
400 );
401 }
402
403 #[test]
404 fn serialize() {
405 assert_eq!("100 Continue".to_owned(), StatusCode::Continue.serialize());
406 assert_eq!(
407 "101 Switching Protocols".to_owned(),
408 StatusCode::SwitchingProtocols.serialize()
409 );
410 assert_eq!("200 OK".to_owned(), StatusCode::OK.serialize());
411 assert_eq!("201 Created".to_owned(), StatusCode::Created.serialize());
412 assert_eq!("202 Accepted".to_owned(), StatusCode::Accepted.serialize());
413 assert_eq!(
414 "203 Non-Authoritative Information".to_owned(),
415 StatusCode::NonAuthoritativeInformation.serialize()
416 );
417 assert_eq!(
418 "204 No Content".to_owned(),
419 StatusCode::NoContent.serialize()
420 );
421 assert_eq!(
422 "205 Reset Content".to_owned(),
423 StatusCode::ResetContent.serialize()
424 );
425 assert_eq!(
426 "206 Partial Content".to_owned(),
427 StatusCode::PartialContent.serialize()
428 );
429
430 assert_eq!(
431 "300 Multiple Choices".to_owned(),
432 StatusCode::MultipleChoices.serialize()
433 );
434 assert_eq!(
435 "301 Moved Permanently".to_owned(),
436 StatusCode::MovedPermanently.serialize()
437 );
438 assert_eq!("302 Found".to_owned(), StatusCode::Found.serialize());
439 assert_eq!("303 See Other".to_owned(), StatusCode::SeeOther.serialize());
440 assert_eq!(
441 "304 Not Modified".to_owned(),
442 StatusCode::NotModified.serialize()
443 );
444 assert_eq!("305 Use Proxy".to_owned(), StatusCode::UseProxy.serialize());
445 assert_eq!(
446 "307 Temporary Redirect".to_owned(),
447 StatusCode::TemporaryRedirect.serialize()
448 );
449
450 assert_eq!(
451 "400 Bad Request".to_owned(),
452 StatusCode::BadRequest.serialize()
453 );
454 assert_eq!(
455 "401 Unauthorized".to_owned(),
456 StatusCode::Unauthorized.serialize()
457 );
458 assert_eq!(
459 "402 Payment Required".to_owned(),
460 StatusCode::PaymentRequired.serialize()
461 );
462 assert_eq!(
463 "403 Forbidden".to_owned(),
464 StatusCode::Forbidden.serialize()
465 );
466 assert_eq!("404 Not Found".to_owned(), StatusCode::NotFound.serialize());
467 assert_eq!(
468 "405 Method Not Allowed".to_owned(),
469 StatusCode::MethodNotAllowed.serialize()
470 );
471 assert_eq!(
472 "406 Not Acceptable".to_owned(),
473 StatusCode::NotAcceptable.serialize()
474 );
475 assert_eq!(
476 "407 Proxy Authentication Required".to_owned(),
477 StatusCode::ProxyAuthenticationRequired.serialize()
478 );
479 assert_eq!(
480 "408 Request Time-out".to_owned(),
481 StatusCode::RequestTimeOut.serialize()
482 );
483 assert_eq!("409 Conflict".to_owned(), StatusCode::Conflict.serialize());
484 assert_eq!("410 Gone".to_owned(), StatusCode::Gone.serialize());
485 assert_eq!(
486 "411 Length Required".to_owned(),
487 StatusCode::LengthRequired.serialize()
488 );
489 assert_eq!(
490 "412 Precondition Failed".to_owned(),
491 StatusCode::PreconditionFailed.serialize()
492 );
493 assert_eq!(
494 "413 Request Entity Too Large".to_owned(),
495 StatusCode::RequestEntityTooLarge.serialize()
496 );
497 assert_eq!(
498 "414 Request-URI Too Large".to_owned(),
499 StatusCode::RequestURITooLarge.serialize()
500 );
501 assert_eq!(
502 "415 Unsupported Media Type".to_owned(),
503 StatusCode::UnsupportedMediaType.serialize()
504 );
505 assert_eq!(
506 "416 Requested Range Not Satisfiable".to_owned(),
507 StatusCode::RequestedRangeNotSatisfiable.serialize()
508 );
509 assert_eq!(
510 "417 Expectation Failed".to_owned(),
511 StatusCode::ExpectationFailed.serialize()
512 );
513 assert_eq!(
514 "418 I'm a Teapot".to_owned(),
515 StatusCode::ImATeapot.serialize()
516 );
517
518 assert_eq!(
519 "500 Internal Server Error".to_owned(),
520 StatusCode::InternalServerError.serialize()
521 );
522 assert_eq!(
523 "501 Not Implemented".to_owned(),
524 StatusCode::NotImplemented.serialize()
525 );
526 assert_eq!(
527 "502 Bad Gateway".to_owned(),
528 StatusCode::BadGateway.serialize()
529 );
530 assert_eq!(
531 "503 Service Unavailable".to_owned(),
532 StatusCode::ServiceUnavailable.serialize()
533 );
534 assert_eq!(
535 "504 Gateway Time-out".to_owned(),
536 StatusCode::GatewayTimeout.serialize()
537 );
538 assert_eq!(
539 "505 HTTP Version Not Supported".to_owned(),
540 StatusCode::HTTPVersionNotSupported.serialize()
541 );
542 }
543}