#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum HttpStatus {
Continue = 100,
SwitchingProtocols = 101,
Processing = 102,
EarlyHints = 103,
OK = 200,
Created = 201,
Accepted = 202,
NonAuthoritativeInformation = 203,
NoContent = 204,
ResetContent = 205,
PartialContent = 206,
MultiStatus = 207,
AlreadyReported = 208,
ImUsed = 226,
MultipleChoices = 300,
MovedPermanently = 301,
Found = 302,
SeeOther = 303,
NotModified = 304,
UseProxy = 305,
TemporaryRedirect = 307,
PermanentRedirect = 308,
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,
InternalServerError = 500,
NotImplemented = 501,
BadGateway = 502,
ServiceUnavailable = 503,
GatewayTimeout = 504,
HttpVersionNotSupported = 505,
VariantAlsoNegotiates = 506,
InsufficientStorage = 507,
LoopDetected = 508,
NotExtended = 510,
NetworkAuthenticationRequired = 511,
}
impl HttpStatus {
pub fn reason(code: u16) -> &'static str {
match code {
100 => "Continue",
101 => "Switching Protocols",
102 => "Processing",
103 => "Early Hints",
200 => "OK",
201 => "Created",
202 => "Accepted",
203 => "Non-Authoritative Information",
204 => "No Content",
205 => "Reset Content",
206 => "Partial Content",
207 => "Multi-Status",
208 => "Already Reported",
226 => "IM Used",
300 => "Multiple Choices",
301 => "Moved Permanently",
302 => "Found",
303 => "See Other",
304 => "Not Modified",
305 => "Use Proxy",
307 => "Temporary Redirect",
308 => "Permanent Redirect",
400 => "Bad Request",
401 => "Unauthorized",
402 => "Payment Required",
403 => "Forbidden",
404 => "Not Found",
405 => "Method Not Allowed",
406 => "Not Acceptable",
407 => "Proxy Authentication Required",
408 => "Request Timeout",
409 => "Conflict",
410 => "Gone",
411 => "Length Required",
412 => "Precondition Failed",
413 => "Payload Too Large",
414 => "URI Too Long",
415 => "Unsupported Media Type",
416 => "Range Not Satisfiable",
417 => "Expectation Failed",
418 => "I'm a Teapot",
421 => "Misdirected Request",
422 => "Unprocessable Entity",
423 => "Locked",
424 => "Failed Dependency",
425 => "Too Early",
426 => "Upgrade Required",
428 => "Precondition Required",
429 => "Too Many Requests",
431 => "Request Header Fields Too Large",
451 => "Unavailable For Legal Reasons",
500 => "Internal Server Error",
501 => "Not Implemented",
502 => "Bad Gateway",
503 => "Service Unavailable",
504 => "Gateway Timeout",
505 => "HTTP Version Not Supported",
506 => "Variant Also Negotiates",
507 => "Insufficient Storage",
508 => "Loop Detected",
510 => "Not Extended",
511 => "Network Authentication Required",
_ => "",
}
}
pub fn code(&self) -> u16 {
*self as u16
}
}
pub enum StatusArg<'a> {
Enum(HttpStatus),
Code(u16),
CodeReason(u16, &'a str),
}
impl<'a> From<HttpStatus> for StatusArg<'a> {
fn from(s: HttpStatus) -> Self {
StatusArg::Enum(s)
}
}
impl<'a> From<u16> for StatusArg<'a> {
fn from(code: u16) -> Self {
StatusArg::Code(code)
}
}
impl<'a> From<(u16, &'a str)> for StatusArg<'a> {
fn from(tuple: (u16, &'a str)) -> Self {
StatusArg::CodeReason(tuple.0, tuple.1)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_http_status_codes() {
assert_eq!(HttpStatus::OK.code(), 200);
assert_eq!(HttpStatus::NotFound.code(), 404);
assert_eq!(HttpStatus::InternalServerError.code(), 500);
}
#[test]
fn test_http_status_reason_lookup() {
assert_eq!(HttpStatus::reason(200), "OK");
assert_eq!(HttpStatus::reason(404), "Not Found");
assert_eq!(HttpStatus::reason(418), "I'm a Teapot");
assert_ne!(HttpStatus::reason(418), "I'm a teapot");
}
#[test]
fn test_status_arg_from() {
let a: StatusArg = HttpStatus::OK.into();
match a {
StatusArg::Enum(HttpStatus::OK) => (),
_ => panic!("Expected StatusArg::Enum(HttpStatus::OK)"),
}
let b: StatusArg = 404.into();
match b {
StatusArg::Code(404) => (),
_ => panic!("Expected StatusArg::Code(404)"),
}
let c: StatusArg = (599, "Timeout").into();
match c {
StatusArg::CodeReason(599, "Timeout") => (),
_ => panic!("Expected StatusArg::CodeReason(599, \"Timeout\")"),
}
}
}