#[derive(Clone, Debug)]
pub enum StatusCode {
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 core::fmt::Display for StatusCode {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f, "{} {}", self.clone() as u16, self.reason_phrase())
}
}
impl StatusCode {
fn reason_phrase(&self) -> &'static str {
match self.clone() as u16 {
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",
_ => "Unknown Status Code",
}
}
pub fn from_u16(code: u16) -> Option<StatusCode> {
match code {
100 => Some(StatusCode::Continue),
101 => Some(StatusCode::SwitchingProtocols),
102 => Some(StatusCode::Processing),
103 => Some(StatusCode::EarlyHints),
200 => Some(StatusCode::OK),
201 => Some(StatusCode::Created),
202 => Some(StatusCode::Accepted),
203 => Some(StatusCode::NonAuthoritativeInformation),
204 => Some(StatusCode::NoContent),
205 => Some(StatusCode::ResetContent),
206 => Some(StatusCode::PartialContent),
207 => Some(StatusCode::MultiStatus),
208 => Some(StatusCode::AlreadyReported),
226 => Some(StatusCode::IMUsed),
300 => Some(StatusCode::MultipleChoices),
301 => Some(StatusCode::MovedPermanently),
302 => Some(StatusCode::Found),
303 => Some(StatusCode::SeeOther),
304 => Some(StatusCode::NotModified),
305 => Some(StatusCode::UseProxy),
307 => Some(StatusCode::TemporaryRedirect),
308 => Some(StatusCode::PermanentRedirect),
400 => Some(StatusCode::BadRequest),
401 => Some(StatusCode::Unauthorized),
402 => Some(StatusCode::PaymentRequired),
403 => Some(StatusCode::Forbidden),
404 => Some(StatusCode::NotFound),
405 => Some(StatusCode::MethodNotAllowed),
406 => Some(StatusCode::NotAcceptable),
407 => Some(StatusCode::ProxyAuthenticationRequired),
408 => Some(StatusCode::RequestTimeout),
409 => Some(StatusCode::Conflict),
410 => Some(StatusCode::Gone),
411 => Some(StatusCode::LengthRequired),
412 => Some(StatusCode::PreconditionFailed),
413 => Some(StatusCode::PayloadTooLarge),
414 => Some(StatusCode::URITooLong),
415 => Some(StatusCode::UnsupportedMediaType),
416 => Some(StatusCode::RangeNotSatisfiable),
417 => Some(StatusCode::ExpectationFailed),
418 => Some(StatusCode::ImATeapot),
421 => Some(StatusCode::MisdirectedRequest),
422 => Some(StatusCode::UnprocessableEntity),
423 => Some(StatusCode::Locked),
424 => Some(StatusCode::FailedDependency),
425 => Some(StatusCode::TooEarly),
426 => Some(StatusCode::UpgradeRequired),
428 => Some(StatusCode::PreconditionRequired),
429 => Some(StatusCode::TooManyRequests),
431 => Some(StatusCode::RequestHeaderFieldsTooLarge),
451 => Some(StatusCode::UnavailableForLegalReasons),
500 => Some(StatusCode::InternalServerError),
501 => Some(StatusCode::NotImplemented),
502 => Some(StatusCode::BadGateway),
503 => Some(StatusCode::ServiceUnavailable),
504 => Some(StatusCode::GatewayTimeout),
505 => Some(StatusCode::HTTPVersionNotSupported),
506 => Some(StatusCode::VariantAlsoNegotiates),
507 => Some(StatusCode::InsufficientStorage),
508 => Some(StatusCode::LoopDetected),
510 => Some(StatusCode::NotExtended),
511 => Some(StatusCode::NetworkAuthenticationRequired),
_ => None,
}
}
}