trillium_http/status.rs
1// originally from https://github.com/http-rs/http-types/blob/main/src/status_code.rs
2use crate::Error;
3use std::{
4 convert::TryFrom,
5 fmt::{self, Debug, Display},
6 str::FromStr,
7};
8
9/// HTTP response status codes.
10///
11/// The status codes of [RFC 9110 §15][rfc], plus registered extensions such as `103 Early Hints`
12/// and `429 Too Many Requests`.
13/// [Read more](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
14///
15/// [rfc]: https://www.rfc-editor.org/rfc/rfc9110#section-15
16#[repr(u16)]
17#[derive(Clone, Copy, Eq, PartialEq, Hash)]
18#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
19pub enum Status {
20 /// 100 Continue
21 ///
22 /// This interim response indicates that everything so far is OK and that
23 /// the client should continue the request, or ignore the response if
24 /// the request is already finished.
25 Continue = 100,
26
27 /// 101 Switching Protocols
28 ///
29 /// This code is sent in response to an Upgrade request header from the
30 /// client, and indicates the protocol the server is switching to.
31 SwitchingProtocols = 101,
32
33 /// 103 Early Hints
34 ///
35 /// This status code is primarily intended to be used with the Link header,
36 /// letting the user agent start preloading resources while the server
37 /// prepares a response.
38 EarlyHints = 103,
39
40 /// 200 Ok
41 ///
42 /// The request has succeeded
43 Ok = 200,
44
45 /// 201 Created
46 ///
47 /// The request has succeeded and a new resource has been created as a
48 /// result. This is typically the response sent after POST requests, or
49 /// some PUT requests.
50 Created = 201,
51
52 /// 202 Accepted
53 ///
54 /// The request has been received but not yet acted upon. It is
55 /// noncommittal, since there is no way in HTTP to later send an
56 /// asynchronous response indicating the outcome of the request. It is
57 /// intended for cases where another process or server handles the request,
58 /// or for batch processing.
59 Accepted = 202,
60
61 /// 203 Non Authoritative Information
62 ///
63 /// This response code means the returned meta-information is not exactly
64 /// the same as is available from the origin server, but is collected
65 /// from a local or a third-party copy. This is mostly used for mirrors
66 /// or backups of another resource. Except for that specific case, the
67 /// "200 OK" response is preferred to this status.
68 NonAuthoritativeInformation = 203,
69
70 /// 204 No Content
71 ///
72 /// There is no content to send for this request, but the headers may be
73 /// useful. The user-agent may update its cached headers for this
74 /// resource with the new ones.
75 NoContent = 204,
76
77 /// 205 Reset Content
78 ///
79 /// Tells the user-agent to reset the document which sent this request.
80 ResetContent = 205,
81
82 /// 206 Partial Content
83 ///
84 /// This response code is used when the Range header is sent from the client
85 /// to request only part of a resource.
86 PartialContent = 206,
87
88 /// 207 Multi-Status
89 ///
90 /// A Multi-Status response conveys information about
91 /// multiple resources in situations where multiple
92 /// status codes might be appropriate.
93 MultiStatus = 207,
94
95 /// 226 Im Used
96 ///
97 /// The server has fulfilled a GET request for the resource, and the
98 /// response is a representation of the result of one or more
99 /// instance-manipulations applied to the current instance.
100 ImUsed = 226,
101
102 /// 300 Multiple Choice
103 ///
104 /// The request has more than one possible response. The user-agent or user
105 /// should choose one of them. (There is no standardized way of choosing
106 /// one of the responses, but HTML links to the possibilities are
107 /// recommended so the user can pick.)
108 MultipleChoice = 300,
109
110 /// 301 Moved Permanently
111 ///
112 /// The URL of the requested resource has been changed permanently. The new
113 /// URL is given in the response.
114 MovedPermanently = 301,
115
116 /// 302 Found
117 ///
118 /// This response code means that the URI of requested resource has been
119 /// changed temporarily. Further changes in the URI might be made in the
120 /// future. Therefore, this same URI should be used by the client in
121 /// future requests.
122 Found = 302,
123
124 /// 303 See Other
125 ///
126 /// The server sent this response to direct the client to get the requested
127 /// resource at another URI with a GET request.
128 SeeOther = 303,
129
130 /// 304 Not Modified
131 ///
132 /// This is used for caching purposes. It tells the client that the response
133 /// has not been modified, so the client can continue to use the same
134 /// cached version of the response.
135 NotModified = 304,
136
137 /// 307 Temporary Redirect
138 ///
139 /// The server sends this response to direct the client to get the requested
140 /// resource at another URI with same method that was used in the prior
141 /// request. This has the same semantics as the 302 Found HTTP response
142 /// code, with the exception that the user agent must not change the
143 /// HTTP method used: If a POST was used in the first request, a POST must
144 /// be used in the second request.
145 TemporaryRedirect = 307,
146
147 /// 308 Permanent Redirect
148 ///
149 /// This means that the resource is now permanently located at another URI,
150 /// specified by the Location: HTTP Response header. This has the same
151 /// semantics as the 301 Moved Permanently HTTP response code, with the
152 /// exception that the user agent must not change the HTTP method
153 /// used: If a POST was used in the first request, a POST must be used in
154 /// the second request.
155 PermanentRedirect = 308,
156
157 /// 400 Bad Request
158 ///
159 /// The server could not understand the request due to invalid syntax.
160 BadRequest = 400,
161
162 /// 401 Unauthorized
163 ///
164 /// Although the HTTP standard specifies "unauthorized", semantically this
165 /// response means "unauthenticated". That is, the client must
166 /// authenticate itself to get the requested response.
167 Unauthorized = 401,
168
169 /// 402 Payment Required
170 ///
171 /// This response code is reserved for future use. The initial aim for
172 /// creating this code was using it for digital payment systems, however
173 /// this status code is used very rarely and no standard convention
174 /// exists.
175 PaymentRequired = 402,
176
177 /// 403 Forbidden
178 ///
179 /// The client does not have access rights to the content; that is, it is
180 /// unauthorized, so the server is refusing to give the requested
181 /// resource. Unlike 401, the client's identity is known to the server.
182 Forbidden = 403,
183
184 /// 404 Not Found
185 ///
186 /// The server can not find requested resource. In the browser, this means
187 /// the URL is not recognized. In an API, this can also mean that the
188 /// endpoint is valid but the resource itself does not exist. Servers
189 /// may also send this response instead of 403 to hide the existence of
190 /// a resource from an unauthorized client. This response code is probably
191 /// the most famous one due to its frequent occurrence on the web.
192 NotFound = 404,
193
194 /// 405 Method Not Allowed
195 ///
196 /// The request method is known by the server but has been disabled and
197 /// cannot be used. For example, an API may forbid DELETE-ing a
198 /// resource. The two mandatory methods, GET and HEAD, must never be
199 /// disabled and should not return this error code.
200 MethodNotAllowed = 405,
201
202 /// 406 Not Acceptable
203 ///
204 /// This response is sent when the web server, after performing
205 /// server-driven content negotiation, doesn't find any content that
206 /// conforms to the criteria given by the user agent.
207 NotAcceptable = 406,
208
209 /// 407 Proxy Authentication Required
210 ///
211 /// This is similar to 401 but authentication is needed to be done by a
212 /// proxy.
213 ProxyAuthenticationRequired = 407,
214
215 /// 408 Request Timeout
216 ///
217 /// This response is sent on an idle connection by some servers, even
218 /// without any previous request by the client. It means that the server
219 /// would like to shut down this unused connection. This response is
220 /// used much more since some browsers, like Chrome, Firefox 27+,
221 /// or IE9, use HTTP pre-connection mechanisms to speed up surfing. Also
222 /// note that some servers merely shut down the connection without
223 /// sending this message.
224 RequestTimeout = 408,
225
226 /// 409 Conflict
227 ///
228 /// This response is sent when a request conflicts with the current state of
229 /// the server.
230 Conflict = 409,
231
232 /// 410 Gone
233 ///
234 /// This response is sent when the requested content has been permanently
235 /// deleted from server, with no forwarding address. Clients are
236 /// expected to remove their caches and links to the resource. The HTTP
237 /// specification intends this status code to be used for "limited-time,
238 /// promotional services". APIs should not feel compelled to indicate
239 /// resources that have been deleted with this status code.
240 Gone = 410,
241
242 /// 411 Length Required
243 ///
244 /// Server rejected the request because the Content-Length header field is
245 /// not defined and the server requires it.
246 LengthRequired = 411,
247
248 /// 412 Precondition Failed
249 ///
250 /// The client has indicated preconditions in its headers which the server
251 /// does not meet.
252 PreconditionFailed = 412,
253
254 /// 413 Payload Too Large
255 ///
256 /// Request entity is larger than limits defined by server; the server might
257 /// close the connection or return an Retry-After header field.
258 PayloadTooLarge = 413,
259
260 /// 414 URI Too Long
261 ///
262 /// The URI requested by the client is longer than the server is willing to
263 /// interpret.
264 UriTooLong = 414,
265
266 /// 415 Unsupported Media Type
267 ///
268 /// The media format of the requested data is not supported by the server,
269 /// so the server is rejecting the request.
270 UnsupportedMediaType = 415,
271
272 /// 416 Requested Range Not Satisfiable
273 ///
274 /// The range specified by the Range header field in the request can't be
275 /// fulfilled; it's possible that the range is outside the size of the
276 /// target URI's data.
277 RequestedRangeNotSatisfiable = 416,
278
279 /// 417 Expectation Failed
280 ///
281 /// This response code means the expectation indicated by the Expect request
282 /// header field can't be met by the server.
283 ExpectationFailed = 417,
284 /// 418 I'm a teapot
285 ///
286 /// The server refuses the attempt to brew coffee with a teapot.
287 ImATeapot = 418,
288
289 /// 421 Misdirected Request
290 ///
291 /// The request was directed at a server that is not able to produce a
292 /// response. This can be sent by a server that is not configured to
293 /// produce responses for the combination of scheme and authority that
294 /// are included in the request URI.
295 MisdirectedRequest = 421,
296
297 /// 422 Unprocessable Entity
298 ///
299 /// The request was well-formed but was unable to be followed due to
300 /// semantic errors.
301 UnprocessableEntity = 422,
302
303 /// 423 Locked
304 ///
305 /// The resource that is being accessed is locked.
306 Locked = 423,
307
308 /// 424 Failed Dependency
309 ///
310 /// The request failed because it depended on another request and that
311 /// request failed (e.g., a PROPPATCH).
312 FailedDependency = 424,
313
314 /// 425 Too Early
315 ///
316 /// Indicates that the server is unwilling to risk processing a request that
317 /// might be replayed.
318 TooEarly = 425,
319
320 /// 426 Upgrade Required
321 ///
322 /// The server refuses to perform the request using the current protocol but
323 /// might be willing to do so after the client upgrades to a different
324 /// protocol. The server sends an Upgrade header in a 426 response to
325 /// indicate the required protocol(s).
326 UpgradeRequired = 426,
327
328 /// 428 Precondition Required
329 ///
330 /// The origin server requires the request to be conditional. This response
331 /// is intended to prevent the 'lost update' problem, where a client
332 /// GETs a resource's state, modifies it, and PUTs it back to the
333 /// server, when meanwhile a third party has modified the state on the
334 /// server, leading to a conflict.
335 PreconditionRequired = 428,
336
337 /// 429 Too Many Requests
338 ///
339 /// The user has sent too many requests in a given amount of time ("rate
340 /// limiting").
341 TooManyRequests = 429,
342
343 /// 431 Request Header Fields Too Large
344 ///
345 /// The server is unwilling to process the request because its header fields
346 /// are too large. The request may be resubmitted after reducing the
347 /// size of the request header fields.
348 RequestHeaderFieldsTooLarge = 431,
349
350 /// 451 Unavailable For Legal Reasons
351 ///
352 /// The user-agent requested a resource that cannot legally be provided,
353 /// such as a web page censored by a government.
354 UnavailableForLegalReasons = 451,
355
356 /// 500 Internal Server Error
357 ///
358 /// The server has encountered a situation it doesn't know how to handle.
359 InternalServerError = 500,
360
361 /// 501 Not Implemented
362 ///
363 /// The request method is not supported by the server and cannot be handled.
364 /// The only methods that servers are required to support (and therefore
365 /// that must not return this code) are GET and HEAD.
366 NotImplemented = 501,
367
368 /// 502 Bad Gateway
369 ///
370 /// This error response means that the server, while working as a gateway to
371 /// get a response needed to handle the request, got an invalid
372 /// response.
373 BadGateway = 502,
374
375 /// 503 Service Unavailable
376 ///
377 /// The server is not ready to handle the request. Common causes are a
378 /// server that is down for maintenance or that is overloaded. Note that
379 /// together with this response, a user-friendly page explaining the
380 /// problem should be sent. This responses should be used for temporary
381 /// conditions and the Retry-After: HTTP header should, if possible, contain
382 /// the estimated time before the recovery of the service. The webmaster
383 /// must also take care about the caching-related headers that are sent
384 /// along with this response, as these temporary condition responses
385 /// should usually not be cached.
386 ServiceUnavailable = 503,
387
388 /// 504 Gateway Timeout
389 ///
390 /// This error response is given when the server is acting as a gateway and
391 /// cannot get a response in time.
392 GatewayTimeout = 504,
393
394 /// 505 HTTP Version Not Supported
395 ///
396 /// The HTTP version used in the request is not supported by the server.
397 HttpVersionNotSupported = 505,
398
399 /// 506 Variant Also Negotiates
400 ///
401 /// The server has an internal configuration error: the chosen variant
402 /// resource is configured to engage in transparent content negotiation
403 /// itself, and is therefore not a proper end point in the negotiation
404 /// process.
405 VariantAlsoNegotiates = 506,
406
407 /// 507 Insufficient Storage
408 ///
409 /// The server is unable to store the representation needed to complete the
410 /// request.
411 InsufficientStorage = 507,
412
413 /// 508 Loop Detected
414 ///
415 /// The server detected an infinite loop while processing the request.
416 LoopDetected = 508,
417
418 /// 510 Not Extended
419 ///
420 /// Further extensions to the request are required for the server to fulfil
421 /// it.
422 NotExtended = 510,
423
424 /// 511 Network Authentication Required
425 ///
426 /// The 511 status code indicates that the client needs to authenticate to
427 /// gain network access.
428 NetworkAuthenticationRequired = 511,
429}
430
431impl Status {
432 /// Interprets a status code received from a peer. Never fails.
433 ///
434 /// A recognized code converts exactly, as with [`TryFrom<u16>`]. An unrecognized code from 100
435 /// to 599 becomes the `x00` status of its class, and a code outside that range becomes
436 /// [`Status::InternalServerError`], as [RFC 9110 §15][rfc] directs a client to treat them. The
437 /// original code is not recoverable from the result.
438 ///
439 /// [rfc]: https://www.rfc-editor.org/rfc/rfc9110#section-15
440 #[cfg(feature = "unstable")]
441 #[doc(hidden)]
442 pub fn from_code_lossy(code: u16) -> Self {
443 Self::try_from(code).unwrap_or(match code / 100 {
444 1 => Self::Continue,
445 2 => Self::Ok,
446 3 => Self::MultipleChoice,
447 4 => Self::BadRequest,
448 _ => Self::InternalServerError,
449 })
450 }
451
452 /// Retrieve a string representation of the status code (just the number, not the reason).
453 ///
454 /// See also [`Status::canonical_reason`]
455 pub const fn code(&self) -> &'static str {
456 match self {
457 Self::Continue => "100",
458 Self::SwitchingProtocols => "101",
459 Self::EarlyHints => "103",
460 Self::Ok => "200",
461 Self::Created => "201",
462 Self::Accepted => "202",
463 Self::NonAuthoritativeInformation => "203",
464 Self::NoContent => "204",
465 Self::ResetContent => "205",
466 Self::PartialContent => "206",
467 Self::MultiStatus => "207",
468 Self::ImUsed => "226",
469 Self::MultipleChoice => "300",
470 Self::MovedPermanently => "301",
471 Self::Found => "302",
472 Self::SeeOther => "303",
473 Self::NotModified => "304",
474 Self::TemporaryRedirect => "307",
475 Self::PermanentRedirect => "308",
476 Self::BadRequest => "400",
477 Self::Unauthorized => "401",
478 Self::PaymentRequired => "402",
479 Self::Forbidden => "403",
480 Self::NotFound => "404",
481 Self::MethodNotAllowed => "405",
482 Self::NotAcceptable => "406",
483 Self::ProxyAuthenticationRequired => "407",
484 Self::RequestTimeout => "408",
485 Self::Conflict => "409",
486 Self::Gone => "410",
487 Self::LengthRequired => "411",
488 Self::PreconditionFailed => "412",
489 Self::PayloadTooLarge => "413",
490 Self::UriTooLong => "414",
491 Self::UnsupportedMediaType => "415",
492 Self::RequestedRangeNotSatisfiable => "416",
493 Self::ExpectationFailed => "417",
494 Self::ImATeapot => "418",
495 Self::MisdirectedRequest => "421",
496 Self::UnprocessableEntity => "422",
497 Self::Locked => "423",
498 Self::FailedDependency => "424",
499 Self::TooEarly => "425",
500 Self::UpgradeRequired => "426",
501 Self::PreconditionRequired => "428",
502 Self::TooManyRequests => "429",
503 Self::RequestHeaderFieldsTooLarge => "431",
504 Self::UnavailableForLegalReasons => "451",
505 Self::InternalServerError => "500",
506 Self::NotImplemented => "501",
507 Self::BadGateway => "502",
508 Self::ServiceUnavailable => "503",
509 Self::GatewayTimeout => "504",
510 Self::HttpVersionNotSupported => "505",
511 Self::VariantAlsoNegotiates => "506",
512 Self::InsufficientStorage => "507",
513 Self::LoopDetected => "508",
514 Self::NotExtended => "510",
515 Self::NetworkAuthenticationRequired => "511",
516 }
517 }
518
519 /// Returns `true` if the status code is `1xx` range.
520 ///
521 /// If this returns `true` it indicates that the request was received,
522 /// continuing process.
523 pub fn is_informational(&self) -> bool {
524 let num: u16 = (*self).into();
525 (100..200).contains(&num)
526 }
527
528 /// Returns `true` if the status code is the `2xx` range.
529 ///
530 /// If this returns `true` it indicates that the request was successfully
531 /// received, understood, and accepted.
532 pub fn is_success(&self) -> bool {
533 let num: u16 = (*self).into();
534 (200..300).contains(&num)
535 }
536
537 /// Returns `true` if the status code is the `3xx` range.
538 ///
539 /// If this returns `true` it indicates that further action needs to be
540 /// taken in order to complete the request.
541 pub fn is_redirection(&self) -> bool {
542 let num: u16 = (*self).into();
543 (300..400).contains(&num)
544 }
545
546 /// Returns `true` if the status code is the `4xx` range.
547 ///
548 /// If this returns `true` it indicates that the request contains bad syntax
549 /// or cannot be fulfilled.
550 pub fn is_client_error(&self) -> bool {
551 let num: u16 = (*self).into();
552 (400..500).contains(&num)
553 }
554
555 /// Returns `true` if the status code is the `5xx` range.
556 ///
557 /// If this returns `true` it indicates that the server failed to fulfill an
558 /// apparently valid request.
559 pub fn is_server_error(&self) -> bool {
560 let num: u16 = (*self).into();
561 (500..600).contains(&num)
562 }
563
564 /// The canonical reason for a given status code
565 pub const fn canonical_reason(&self) -> &'static str {
566 match self {
567 Status::Continue => "Continue",
568 Status::SwitchingProtocols => "Switching Protocols",
569 Status::EarlyHints => "Early Hints",
570 Status::Ok => "OK",
571 Status::Created => "Created",
572 Status::Accepted => "Accepted",
573 Status::NonAuthoritativeInformation => "Non Authoritative Information",
574 Status::NoContent => "No Content",
575 Status::ResetContent => "Reset Content",
576 Status::PartialContent => "Partial Content",
577 Status::MultiStatus => "Multi-Status",
578 Status::ImUsed => "Im Used",
579 Status::MultipleChoice => "Multiple Choice",
580 Status::MovedPermanently => "Moved Permanently",
581 Status::Found => "Found",
582 Status::SeeOther => "See Other",
583 Status::NotModified => "Not Modified",
584 Status::TemporaryRedirect => "Temporary Redirect",
585 Status::PermanentRedirect => "Permanent Redirect",
586 Status::BadRequest => "Bad Request",
587 Status::Unauthorized => "Unauthorized",
588 Status::PaymentRequired => "Payment Required",
589 Status::Forbidden => "Forbidden",
590 Status::NotFound => "Not Found",
591 Status::MethodNotAllowed => "Method Not Allowed",
592 Status::NotAcceptable => "Not Acceptable",
593 Status::ProxyAuthenticationRequired => "Proxy Authentication Required",
594 Status::RequestTimeout => "Request Timeout",
595 Status::Conflict => "Conflict",
596 Status::Gone => "Gone",
597 Status::LengthRequired => "Length Required",
598 Status::PreconditionFailed => "Precondition Failed",
599 Status::PayloadTooLarge => "Payload Too Large",
600 Status::UriTooLong => "URI Too Long",
601 Status::UnsupportedMediaType => "Unsupported Media Type",
602 Status::RequestedRangeNotSatisfiable => "Requested Range Not Satisfiable",
603 Status::ExpectationFailed => "Expectation Failed",
604 Status::ImATeapot => "I'm a teapot",
605 Status::MisdirectedRequest => "Misdirected Request",
606 Status::UnprocessableEntity => "Unprocessable Entity",
607 Status::Locked => "Locked",
608 Status::FailedDependency => "Failed Dependency",
609 Status::TooEarly => "Too Early",
610 Status::UpgradeRequired => "Upgrade Required",
611 Status::PreconditionRequired => "Precondition Required",
612 Status::TooManyRequests => "Too Many Requests",
613 Status::RequestHeaderFieldsTooLarge => "Request Header Fields Too Large",
614 Status::UnavailableForLegalReasons => "Unavailable For Legal Reasons",
615 Status::InternalServerError => "Internal Server Error",
616 Status::NotImplemented => "Not Implemented",
617 Status::BadGateway => "Bad Gateway",
618 Status::ServiceUnavailable => "Service Unavailable",
619 Status::GatewayTimeout => "Gateway Timeout",
620 Status::HttpVersionNotSupported => "HTTP Version Not Supported",
621 Status::VariantAlsoNegotiates => "Variant Also Negotiates",
622 Status::InsufficientStorage => "Insufficient Storage",
623 Status::LoopDetected => "Loop Detected",
624 Status::NotExtended => "Not Extended",
625 Status::NetworkAuthenticationRequired => "Network Authentication Required",
626 }
627 }
628}
629
630impl From<Status> for u16 {
631 fn from(code: Status) -> u16 {
632 code as u16
633 }
634}
635
636impl TryFrom<u16> for Status {
637 type Error = Error;
638
639 fn try_from(num: u16) -> Result<Self, Self::Error> {
640 match num {
641 100 => Ok(Status::Continue),
642 101 => Ok(Status::SwitchingProtocols),
643 103 => Ok(Status::EarlyHints),
644 200 => Ok(Status::Ok),
645 201 => Ok(Status::Created),
646 202 => Ok(Status::Accepted),
647 203 => Ok(Status::NonAuthoritativeInformation),
648 204 => Ok(Status::NoContent),
649 205 => Ok(Status::ResetContent),
650 206 => Ok(Status::PartialContent),
651 207 => Ok(Status::MultiStatus),
652 226 => Ok(Status::ImUsed),
653 300 => Ok(Status::MultipleChoice),
654 301 => Ok(Status::MovedPermanently),
655 302 => Ok(Status::Found),
656 303 => Ok(Status::SeeOther),
657 304 => Ok(Status::NotModified),
658 307 => Ok(Status::TemporaryRedirect),
659 308 => Ok(Status::PermanentRedirect),
660 400 => Ok(Status::BadRequest),
661 401 => Ok(Status::Unauthorized),
662 402 => Ok(Status::PaymentRequired),
663 403 => Ok(Status::Forbidden),
664 404 => Ok(Status::NotFound),
665 405 => Ok(Status::MethodNotAllowed),
666 406 => Ok(Status::NotAcceptable),
667 407 => Ok(Status::ProxyAuthenticationRequired),
668 408 => Ok(Status::RequestTimeout),
669 409 => Ok(Status::Conflict),
670 410 => Ok(Status::Gone),
671 411 => Ok(Status::LengthRequired),
672 412 => Ok(Status::PreconditionFailed),
673 413 => Ok(Status::PayloadTooLarge),
674 414 => Ok(Status::UriTooLong),
675 415 => Ok(Status::UnsupportedMediaType),
676 416 => Ok(Status::RequestedRangeNotSatisfiable),
677 417 => Ok(Status::ExpectationFailed),
678 418 => Ok(Status::ImATeapot),
679 421 => Ok(Status::MisdirectedRequest),
680 422 => Ok(Status::UnprocessableEntity),
681 423 => Ok(Status::Locked),
682 424 => Ok(Status::FailedDependency),
683 425 => Ok(Status::TooEarly),
684 426 => Ok(Status::UpgradeRequired),
685 428 => Ok(Status::PreconditionRequired),
686 429 => Ok(Status::TooManyRequests),
687 431 => Ok(Status::RequestHeaderFieldsTooLarge),
688 451 => Ok(Status::UnavailableForLegalReasons),
689 500 => Ok(Status::InternalServerError),
690 501 => Ok(Status::NotImplemented),
691 502 => Ok(Status::BadGateway),
692 503 => Ok(Status::ServiceUnavailable),
693 504 => Ok(Status::GatewayTimeout),
694 505 => Ok(Status::HttpVersionNotSupported),
695 506 => Ok(Status::VariantAlsoNegotiates),
696 507 => Ok(Status::InsufficientStorage),
697 508 => Ok(Status::LoopDetected),
698 510 => Ok(Status::NotExtended),
699 511 => Ok(Status::NetworkAuthenticationRequired),
700 _ => Err(Error::InvalidStatus),
701 }
702 }
703}
704
705impl PartialEq<Status> for u16 {
706 fn eq(&self, other: &Status) -> bool {
707 *self == *other as u16
708 }
709}
710
711impl PartialEq<u16> for Status {
712 fn eq(&self, other: &u16) -> bool {
713 *self as u16 == *other
714 }
715}
716
717impl Debug for Status {
718 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
719 Debug::fmt(&(*self as u16), f)
720 }
721}
722
723impl Display for Status {
724 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
725 write!(f, "{} {}", *self as u16, self.canonical_reason())
726 }
727}
728
729impl FromStr for Status {
730 type Err = Error;
731
732 fn from_str(s: &str) -> Result<Self, Self::Err> {
733 u16::from_str(s)
734 .map_err(|_| Error::InvalidStatus)?
735 .try_into()
736 }
737}