status_code/
lib.rs

1pub mod statuses {
2
3    pub fn status(status: u16) -> u16 {
4        let code_list = [100, 101, 102, 200, 201, 202, 203, 204, 205, 206, 207, 208, 226, 300, 301, 302, 303, 304, 305, 306, 307, 308, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 421, 422, 423, 424, 425, 426, 428, 429, 431, 451, 500, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510, 511];
5        let is_valid = code_list.contains(&status);
6        if is_valid{
7            status
8        }else{
9            panic!("invalid status code:{}", &status);
10        }
11    }
12
13    pub fn code(code: u16) -> &'static str {
14        match code {
15            100 => "Continue",
16            101 => "Switching Protocols",
17            102 => "Processing",
18            200 => "OK",
19            201 => "Created",
20            202 => "Accepted",
21            203 => "Non-Authoritative Information",
22            204 => "No Content",
23            205 => "Reset Content",
24            206 => "Partial Content",
25            207 => "Multi-Status",
26            208 => "Already Reported",
27            226 => "IM Used",
28            300 => "Multiple Choices",
29            301 => "Moved Permanently",
30            302 => "Found",
31            303 => "See Other",
32            304 => "Not Modified",
33            305 => "Use Proxy",
34            306 => "(Unused)",
35            307 => "Temporary Redirect",
36            308 => "Permanent Redirect",
37            400 => "Bad Request",
38            401 => "Unauthorized",
39            402 => "Payment Required",
40            403 => "Forbidden",
41            404 => "Not Found",
42            405 => "Method Not Allowed",
43            406 => "Not Acceptable",
44            407 => "Proxy Authentication Required",
45            408 => "Request Timeout",
46            409 => "Conflict",
47            410 => "Gone",
48            411 => "Length Required",
49            412 => "Precondition Failed",
50            413 => "Payload Too Large",
51            414 => "URI Too Long",
52            415 => "Unsupported Media Type",
53            416 => "Range Not Satisfiable",
54            417 => "Expectation Failed",
55            418 => "I'm a teapot",
56            421 => "Misdirected Request",
57            422 => "Unprocessable Entity",
58            423 => "Locked",
59            424 => "Failed Dependency",
60            425 => "Unordered Collection",
61            426 => "Upgrade Required",
62            428 => "Precondition Required",
63            429 => "Too Many Requests",
64            431 => "Request Header Fields Too Large",
65            451 => "Unavailable For Legal Reasons",
66            500 => "Internal Server Error",
67            501 => "Not Implemented",
68            502 => "Bad Gateway",
69            503 => "Service Unavailable",
70            504 => "Gateway Timeout",
71            505 => "HTTP Version Not Supported",
72            506 => "Variant Also Negotiates",
73            507 => "Insufficient Storage",
74            508 => "Loop Detected",
75            509 => "Bandwidth Limit Exceeded",
76            510 => "Not Extended",
77            511 => "Network Authentication Required",
78            _ => panic!("invalid status code:{}", &code),
79        }
80    }
81
82    pub fn message(message: &str) -> u16 {
83        match message {
84            "Continue" => 100,
85            "continue" => 100,
86            "Switching Protocols" => 101,
87            "switching protocols" => 101,
88            "Processing" => 102,
89            "processing" => 102,
90            "OK" => 200,
91            "ok" => 200,
92            "Created" => 201,
93            "created" => 201,
94            "Accepted" => 202,
95            "accepted" => 202,
96            "Non-Authoritative Information" => 203,
97            "non-authoritative information" => 203,
98            "No Content" => 204,
99            "no content" => 204,
100            "Reset Content" => 205,
101            "reset content" => 205,
102            "Partial Content" => 206,
103            "partial content" => 206,
104            "Multi-Status" => 207,
105            "multi-status" => 207,
106            "Already Reported" => 208,
107            "already reported" => 208,
108            "IM Used" => 226,
109            "im used" => 226,
110            "Multiple Choices" => 300,
111            "multiple choices" => 300,
112            "Moved Permanently" => 301,
113            "moved permanently" => 301,
114            "Found" => 302,
115            "found" => 302,
116            "See Other" => 303,
117            "see other" => 303,
118            "Not Modified" => 304,
119            "not modified" => 304,
120            "Use Proxy" => 305,
121            "use proxy" => 305,
122            "(Unused)" => 306,
123            "(unused)" => 306,
124            "Temporary Redirect" => 307,
125            "temporary redirect" => 307,
126            "Permanent Redirect" => 308,
127            "permanent redirect" => 308,
128            "Bad Request" => 400,
129            "bad request" => 400,
130            "Unauthorized" => 401,
131            "unauthorized" => 401,
132            "Payment Required" => 402,
133            "payment required" => 402,
134            "Forbidden" => 403,
135            "forbidden" => 403,
136            "Not Found" => 404,
137            "not found" => 404,
138            "Method Not Allowed" => 405,
139            "method not allowed" => 405,
140            "Not Acceptable" => 406,
141            "not acceptable" => 406,
142            "Proxy Authentication Required" => 407,
143            "proxy authentication required" => 407,
144            "Request Timeout" => 408,
145            "request timeout" => 408,
146            "Conflict" => 409,
147            "conflict" => 409,
148            "Gone" => 410,
149            "gone" => 410,
150            "Length Required" => 411,
151            "length required" => 411,
152            "Precondition Failed" => 412,
153            "precondition failed" => 412,
154            "Payload Too Large" => 413,
155            "payload too large" => 413,
156            "URI Too Long" => 414,
157            "uri too long" => 414,
158            "Unsupported Media Type" => 415,
159            "unsupported media type" => 415,
160            "Range Not Satisfiable" => 416,
161            "range not satisfiable" => 416,
162            "Expectation Failed" => 417,
163            "expectation failed" => 417,
164            "I'm a teapot" => 418,
165            "i'm a teapot" => 418,
166            "Misdirected Request" => 421,
167            "misdirected request" => 421,
168            "Unprocessable Entity" => 422,
169            "unprocessable entity" => 422,
170            "Locked" => 423,
171            "locked" => 423,
172            "Failed Dependency" => 424,
173            "failed dependency" => 424,
174            "Unordered Collection" => 425,
175            "unordered collection" => 425,
176            "Upgrade Required" => 426,
177            "upgrade required" => 426,
178            "Precondition Required" => 428,
179            "precondition required" => 428,
180            "Too Many Requests" => 429,
181            "too many requests" => 429,
182            "Request Header Fields Too Large" => 431,
183            "request header fields too large" => 431,
184            "Unavailable For Legal Reasons" => 451,
185            "unavailable for legal reasons" => 451,
186            "Internal Server Error" => 500,
187            "internal server error" => 500,
188            "Not Implemented" => 501,
189            "not implemented" => 501,
190            "Bad Gateway" => 502,
191            "bad gateway" => 502,
192            "Service Unavailable" => 503,
193            "service unavailable" => 503,
194            "Gateway Timeout" => 504,
195            "gateway timeout" => 504,
196            "HTTP Version Not Supported" => 505,
197            "http version not supported" => 505,
198            "Variant Also Negotiates" => 506,
199            "variant also negotiates" => 506,
200            "Insufficient Storage" => 507,
201            "insufficient storage" => 507,
202            "Loop Detected" => 508,
203            "loop detected" => 508,
204            "Bandwidth Limit Exceeded" => 509,
205            "bandwidth limit exceeded" => 509,
206            "Not Extended" => 510,
207            "not extended" => 510,
208            "Network Authentication Required" => 511,
209            "network authentication required" => 511,
210            _ => panic!("invalid status message:{}", &message),
211        }
212    }
213
214    pub fn redirect(code: u16) -> bool {
215        match code {
216            300 => true,
217            301 => true,
218            302 => true,
219            303 => true,
220            305 => true,
221            307 => true,
222            308 => true,
223            _ => false,
224        }
225    }
226
227    pub fn empty(code: u16) -> bool {
228        let valid = status(code);
229        match valid {
230            204 => true,
231            205 => true,
232            304 => true,
233            _ => false,
234        }
235    }
236
237    pub fn retry(code: u16) -> bool {
238        let valid = status(code);
239        match valid {
240            502 => true,
241            503 => true,
242            504 => true,
243            _ => false,
244        }
245    }
246}