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
macro_rules! enum_status_code {
(enum $name:ident {
$($variant:ident = $num:expr, $str:expr),*
}) => {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u16)]
#[non_exhaustive]
pub enum $name {
$(
#[doc="`"]
#[doc=$str]
#[doc="`"]
$variant = $num
),*
}
impl $name {
pub fn message(&self) -> &'static str {
match self {
$(Self::$variant => $str),*
}
}
pub fn code(&self) -> u16 {
*self as u16
}
}
impl std::convert::TryFrom<u16> for $name {
type Error = ();
fn try_from(value: u16) -> Result<Self, Self::Error> {
match value {
$($num => Ok(Self::$variant)),*,
_ => Err(())
}
}
}
impl std::fmt::Display for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} {}", self.code(), self.message())
}
}
}
}
enum_status_code! {
enum StatusCode {
Continue = 100, "Continue",
SwitchingProtocols = 101, "Switching Protocols",
Ok = 200, "OK",
Created = 201, "Created",
Accepted = 202, "Accepted",
NonAuthoritativeInformation = 203, "Non-Authoritative Information",
NoContent = 204, "No Content",
ResetContent = 205, "Reset Content",
PartialContent = 206, "Partial Content",
MultipleChoices = 300, "Multiple Choices",
MovedPermanently = 301, "Moved Permanently",
Found = 302, "Found",
SeeOther = 303, "See Other",
NotModified = 304, "Not Modified",
UseProxy = 305, "Use Proxy",
TemporaryRedirect = 307, "Temporary Redirect",
BadRequest = 400, "Bad Request",
Unauthorized = 401, "Unauthorized",
PaymentRequired = 402, "Payment Required",
Forbidden = 403, "Forbidden",
NotFound = 404, "Not Found",
MethodNotAllowed = 405, "Method Not Allowed",
NotAcceptable = 406, "Not Acceptable",
ProxyAuthenticationRequired = 407, "Proxy Authentication Required",
RequestTimeout = 408, "Request Time-out",
Conflict = 409, "Conflict",
Gone = 410, "Gone",
LengthRequired = 411, "Length Required",
PreconditionFailed = 412, "Precondition Failed",
RequestEntityTooLarge = 413, "Request Entity Too Large",
RequestURITooLarge = 414, "Request-URI Too Large",
UnsupportedMediaType = 415, "Unsupported Media Type",
RequestedRangeNotSatisfiable = 416, "Requested range not satisfiable",
ExpectationFailed = 417, "Expectation Failed",
InternalServerError = 500, "Internal Server Error",
NotImplemented = 501, "Not Implemented",
BadGateway = 502, "Bad Gateway",
ServiceUnavailable = 503, "Service Unavailable",
GatewayTimeout = 504, "Gateway Time-out",
HTTPVersionNotSupported = 505, "HTTP Version not supported"
}
}