1use crate::*;
2
3impl HttpStatus {
9 pub fn code(&self) -> ResponseStatusCode {
21 match self {
22 Self::Continue => 100,
23 Self::SwitchingProtocols => 101,
24 Self::Processing => 102,
25 Self::EarlyHints => 103,
26 Self::Ok => 200,
27 Self::Created => 201,
28 Self::Accepted => 202,
29 Self::NonAuthoritativeInformation => 203,
30 Self::NoContent => 204,
31 Self::ResetContent => 205,
32 Self::PartialContent => 206,
33 Self::MultiStatus => 207,
34 Self::AlreadyReported => 208,
35 Self::IMUsed => 226,
36 Self::MultipleChoices => 300,
37 Self::MovedPermanently => 301,
38 Self::Found => 302,
39 Self::SeeOther => 303,
40 Self::NotModified => 304,
41 Self::UseProxy => 305,
42 Self::TemporaryRedirect => 307,
43 Self::PermanentRedirect => 308,
44 Self::BadRequest => 400,
45 Self::Unauthorized => 401,
46 Self::PaymentRequired => 402,
47 Self::Forbidden => 403,
48 Self::NotFound => 404,
49 Self::MethodNotAllowed => 405,
50 Self::NotAcceptable => 406,
51 Self::ProxyAuthenticationRequired => 407,
52 Self::RequestTimeout => 408,
53 Self::Conflict => 409,
54 Self::Gone => 410,
55 Self::LengthRequired => 411,
56 Self::PreconditionFailed => 412,
57 Self::PayloadTooLarge => 413,
58 Self::URITooLong => 414,
59 Self::UnsupportedMediaType => 415,
60 Self::RangeNotSatisfiable => 416,
61 Self::ExpectationFailed => 417,
62 Self::ImATeapot => 418,
63 Self::MisdirectedRequest => 421,
64 Self::UnprocessableEntity => 422,
65 Self::Locked => 423,
66 Self::FailedDependency => 424,
67 Self::TooEarly => 425,
68 Self::UpgradeRequired => 426,
69 Self::PreconditionRequired => 428,
70 Self::TooManyRequests => 429,
71 Self::RequestHeaderFieldsTooLarge => 431,
72 Self::UnavailableForLegalReasons => 451,
73 Self::InternalServerError => 500,
74 Self::NotImplemented => 501,
75 Self::BadGateway => 502,
76 Self::ServiceUnavailable => 503,
77 Self::GatewayTimeout => 504,
78 Self::HTTPVersionNotSupported => 505,
79 Self::VariantAlsoNegotiates => 506,
80 Self::InsufficientStorage => 507,
81 Self::LoopDetected => 508,
82 Self::NotExtended => 510,
83 Self::NetworkAuthenticationRequired => 511,
84 Self::Unknown => 0,
85 }
86 }
87
88 pub fn phrase(code: ResponseStatusCode) -> String {
100 match code {
101 100 => Self::Continue.to_string(),
102 101 => Self::SwitchingProtocols.to_string(),
103 102 => Self::Processing.to_string(),
104 103 => Self::EarlyHints.to_string(),
105 200 => Self::Ok.to_string(),
106 201 => Self::Created.to_string(),
107 202 => Self::Accepted.to_string(),
108 203 => Self::NonAuthoritativeInformation.to_string(),
109 204 => Self::NoContent.to_string(),
110 205 => Self::ResetContent.to_string(),
111 206 => Self::PartialContent.to_string(),
112 207 => Self::MultiStatus.to_string(),
113 208 => Self::AlreadyReported.to_string(),
114 226 => Self::IMUsed.to_string(),
115 300 => Self::MultipleChoices.to_string(),
116 301 => Self::MovedPermanently.to_string(),
117 302 => Self::Found.to_string(),
118 303 => Self::SeeOther.to_string(),
119 304 => Self::NotModified.to_string(),
120 305 => Self::UseProxy.to_string(),
121 307 => Self::TemporaryRedirect.to_string(),
122 308 => Self::PermanentRedirect.to_string(),
123 400 => Self::BadRequest.to_string(),
124 401 => Self::Unauthorized.to_string(),
125 402 => Self::PaymentRequired.to_string(),
126 403 => Self::Forbidden.to_string(),
127 404 => Self::NotFound.to_string(),
128 405 => Self::MethodNotAllowed.to_string(),
129 406 => Self::NotAcceptable.to_string(),
130 407 => Self::ProxyAuthenticationRequired.to_string(),
131 408 => Self::RequestTimeout.to_string(),
132 409 => Self::Conflict.to_string(),
133 410 => Self::Gone.to_string(),
134 411 => Self::LengthRequired.to_string(),
135 412 => Self::PreconditionFailed.to_string(),
136 413 => Self::PayloadTooLarge.to_string(),
137 414 => Self::URITooLong.to_string(),
138 415 => Self::UnsupportedMediaType.to_string(),
139 416 => Self::RangeNotSatisfiable.to_string(),
140 417 => Self::ExpectationFailed.to_string(),
141 418 => Self::ImATeapot.to_string(),
142 421 => Self::MisdirectedRequest.to_string(),
143 422 => Self::UnprocessableEntity.to_string(),
144 423 => Self::Locked.to_string(),
145 424 => Self::FailedDependency.to_string(),
146 425 => Self::TooEarly.to_string(),
147 426 => Self::UpgradeRequired.to_string(),
148 428 => Self::PreconditionRequired.to_string(),
149 429 => Self::TooManyRequests.to_string(),
150 431 => Self::RequestHeaderFieldsTooLarge.to_string(),
151 451 => Self::UnavailableForLegalReasons.to_string(),
152 500 => Self::InternalServerError.to_string(),
153 501 => Self::NotImplemented.to_string(),
154 502 => Self::BadGateway.to_string(),
155 503 => Self::ServiceUnavailable.to_string(),
156 504 => Self::GatewayTimeout.to_string(),
157 505 => Self::HTTPVersionNotSupported.to_string(),
158 506 => Self::VariantAlsoNegotiates.to_string(),
159 507 => Self::InsufficientStorage.to_string(),
160 508 => Self::LoopDetected.to_string(),
161 510 => Self::NotExtended.to_string(),
162 511 => Self::NetworkAuthenticationRequired.to_string(),
163 _ => Self::Unknown.to_string(),
164 }
165 }
166
167 pub fn same<C>(&self, code_str: C) -> bool
180 where
181 C: AsRef<str>,
182 {
183 self.to_string().eq_ignore_ascii_case(code_str.as_ref())
184 }
185}
186
187impl Display for HttpStatus {
189 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
199 let res: &str = match self {
200 Self::Continue => CONTINUE,
201 Self::SwitchingProtocols => SWITCHING_PROTOCOLS,
202 Self::Processing => PROCESSING,
203 Self::EarlyHints => EARLY_HINTS,
204 Self::Ok => OK,
205 Self::Created => CREATED,
206 Self::Accepted => ACCEPTED,
207 Self::NonAuthoritativeInformation => NON_AUTHORITATIVE_INFORMATION,
208 Self::NoContent => NO_CONTENT,
209 Self::ResetContent => RESET_CONTENT,
210 Self::PartialContent => PARTIAL_CONTENT,
211 Self::MultiStatus => MULTI_STATUS,
212 Self::AlreadyReported => ALREADY_REPORTED,
213 Self::IMUsed => IM_USED,
214 Self::MultipleChoices => MULTIPLE_CHOICES,
215 Self::MovedPermanently => MOVED_PERMANENTLY,
216 Self::Found => FOUND,
217 Self::SeeOther => SEE_OTHER,
218 Self::NotModified => NOT_MODIFIED,
219 Self::UseProxy => USE_PROXY,
220 Self::TemporaryRedirect => TEMPORARY_REDIRECT,
221 Self::PermanentRedirect => PERMANENT_REDIRECT,
222 Self::BadRequest => BAD_REQUEST,
223 Self::Unauthorized => UNAUTHORIZED,
224 Self::PaymentRequired => PAYMENT_REQUIRED,
225 Self::Forbidden => FORBIDDEN,
226 Self::NotFound => NOT_FOUND,
227 Self::MethodNotAllowed => METHOD_NOT_ALLOWED,
228 Self::NotAcceptable => NOT_ACCEPTABLE,
229 Self::ProxyAuthenticationRequired => PROXY_AUTHENTICATION_REQUIRED,
230 Self::RequestTimeout => REQUEST_TIMEOUT,
231 Self::Conflict => CONFLICT,
232 Self::Gone => GONE,
233 Self::LengthRequired => LENGTH_REQUIRED,
234 Self::PreconditionFailed => PRECONDITION_FAILED,
235 Self::PayloadTooLarge => PAYLOAD_TOO_LARGE,
236 Self::URITooLong => URI_TOO_LONG,
237 Self::UnsupportedMediaType => UNSUPPORTED_MEDIA_TYPE,
238 Self::RangeNotSatisfiable => RANGE_NOT_SATISFIABLE,
239 Self::ExpectationFailed => EXPECTATION_FAILED,
240 Self::ImATeapot => IM_A_TEAPOT,
241 Self::MisdirectedRequest => MISDIRECTED_REQUEST,
242 Self::UnprocessableEntity => UNPROCESSABLE_ENTITY,
243 Self::Locked => LOCKED,
244 Self::FailedDependency => FAILED_DEPENDENCY,
245 Self::TooEarly => TOO_EARLY,
246 Self::UpgradeRequired => UPGRADE_REQUIRED,
247 Self::PreconditionRequired => PRECONDITION_REQUIRED,
248 Self::TooManyRequests => TOO_MANY_REQUESTS,
249 Self::RequestHeaderFieldsTooLarge => REQUEST_HEADER_FIELDS_TOO_LARGE,
250 Self::UnavailableForLegalReasons => UNAVAILABLE_FOR_LEGAL_REASONS,
251 Self::InternalServerError => INTERNAL_SERVER_ERROR,
252 Self::NotImplemented => NOT_IMPLEMENTED,
253 Self::BadGateway => BAD_GATEWAY,
254 Self::ServiceUnavailable => SERVICE_UNAVAILABLE,
255 Self::GatewayTimeout => GATEWAY_TIMEOUT,
256 Self::HTTPVersionNotSupported => HTTP_VERSION_NOT_SUPPORTED,
257 Self::VariantAlsoNegotiates => VARIANT_ALSO_NEGOTIATES,
258 Self::InsufficientStorage => INSUFFICIENT_STORAGE,
259 Self::LoopDetected => LOOP_DETECTED,
260 Self::NotExtended => NOT_EXTENDED,
261 Self::NetworkAuthenticationRequired => NETWORK_AUTHENTICATION_REQUIRED,
262 Self::Unknown => UNKNOWN,
263 };
264 write!(f, "{res}")
265 }
266}
267
268impl FromStr for HttpStatus {
270 type Err = ();
272
273 fn from_str(code_str: &str) -> Result<Self, Self::Err> {
285 if let Ok(code) = code_str.parse::<ResponseStatusCode>() {
286 match code {
287 100 => Ok(Self::Continue),
288 101 => Ok(Self::SwitchingProtocols),
289 102 => Ok(Self::Processing),
290 103 => Ok(Self::EarlyHints),
291 200 => Ok(Self::Ok),
292 201 => Ok(Self::Created),
293 202 => Ok(Self::Accepted),
294 203 => Ok(Self::NonAuthoritativeInformation),
295 204 => Ok(Self::NoContent),
296 205 => Ok(Self::ResetContent),
297 206 => Ok(Self::PartialContent),
298 207 => Ok(Self::MultiStatus),
299 208 => Ok(Self::AlreadyReported),
300 226 => Ok(Self::IMUsed),
301 300 => Ok(Self::MultipleChoices),
302 301 => Ok(Self::MovedPermanently),
303 302 => Ok(Self::Found),
304 303 => Ok(Self::SeeOther),
305 304 => Ok(Self::NotModified),
306 305 => Ok(Self::UseProxy),
307 307 => Ok(Self::TemporaryRedirect),
308 308 => Ok(Self::PermanentRedirect),
309 400 => Ok(Self::BadRequest),
310 401 => Ok(Self::Unauthorized),
311 402 => Ok(Self::PaymentRequired),
312 403 => Ok(Self::Forbidden),
313 404 => Ok(Self::NotFound),
314 405 => Ok(Self::MethodNotAllowed),
315 406 => Ok(Self::NotAcceptable),
316 407 => Ok(Self::ProxyAuthenticationRequired),
317 408 => Ok(Self::RequestTimeout),
318 409 => Ok(Self::Conflict),
319 410 => Ok(Self::Gone),
320 411 => Ok(Self::LengthRequired),
321 412 => Ok(Self::PreconditionFailed),
322 413 => Ok(Self::PayloadTooLarge),
323 414 => Ok(Self::URITooLong),
324 415 => Ok(Self::UnsupportedMediaType),
325 416 => Ok(Self::RangeNotSatisfiable),
326 417 => Ok(Self::ExpectationFailed),
327 418 => Ok(Self::ImATeapot),
328 421 => Ok(Self::MisdirectedRequest),
329 422 => Ok(Self::UnprocessableEntity),
330 423 => Ok(Self::Locked),
331 424 => Ok(Self::FailedDependency),
332 425 => Ok(Self::TooEarly),
333 426 => Ok(Self::UpgradeRequired),
334 428 => Ok(Self::PreconditionRequired),
335 429 => Ok(Self::TooManyRequests),
336 431 => Ok(Self::RequestHeaderFieldsTooLarge),
337 451 => Ok(Self::UnavailableForLegalReasons),
338 500 => Ok(Self::InternalServerError),
339 501 => Ok(Self::NotImplemented),
340 502 => Ok(Self::BadGateway),
341 503 => Ok(Self::ServiceUnavailable),
342 504 => Ok(Self::GatewayTimeout),
343 505 => Ok(Self::HTTPVersionNotSupported),
344 506 => Ok(Self::VariantAlsoNegotiates),
345 507 => Ok(Self::InsufficientStorage),
346 508 => Ok(Self::LoopDetected),
347 510 => Ok(Self::NotExtended),
348 511 => Ok(Self::NetworkAuthenticationRequired),
349 _ => Ok(Self::Unknown),
350 }
351 } else {
352 Ok(Self::Unknown)
353 }
354 }
355}