rust_integration_services/http/
http_status.rs1use crate::utils::{error::Error, result::ResultDyn};
2
3#[repr(u16)]
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum HttpStatus {
6 Continue = 100,
8 SwitchingProtocols = 101,
9 EarlyHints = 103,
10
11 Ok = 200,
13 Created = 201,
14 Accepted = 202,
15 NoContent = 204,
16 PartialContent = 206,
17
18 MovedPermanently = 301,
20 Found = 302,
21 SeeOther = 303,
22 NotModified = 304,
23 TemporaryRedirect = 307,
24 PermanentRedirect = 308,
25
26 BadRequest = 400,
28 Unauthorized = 401,
29 Forbidden = 403,
30 NotFound = 404,
31 MethodNotAllowed = 405,
32
33 InternalServerError = 500,
35 NotImplemented = 501,
36 BadGateway = 502,
37 ServiceUnavailable = 503,
38 GatewayTimeout = 504,
39}
40
41impl HttpStatus {
42 pub fn code(self) -> u16 {
43 self as u16
44 }
45
46 pub fn text(self) -> &'static str {
47 match self {
48 HttpStatus::Continue => "Continue",
50 HttpStatus::SwitchingProtocols => "Switching Protocols",
51 HttpStatus::EarlyHints => "Early Hints",
52
53 HttpStatus::Ok => "OK",
55 HttpStatus::Created => "Created",
56 HttpStatus::Accepted => "Accepted",
57 HttpStatus::NoContent => "No Content",
58 HttpStatus::PartialContent => "Partial Content",
59
60 HttpStatus::MovedPermanently => "Moved Permanently",
62 HttpStatus::Found => "Found",
63 HttpStatus::SeeOther => "See Other",
64 HttpStatus::NotModified => "Not Modified",
65 HttpStatus::TemporaryRedirect => "Temporary Redirect",
66 HttpStatus::PermanentRedirect => "Permanent Redirect",
67
68 HttpStatus::BadRequest => "Bad Request",
70 HttpStatus::Unauthorized => "Unauthorized",
71 HttpStatus::Forbidden => "Forbidden",
72 HttpStatus::NotFound => "Not Found",
73 HttpStatus::MethodNotAllowed => "Method Not Allowed",
74
75 HttpStatus::InternalServerError => "Internal Server Error",
77 HttpStatus::NotImplemented => "Not Implemented",
78 HttpStatus::BadGateway => "Bad Gateway",
79 HttpStatus::ServiceUnavailable => "Service Unavailable",
80 HttpStatus::GatewayTimeout => "Gateway Timeout",
81 }
82 }
83
84 pub fn from_code(code: u16) -> ResultDyn<Self> {
85 match code {
86 100 => Ok(HttpStatus::Continue),
87 101 => Ok(HttpStatus::SwitchingProtocols),
88 103 => Ok(HttpStatus::EarlyHints),
89
90 200 => Ok(HttpStatus::Ok),
91 201 => Ok(HttpStatus::Created),
92 202 => Ok(HttpStatus::Accepted),
93 204 => Ok(HttpStatus::NoContent),
94 206 => Ok(HttpStatus::PartialContent),
95
96 301 => Ok(HttpStatus::MovedPermanently),
97 302 => Ok(HttpStatus::Found),
98 303 => Ok(HttpStatus::SeeOther),
99 304 => Ok(HttpStatus::NotModified),
100 307 => Ok(HttpStatus::TemporaryRedirect),
101 308 => Ok(HttpStatus::PermanentRedirect),
102
103 400 => Ok(HttpStatus::BadRequest),
104 401 => Ok(HttpStatus::Unauthorized),
105 403 => Ok(HttpStatus::Forbidden),
106 404 => Ok(HttpStatus::NotFound),
107 405 => Ok(HttpStatus::MethodNotAllowed),
108
109 500 => Ok(HttpStatus::InternalServerError),
110 501 => Ok(HttpStatus::NotImplemented),
111 502 => Ok(HttpStatus::BadGateway),
112 503 => Ok(HttpStatus::ServiceUnavailable),
113 504 => Ok(HttpStatus::GatewayTimeout),
114
115 _ => Err(Box::new(Error::std_io("Invalid HTTP status code"))),
116 }
117 }
118}