oxedyne/fe2o3/fe2o3_net/src/http/status.rs
8.1 KiB, 10 runs
created by r1870400018:585, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | use oxedyne_fe2o3_core::prelude::*; |
| 2 | |
| 3 | use std::{ |
| 4 | fmt, |
| 5 | str::FromStr, |
| 6 | }; |
| 7 | |
| 8 | use strum::FromRepr; |
| 9 | |
| 10 | /// Ref: [IANA](https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml) |
| 11 | #[derive(Clone, Copy, Debug, Eq, FromRepr, PartialEq)] |
| 12 | #[repr(u16)] |
| 13 | pub enum HttpStatus { |
| 14 | Continue = 100, |
| 15 | SwitchingProtocols = 101, |
| 16 | Processing = 102, |
| 17 | EarlyHints = 103, |
| 18 | OK = 200, |
| 19 | Created = 201, |
| 20 | Accepted = 202, |
| 21 | NonAuthoritativeInformation = 203, |
| 22 | NoContent = 204, |
| 23 | ResetContent = 205, |
| 24 | PartialContent = 206, |
| 25 | MultiStatus = 207, |
| 26 | AlreadyReported = 208, |
| 27 | IMUsed = 226, |
| 28 | MultipleChoices = 300, |
| 29 | MovedPermanently = 301, |
| 30 | Found = 302, |
| 31 | SeeOther = 303, |
| 32 | NotModified = 304, |
| 33 | UseProxy = 305, |
| 34 | TemporaryRedirect = 307, |
| 35 | PermanentRedirect = 308, |
| 36 | BadRequest = 400, |
| 37 | Unauthorized = 401, |
| 38 | PaymentRequired = 402, |
| 39 | Forbidden = 403, |
| 40 | NotFound = 404, |
| 41 | MethodNotAllowed = 405, |
| 42 | NotAcceptable = 406, |
| 43 | ProxyAuthenticationRequired = 407, |
| 44 | RequestTimeout = 408, |
| 45 | Conflict = 409, |
| 46 | Gone = 410, |
| 47 | LengthRequired = 411, |
| 48 | PreconditionFailed = 412, |
| 49 | ContentTooLarge = 413, |
| 50 | URITooLong = 414, |
| 51 | UnsupportedMediaType = 415, |
| 52 | RangeNotSatisfiable = 416, |
| 53 | ExpectationFailed = 417, |
| 54 | MisdirectedRequest = 421, |
| 55 | UnprocessableContent = 422, |
| 56 | Locked = 423, |
| 57 | FailedDependency = 424, |
| 58 | TooEarly = 425, |
| 59 | UpgradeRequired = 426, |
| 60 | PreconditionRequired = 428, |
| 61 | TooManyRequests = 429, |
| 62 | RequestHeaderFieldsTooLarge = 431, |
| 63 | UnavailableForLegalReasons = 451, |
| 64 | InternalServerError = 500, |
| 65 | NotImplemented = 501, |
| 66 | BadGateway = 502, |
| 67 | ServiceUnavailable = 503, |
| 68 | GatewayTimeout = 504, |
| 69 | HTTPVersionNotSupported = 505, |
| 70 | VariantAlsoNegotiates = 506, |
| 71 | InsufficientStorage = 507, |
| 72 | LoopDetected = 508, |
| 73 | NotExtended = 510, |
| 74 | NetworkAuthenticationRequired = 511, |
| 75 | } |
| 76 | |
| 77 | impl fmt::Display for HttpStatus { |
| 78 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 79 | write!(f, "{}", (*self as u16).to_string()) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | impl FromStr for HttpStatus { |
| 84 | type Err = Error<ErrTag>; |
| 85 | |
| 86 | fn from_str(s: &str) -> std::result::Result<Self, Self::Err> { |
| 87 | match s.parse::<u16>() { |
| 88 | Ok(n) => match Self::from_repr(n) { |
| 89 | Some(status) => Ok(status), |
| 90 | None => Err(err!( |
| 91 | "Unrecognised HTTP status code '{}'.", s; |
| 92 | IO, Network, Unknown, Input)), |
| 93 | }, |
| 94 | Err(e) => Err(err!(e, "Code '{}' is not a u16.", s; IO, Network, Mismatch, Input)) |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | impl HttpStatus { |
| 100 | |
| 101 | pub fn desc(&self) -> &'static str { |
| 102 | match self { |
| 103 | Self::Continue => "Continue", |
| 104 | Self::SwitchingProtocols => "Switching Protocols", |
| 105 | Self::Processing => "Processing", |
| 106 | Self::EarlyHints => "Early Hints", |
| 107 | Self::OK => "OK", |
| 108 | Self::Created => "Created", |
| 109 | Self::Accepted => "Accepted", |
| 110 | Self::NonAuthoritativeInformation => "Non Authoritative Information", |
| 111 | Self::NoContent => "No Content", |
| 112 | Self::ResetContent => "Reset Content", |
| 113 | Self::PartialContent => "Partial Content", |
| 114 | Self::MultiStatus => "MultiStatus", |
| 115 | Self::AlreadyReported => "Already Reported", |
| 116 | Self::IMUsed => "IM Used", |
| 117 | Self::MultipleChoices => "MultipleChoices", |
| 118 | Self::MovedPermanently => "MovedPermanently", |
| 119 | Self::Found => "Found", |
| 120 | Self::SeeOther => "SeeOther", |
| 121 | Self::NotModified => "NotModified", |
| 122 | Self::UseProxy => "UseProxy", |
| 123 | Self::TemporaryRedirect => "Temporary Redirect", |
| 124 | Self::PermanentRedirect => "Permanent Redirect", |
| 125 | Self::BadRequest => "Bad Request", |
| 126 | Self::Unauthorized => "Unauthorized", |
| 127 | Self::PaymentRequired => "Payment Required", |
| 128 | Self::Forbidden => "Forbidden", |
| 129 | Self::NotFound => "Not Found", |
| 130 | Self::MethodNotAllowed => "Method Not Allowed", |
| 131 | Self::NotAcceptable => "Not Acceptable", |
| 132 | Self::ProxyAuthenticationRequired => "Proxy Authentication Required", |
| 133 | Self::RequestTimeout => "Request Timeout", |
| 134 | Self::Conflict => "Conflict", |
| 135 | Self::Gone => "Gone", |
| 136 | Self::LengthRequired => "Length Required", |
| 137 | Self::PreconditionFailed => "Precondition Failed", |
| 138 | Self::ContentTooLarge => "Content Too Large", |
| 139 | Self::URITooLong => "URI Too Long", |
| 140 | Self::UnsupportedMediaType => "Unsupported Media Type", |
| 141 | Self::RangeNotSatisfiable => "Range Not Satisfiable", |
| 142 | Self::ExpectationFailed => "Expectation Failed", |
| 143 | Self::MisdirectedRequest => "Misdirected Request", |
| 144 | Self::UnprocessableContent => "Unprocessable Content", |
| 145 | Self::Locked => "Locked", |
| 146 | Self::FailedDependency => "Failed Dependency", |
| 147 | Self::TooEarly => "Too Early", |
| 148 | Self::UpgradeRequired => "Upgrade Required", |
| 149 | Self::PreconditionRequired => "Precondition Required", |
| 150 | Self::TooManyRequests => "Too Many Requests", |
| 151 | Self::RequestHeaderFieldsTooLarge => "Request Header Fields Too Large", |
| 152 | Self::UnavailableForLegalReasons => "Unavailable For Legal Reasons", |
| 153 | Self::InternalServerError => "Internal Server Error", |
| 154 | Self::NotImplemented => "Not Implemented", |
| 155 | Self::BadGateway => "Bad Gateway", |
| 156 | Self::ServiceUnavailable => "Service Unavailable", |
| 157 | Self::GatewayTimeout => "Gateway Timeout", |
| 158 | Self::HTTPVersionNotSupported => "HTTP Version Not Supported", |
| 159 | Self::VariantAlsoNegotiates => "Variant Also Negotiates", |
| 160 | Self::InsufficientStorage => "Insufficient Storage", |
| 161 | Self::LoopDetected => "Loop Detected", |
| 162 | Self::NotExtended => "Not Extended", |
| 163 | Self::NetworkAuthenticationRequired => "Network Authentication Required", |
| 164 | } |
| 165 | } |
| 166 | } |