dropshot 0.16.2

expose REST APIs from a Rust program
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
// Copyright 2024 Oxide Computer Company
// Copyright 2017 http-rs authors

//! Newtypes around [`http::StatusCode`] that are limited to status ranges
//! representing errors.

use std::fmt;

/// An HTTP 4xx (client error) or 5xx (server error) status code.
///
/// This is a refinement of the [`http::StatusCode`] type that is limited to the
/// error status code ranges. It may be constructed from any
/// [`http::StatusCode`] using the `TryFrom` implementation, which fails if the
/// status is not a 4xx or 5xx status code.
///
/// Alternatively, constants are provided for known error status codes, such as
/// [`ErrorStatusCode::BAD_REQUEST`], [`ErrorStatusCode::NOT_FOUND`],
/// [`ErrorStatusCode::INTERNAL_SERVER_ERROR`], and so on, including those in
/// the IANA HTTP Status Code Registry][iana]. Using these constants avoids the
/// fallible conversion from an [`http::StatusCode`].
///
/// [iana]: https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ErrorStatusCode(http::StatusCode);

// Generate constants for a `http::StatusCode` wrapper type that re-export the
// provided constants defined by `http::StatusCode.`
macro_rules! error_status_code_constants {
    ( $($(#[$docs:meta])* $name:ident;)+ ) => {
        $(
            $(#[$docs])*
            pub const $name: Self = Self(http::StatusCode::$name);
        )+
    }
}

// Implement conversions and traits for `http::StatusCode` wrapper types. This
// generates conversions between the wrapper type and `http::StatusCode`,
// conversions between the wrapper and types that `StatusCode` has conversions
// between, and equality and fmt implementations.
macro_rules! impl_status_code_wrapper {
    (impl StatusCode for $T:ident {
        type NotAnError = $NotAnError:ident;
        type Invalid = $Invalid:ident;
    }) => {
        impl $T {
            /// Converts a `&[u8]` to an error status code
            pub fn from_bytes(src: &[u8]) -> Result<Self, $Invalid> {
                let status = http::StatusCode::from_bytes(src)?;
                Self::from_status(status).map_err(Into::into)
            }

            /// Returns the [`http::StatusCode`] corresponding to this error
            /// status code.
            ///
            /// # Note
            ///
            /// This is the same as the `Into<http::StatusCode>` implementation,
            /// but included as an inherent method because that implementation
            /// doesn't appear in rustdocs, as well as a way to force the type
            /// instead of relying on inference.
            pub fn as_status(&self) -> http::StatusCode {
                self.0
            }

            /// Returns the `u16` corresponding to this `error status code.
            ///
            /// # Note
            ///
            /// This is the same as the `Into<u16>` implementation, but included
            /// as an inherent method because that implementation doesn't appear
            /// in rustdocs, as well as a way to force the type instead of
            /// relying on inference.
            ///
            /// This method wraps the [`http::StatusCode::as_u16`] method.
            pub fn as_u16(&self) -> u16 {
                self.0.as_u16()
            }

            /// Returns a `&str` representation of the `StatusCode`
            ///
            /// The return value only includes a numerical representation of the
            /// status code. The canonical reason is not included.
            ///
            /// This method wraps the [`http::StatusCode::as_str`] method.
            pub fn as_str(&self) -> &str {
                self.0.as_str()
            }

            /// Get the standardised `reason-phrase` for this status code.
            ///
            /// This is mostly here for servers writing responses, but could
            /// potentially have application at other times.
            ///
            /// The reason phrase is defined as being exclusively for human
            /// readers. You should avoid deriving any meaning from it at all
            /// costs.
            ///
            /// Bear in mind also that in HTTP/2.0 and HTTP/3.0 the reason
            /// phrase is abolished from transmission, and so this canonical
            /// reason phrase really is the only reason phrase you’ll find.
            ///
            /// This method wraps the [`http::StatusCode::canonical_reason`]
            /// method.
            pub fn canonical_reason(&self) -> Option<&'static str> {
                self.0.canonical_reason()
            }
        }

        impl fmt::Debug for $T {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                fmt::Debug::fmt(&self.0, f)
            }
        }

        /// Formats the status code, *including* the canonical reason.
        impl fmt::Display for $T {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                fmt::Display::fmt(&self.0, f)
            }
        }

        impl PartialEq<u16> for $T {
            #[inline]
            fn eq(&self, other: &u16) -> bool {
                self.as_u16() == *other
            }
        }

        impl PartialEq<$T> for u16 {
            #[inline]
            fn eq(&self, other: &$T) -> bool {
                *self == other.as_u16()
            }
        }

        impl PartialEq<http::StatusCode> for $T {
            #[inline]
            fn eq(&self, other: &http::StatusCode) -> bool {
                self.0 == *other
            }
        }

        impl PartialEq<$T> for http::StatusCode {
            #[inline]
            fn eq(&self, other: &$T) -> bool {
                *self == other.0
            }
        }

        impl From<$T> for u16 {
            #[inline]
            fn from(status: $T) -> u16 {
                status.as_u16()
            }
        }

        impl std::str::FromStr for $T {
            type Err = $Invalid;

            fn from_str(s: &str) -> Result<Self, Self::Err> {
                Ok(http::StatusCode::from_str(s)?.try_into()?)
            }
        }

        impl From<&'_ $T> for $T {
            #[inline]
            fn from(t: &$T) -> Self {
                t.to_owned()
            }
        }

        impl TryFrom<&'_ [u8]> for $T {
            type Error = $Invalid;

            #[inline]
            fn try_from(t: &[u8]) -> Result<Self, Self::Error> {
                $T::from_bytes(t)
            }
        }

        impl TryFrom<&'_ str> for $T {
            type Error = $Invalid;

            #[inline]
            fn try_from(t: &str) -> Result<Self, Self::Error> {
                t.parse()
            }
        }

        impl TryFrom<u16> for $T {
            type Error = $Invalid;

            #[inline]
            fn try_from(t: u16) -> Result<Self, Self::Error> {
                Self::from_u16(t)
            }
        }

        impl TryFrom<http::StatusCode> for $T {
            type Error = $NotAnError;

            fn try_from(value: http::StatusCode) -> Result<Self, Self::Error> {
                Self::from_status(value)
            }
        }
    };
}

impl ErrorStatusCode {
    // These constants are copied from the `http` crate's `StatusCode` type.
    // Should new status codes be standardized and added upstream, we should add
    // them to this list.
    error_status_code_constants! {
        /// 400 Bad Request [[RFC7231, Section
        /// 6.5.1](https://tools.ietf.org/html/rfc7231#section-6.5.1)]
        BAD_REQUEST;

        /// 401 Unauthorized [[RFC7235, Section
        /// 3.1](https://tools.ietf.org/html/rfc7235#section-3.1)]
        UNAUTHORIZED;
        /// 402 Payment Required [[RFC7231, Section
        /// 6.5.2](https://tools.ietf.org/html/rfc7231#section-6.5.2)]
        PAYMENT_REQUIRED;
        /// 403 Forbidden [[RFC7231, Section
        /// 6.5.3](https://tools.ietf.org/html/rfc7231#section-6.5.3)]
        FORBIDDEN;
        /// 404 Not Found [[RFC7231, Section
        /// 6.5.4](https://tools.ietf.org/html/rfc7231#section-6.5.4)]
        NOT_FOUND;
        /// 405 Method Not Allowed [[RFC7231, Section
        /// 6.5.5](https://tools.ietf.org/html/rfc7231#section-6.5.5)]
        METHOD_NOT_ALLOWED;
        /// 406 Not Acceptable [[RFC7231, Section
        /// 6.5.6](https://tools.ietf.org/html/rfc7231#section-6.5.6)]
        NOT_ACCEPTABLE;
        /// 407 Proxy Authentication Required [[RFC7235, Section
        /// 3.2](https://tools.ietf.org/html/rfc7235#section-3.2)]
        PROXY_AUTHENTICATION_REQUIRED;
        /// 408 Request Timeout [[RFC7231, Section
        /// 6.5.7](https://tools.ietf.org/html/rfc7231#section-6.5.7)]
        REQUEST_TIMEOUT;
        /// 409 Conflict [[RFC7231, Section
        /// 6.5.8](https://tools.ietf.org/html/rfc7231#section-6.5.8)]
        CONFLICT;
        /// 410 Gone [[RFC7231, Section
        /// 6.5.9](https://tools.ietf.org/html/rfc7231#section-6.5.9)]
        GONE;
        /// 411 Length Required [[RFC7231, Section
        /// 6.5.10](https://tools.ietf.org/html/rfc7231#section-6.5.10)]
        LENGTH_REQUIRED;
        /// 412 Precondition Failed [[RFC7232, Section
        /// 4.2](https://tools.ietf.org/html/rfc7232#section-4.2)]
        PRECONDITION_FAILED;
        /// 413 Payload Too Large [[RFC7231, Section
        /// 6.5.11](https://tools.ietf.org/html/rfc7231#section-6.5.11)]
        PAYLOAD_TOO_LARGE;
        /// 414 URI Too Long [[RFC7231, Section
        /// 6.5.12](https://tools.ietf.org/html/rfc7231#section-6.5.12)]
        URI_TOO_LONG;
        /// 415 Unsupported Media Type [[RFC7231, Section
        /// 6.5.13](https://tools.ietf.org/html/rfc7231#section-6.5.13)]
        UNSUPPORTED_MEDIA_TYPE;
        /// 416 Range Not Satisfiable [[RFC7233, Section
        /// 4.4](https://tools.ietf.org/html/rfc7233#section-4.4)]
        RANGE_NOT_SATISFIABLE;
        /// 417 Expectation Failed [[RFC7231, Section
        /// 6.5.14](https://tools.ietf.org/html/rfc7231#section-6.5.14)]
        EXPECTATION_FAILED;
        /// 418 I'm a teapot [curiously not registered by IANA but
        /// [RFC2324](https://tools.ietf.org/html/rfc2324)]
        IM_A_TEAPOT;

        /// 421 Misdirected Request [RFC7540, Section
        /// 9.1.2](https://tools.ietf.org/html/rfc7540#section-9.1.2)
        MISDIRECTED_REQUEST;
        /// 422 Unprocessable Entity
        /// [[RFC4918](https://tools.ietf.org/html/rfc4918)]
        UNPROCESSABLE_ENTITY;
        /// 423 Locked [[RFC4918](https://tools.ietf.org/html/rfc4918)]
        LOCKED;
        /// 424 Failed Dependency
        /// [[RFC4918](https://tools.ietf.org/html/rfc4918)]
        FAILED_DEPENDENCY;

        /// 426 Upgrade Required [[RFC7231, Section
        /// 6.5.15](https://tools.ietf.org/html/rfc7231#section-6.5.15)]
        UPGRADE_REQUIRED;

        /// 428 Precondition Required
        /// [[RFC6585](https://tools.ietf.org/html/rfc6585)]
        PRECONDITION_REQUIRED;
        /// 429 Too Many Requests
        /// [[RFC6585](https://tools.ietf.org/html/rfc6585)]
        TOO_MANY_REQUESTS;

        /// 431 Request Header Fields Too Large
        /// [[RFC6585](https://tools.ietf.org/html/rfc6585)]
        REQUEST_HEADER_FIELDS_TOO_LARGE;

        /// 451 Unavailable For Legal Reasons
        /// [[RFC7725](https://tools.ietf.org/html/rfc7725)]
        UNAVAILABLE_FOR_LEGAL_REASONS;

        /// 500 Internal Server Error [[RFC7231, Section
        /// 6.6.1](https://tools.ietf.org/html/rfc7231#section-6.6.1)]
        INTERNAL_SERVER_ERROR;
        /// 501 Not Implemented [[RFC7231, Section
        /// 6.6.2](https://tools.ietf.org/html/rfc7231#section-6.6.2)]
        NOT_IMPLEMENTED;
        /// 502 Bad Gateway [[RFC7231, Section
        /// 6.6.3](https://tools.ietf.org/html/rfc7231#section-6.6.3)]
        BAD_GATEWAY;
        /// 503 Service Unavailable [[RFC7231, Section
        /// 6.6.4](https://tools.ietf.org/html/rfc7231#section-6.6.4)]
        SERVICE_UNAVAILABLE;
        /// 504 Gateway Timeout [[RFC7231, Section
        /// 6.6.5](https://tools.ietf.org/html/rfc7231#section-6.6.5)]
        GATEWAY_TIMEOUT;
        /// 505 HTTP Version Not Supported [[RFC7231, Section
        /// 6.6.6](https://tools.ietf.org/html/rfc7231#section-6.6.6)]
        HTTP_VERSION_NOT_SUPPORTED;
        /// 506 Variant Also Negotiates
        /// [[RFC2295](https://tools.ietf.org/html/rfc2295)]
        VARIANT_ALSO_NEGOTIATES;
        /// 507 Insufficient Storage
        /// [[RFC4918](https://tools.ietf.org/html/rfc4918)]
        INSUFFICIENT_STORAGE;
        /// 508 Loop Detected [[RFC5842](https://tools.ietf.org/html/rfc5842)]
        LOOP_DETECTED;

        /// 510 Not Extended [[RFC2774](https://tools.ietf.org/html/rfc2774)]
        NOT_EXTENDED;
        /// 511 Network Authentication Required
        /// [[RFC6585](https://tools.ietf.org/html/rfc6585)]
        NETWORK_AUTHENTICATION_REQUIRED;
    }

    /// Converts an [`http::StatusCode`] into an [`ErrorStatusCode`].
    ///
    /// # Returns
    ///
    /// - [`Ok`]`(`[`ErrorStatusCode`]`)` if the status code is a 4xx or 5xx
    ///   status code.
    /// - [`Err`]`(`[`NotAnError`]`)` if the status code is a 1xx, 2xx, or 3xx
    ///   status code.
    pub fn from_status(status: http::StatusCode) -> Result<Self, NotAnError> {
        if status.is_client_error() || status.is_server_error() {
            Ok(ErrorStatusCode(status))
        } else {
            Err(NotAnError(status))
        }
    }

    /// Converts a u16 to a status code.
    ///
    /// The function validates the correctness of the supplied `u16` It must be
    /// a HTTP client error (400-499) or server error (500-599).
    ///
    /// # Example
    ///
    /// ```
    /// use dropshot::ErrorStatusCode;
    ///
    /// // 404 is a client error
    /// let ok = ErrorStatusCode::from_u16(404).unwrap();
    /// assert_eq!(ok, ErrorStatusCode::NOT_FOUND);
    ///
    /// // 555 is a server error (although it lacks a well known meaning)
    /// let _ok = ErrorStatusCode::from_u16(555).unwrap();
    ///
    /// // 200 is a status code, but not an error.
    /// let err = ErrorStatusCode::from_u16(200);
    /// assert!(err.is_err());
    ///
    /// // 99 is out of range for any status code
    /// let err = ErrorStatusCode::from_u16(99);
    /// assert!(err.is_err());
    /// ```
    pub fn from_u16(src: u16) -> Result<Self, InvalidErrorStatusCode> {
        let status = http::StatusCode::from_u16(src)?;
        Self::from_status(status).map_err(Into::into)
    }

    /// Refine this error status code into a [`ClientErrorStatusCode`].
    ///
    /// If this is a client error (4xx) status code, returns a
    /// [`ClientErrorStatusCode`] with that status. Otherwise, this method
    /// returns a [`NotAClientError`] error.
    pub fn as_client_error(
        &self,
    ) -> Result<ClientErrorStatusCode, NotAClientError> {
        if self.is_client_error() {
            Ok(ClientErrorStatusCode(self.0))
        } else {
            Err(NotAClientError(self.0))
        }
    }

    /// Check if status is within 400-499.
    pub fn is_client_error(&self) -> bool {
        self.0.is_client_error()
    }

    /// Check if status is within 500-599.
    pub fn is_server_error(&self) -> bool {
        self.0.is_server_error()
    }
}

impl_status_code_wrapper! {
    impl StatusCode for ErrorStatusCode {
        type NotAnError = NotAnError;
        type Invalid = InvalidErrorStatusCode;
    }
}

/// An HTTP 4xx client error status code
///
/// This is a refinement of the [`http::StatusCode`] type that is limited to the
/// client error status code range (400-499). It may be constructed from any
/// [`http::StatusCode`] using the `TryFrom` implementation, which fails if the
/// status is not a 4xx status code.
///
/// Alternatively, constants are provided for known error status codes, such as
/// [`ClientErrorStatusCode::BAD_REQUEST`],
/// [`ClientErrorStatusCode::NOT_FOUND`], including those in the IANA HTTP
/// Status Code Registry][iana]. Using these constants avoids the fallible
/// conversion from an [`http::StatusCode`].
///
/// [iana]: https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ClientErrorStatusCode(http::StatusCode);

impl ClientErrorStatusCode {
    // These constants are copied from the `http` crate's `StatusCode` type.
    // Should new status codes be standardized and added upstream, we should add
    // them to this list.
    error_status_code_constants! {
        /// 400 Bad Request [[RFC7231, Section
        /// 6.5.1](https://tools.ietf.org/html/rfc7231#section-6.5.1)]
        BAD_REQUEST;

        /// 401 Unauthorized [[RFC7235, Section
        /// 3.1](https://tools.ietf.org/html/rfc7235#section-3.1)]
        UNAUTHORIZED;
        /// 402 Payment Required [[RFC7231, Section
        /// 6.5.2](https://tools.ietf.org/html/rfc7231#section-6.5.2)]
        PAYMENT_REQUIRED;
        /// 403 Forbidden [[RFC7231, Section
        /// 6.5.3](https://tools.ietf.org/html/rfc7231#section-6.5.3)]
        FORBIDDEN;
        /// 404 Not Found [[RFC7231, Section
        /// 6.5.4](https://tools.ietf.org/html/rfc7231#section-6.5.4)]
        NOT_FOUND;
        /// 405 Method Not Allowed [[RFC7231, Section
        /// 6.5.5](https://tools.ietf.org/html/rfc7231#section-6.5.5)]
        METHOD_NOT_ALLOWED;
        /// 406 Not Acceptable [[RFC7231, Section
        /// 6.5.6](https://tools.ietf.org/html/rfc7231#section-6.5.6)]
        NOT_ACCEPTABLE;
        /// 407 Proxy Authentication Required [[RFC7235, Section
        /// 3.2](https://tools.ietf.org/html/rfc7235#section-3.2)]
        PROXY_AUTHENTICATION_REQUIRED;
        /// 408 Request Timeout [[RFC7231, Section
        /// 6.5.7](https://tools.ietf.org/html/rfc7231#section-6.5.7)]
        REQUEST_TIMEOUT;
        /// 409 Conflict [[RFC7231, Section
        /// 6.5.8](https://tools.ietf.org/html/rfc7231#section-6.5.8)]
        CONFLICT;
        /// 410 Gone [[RFC7231, Section
        /// 6.5.9](https://tools.ietf.org/html/rfc7231#section-6.5.9)]
        GONE;
        /// 411 Length Required [[RFC7231, Section
        /// 6.5.10](https://tools.ietf.org/html/rfc7231#section-6.5.10)]
        LENGTH_REQUIRED;
        /// 412 Precondition Failed [[RFC7232, Section
        /// 4.2](https://tools.ietf.org/html/rfc7232#section-4.2)]
        PRECONDITION_FAILED;
        /// 413 Payload Too Large [[RFC7231, Section
        /// 6.5.11](https://tools.ietf.org/html/rfc7231#section-6.5.11)]
        PAYLOAD_TOO_LARGE;
        /// 414 URI Too Long [[RFC7231, Section
        /// 6.5.12](https://tools.ietf.org/html/rfc7231#section-6.5.12)]
        URI_TOO_LONG;
        /// 415 Unsupported Media Type [[RFC7231, Section
        /// 6.5.13](https://tools.ietf.org/html/rfc7231#section-6.5.13)]
        UNSUPPORTED_MEDIA_TYPE;
        /// 416 Range Not Satisfiable [[RFC7233, Section
        /// 4.4](https://tools.ietf.org/html/rfc7233#section-4.4)]
        RANGE_NOT_SATISFIABLE;
        /// 417 Expectation Failed [[RFC7231, Section
        /// 6.5.14](https://tools.ietf.org/html/rfc7231#section-6.5.14)]
        EXPECTATION_FAILED;
        /// 418 I'm a teapot [curiously not registered by IANA but
        /// [RFC2324](https://tools.ietf.org/html/rfc2324)]
        IM_A_TEAPOT;

        /// 421 Misdirected Request [RFC7540, Section
        /// 9.1.2](https://tools.ietf.org/html/rfc7540#section-9.1.2)
        MISDIRECTED_REQUEST;
        /// 422 Unprocessable Entity
        /// [[RFC4918](https://tools.ietf.org/html/rfc4918)]
        UNPROCESSABLE_ENTITY;
        /// 423 Locked [[RFC4918](https://tools.ietf.org/html/rfc4918)]
        LOCKED;
        /// 424 Failed Dependency
        /// [[RFC4918](https://tools.ietf.org/html/rfc4918)]
        FAILED_DEPENDENCY;

        /// 426 Upgrade Required [[RFC7231, Section
        /// 6.5.15](https://tools.ietf.org/html/rfc7231#section-6.5.15)]
        UPGRADE_REQUIRED;

        /// 428 Precondition Required
        /// [[RFC6585](https://tools.ietf.org/html/rfc6585)]
        PRECONDITION_REQUIRED;
        /// 429 Too Many Requests
        /// [[RFC6585](https://tools.ietf.org/html/rfc6585)]
        TOO_MANY_REQUESTS;

        /// 431 Request Header Fields Too Large
        /// [[RFC6585](https://tools.ietf.org/html/rfc6585)]
        REQUEST_HEADER_FIELDS_TOO_LARGE;

        /// 451 Unavailable For Legal Reasons
        /// [[RFC7725](https://tools.ietf.org/html/rfc7725)]
        UNAVAILABLE_FOR_LEGAL_REASONS;
    }

    /// Converts an [`http::StatusCode`] into a [`ClientErrorStatusCode`].
    ///
    /// # Returns
    ///
    /// - [`Ok`]`(`[`ClientErrorStatusCode`]`)` if the status code is a 4xx
    ///   status code.
    /// - [`Err`]`(`[`NotAnError`]`)` if the status code is not a 4xx status
    ///   code.
    pub fn from_status(
        status: http::StatusCode,
    ) -> Result<Self, NotAClientError> {
        if status.is_client_error() {
            Ok(Self(status))
        } else {
            Err(NotAClientError(status))
        }
    }

    /// Converts a `u16` to a [`ClientErrorStatusCode`]
    ///
    /// The function validates the correctness of the supplied `u16` It must be
    /// a HTTP client error (400-499).
    ///
    /// # Example
    ///
    /// ```
    /// use dropshot::ClientErrorStatusCode;
    ///
    /// // 404 is a client error
    /// let ok = ClientErrorStatusCode::from_u16(404).unwrap();
    /// assert_eq!(ok, ClientErrorStatusCode::NOT_FOUND);
    ///
    /// // 444 is a client error (although it lacks a well known meaning)
    /// let _ok = ClientErrorStatusCode::from_u16(444).unwrap();
    ///
    /// // 500 is a status code, but not an error.
    /// let err = ClientErrorStatusCode::from_u16(200);
    /// assert!(err.is_err());
    ///
    /// // 99 is out of range for any status code
    /// let err = ClientErrorStatusCode::from_u16(99);
    /// assert!(err.is_err());
    /// ```
    #[inline]
    pub fn from_u16(src: u16) -> Result<Self, InvalidClientErrorStatusCode> {
        let status = http::StatusCode::from_u16(src)?;
        Self::from_status(status).map_err(Into::into)
    }
}

impl_status_code_wrapper! {
    impl StatusCode for ClientErrorStatusCode {
        type NotAnError = NotAClientError;
        type Invalid = InvalidClientErrorStatusCode;
    }
}

impl TryFrom<ErrorStatusCode> for ClientErrorStatusCode {
    type Error = NotAClientError;
    fn try_from(error: ErrorStatusCode) -> Result<Self, Self::Error> {
        error.as_client_error()
    }
}

impl From<ClientErrorStatusCode> for ErrorStatusCode {
    #[inline]
    fn from(error: ClientErrorStatusCode) -> Self {
        Self(error.0)
    }
}

impl TryFrom<&'_ ErrorStatusCode> for ClientErrorStatusCode {
    type Error = NotAClientError;
    fn try_from(error: &ErrorStatusCode) -> Result<Self, Self::Error> {
        error.as_client_error()
    }
}

impl From<&'_ ClientErrorStatusCode> for ErrorStatusCode {
    #[inline]
    fn from(error: &ClientErrorStatusCode) -> Self {
        Self(error.0)
    }
}

impl PartialEq<ErrorStatusCode> for ClientErrorStatusCode {
    #[inline]
    fn eq(&self, other: &ErrorStatusCode) -> bool {
        self.0 == other.0
    }
}

impl PartialEq<ClientErrorStatusCode> for ErrorStatusCode {
    #[inline]
    fn eq(&self, other: &ClientErrorStatusCode) -> bool {
        self.0 == other.0
    }
}

#[derive(Debug, thiserror::Error)]
#[error("status code {0} is not a 4xx or 5xx error")]
pub struct NotAnError(http::StatusCode);

#[derive(Debug, thiserror::Error)]
#[error("status code {0} is not a 4xx client error")]
pub struct NotAClientError(http::StatusCode);

/// A possible error value when converting an [`ErrorStatusCode`] from a `u16`
/// or `&str`.
#[derive(Debug, thiserror::Error)]
pub enum InvalidErrorStatusCode {
    /// The input was not an error (4xx or 5xx) status code.
    #[error(transparent)]
    NotAnError(#[from] NotAnError),
    /// The input was not a valid number, was less than 100, or was greater than
    /// 999.
    #[error(transparent)]
    InvalidStatus(#[from] http::status::InvalidStatusCode),
}

/// A possible error value when converting a [`ClientErrorStatusCode`] from a
/// `u16` or `&str`.
#[derive(Debug, thiserror::Error)]
pub enum InvalidClientErrorStatusCode {
    /// The input was not a client error (4xx) status code.
    #[error(transparent)]
    NotAClientError(#[from] NotAClientError),
    /// The input was not a valid number, was less than 100, or was greater than
    /// 999.
    #[error(transparent)]
    InvalidStatus(#[from] http::status::InvalidStatusCode),
}