http_type/status_code/impl.rs
1use crate::*;
2use std::{
3 fmt::{self, Display},
4 str::FromStr,
5};
6
7/// The `StatusCode` enum represents the HTTP status codes.
8///
9/// It maps common HTTP status codes to their respective meanings. It provides methods to retrieve
10/// the corresponding numeric code as well as the associated status text. Additionally, it implements
11/// conversion from a string representation of the status code.
12///
13/// # Variants
14/// - `Ok`: HTTP status 200, indicating a successful request.
15/// - `Created`: HTTP status 201, indicating that the request was successful and resulted in a resource creation.
16/// - `NoContent`: HTTP status 204, indicating that the request was successful, but there is no content to return.
17/// - `BadRequest`: HTTP status 400, indicating a bad request, often due to incorrect syntax or invalid data.
18/// - `Unauthorized`: HTTP status 401, indicating that authentication is required and has failed or not been provided.
19/// - `Forbidden`: HTTP status 403, indicating that the server understands the request but refuses to authorize it.
20/// - `NotFound`: HTTP status 404, indicating that the requested resource could not be found.
21/// - `InternalServerError`: HTTP status 500, indicating that the server encountered an internal error.
22/// - `NotImplemented`: HTTP status 501, indicating that the server does not support the functionality required to fulfill the request.
23/// - `BadGateway`: HTTP status 502, indicating that the server, while acting as a gateway or proxy, received an invalid response from an upstream server.
24/// - `Unknown`: A default variant for unrecognized or undefined status codes.
25impl StatusCode {
26 /// Returns the numeric HTTP status code associated with this status code variant.
27 ///
28 /// This method returns the corresponding HTTP numeric status code based on the `StatusCode` variant.
29 /// For example:
30 /// - `Self::Ok` returns 200.
31 /// - `Self::BadRequest` returns 400.
32 /// - `Self::Unknown` returns 0 (the default for unrecognized status codes).
33 ///
34 /// # Parameters
35 /// - `&self`: A reference to the `StatusCode` enum instance. This represents the specific variant of the `StatusCode` enum that the method is called on.
36 ///
37 /// # Return Value
38 /// - `u16`: The numeric HTTP status code associated with the `StatusCode` variant. For example:
39 /// - `Self::Ok` returns `200`.
40 /// - `Self::BadRequest` returns `400`.
41 /// - `Self::Unknown` returns `0`.
42 #[inline]
43 pub fn code(&self) -> StatusCodeUsize {
44 match self {
45 Self::Ok => 200,
46 Self::Created => 201,
47 Self::NoContent => 204,
48 Self::BadRequest => 400,
49 Self::Unauthorized => 401,
50 Self::Forbidden => 403,
51 Self::NotFound => 404,
52 Self::InternalServerError => 500,
53 Self::NotImplemented => 501,
54 Self::BadGateway => 502,
55 Self::Unknown => 0,
56 }
57 }
58
59 /// Converts an HTTP status code to its corresponding textual description.
60 ///
61 /// This method matches a given numeric HTTP status code and returns the corresponding
62 /// textual representation defined in the `StatusCode` enum.
63 ///
64 /// # Parameters
65 /// - `code`: A `usize` representing the HTTP status code to convert.
66 ///
67 /// # Return Value
68 /// - `String`: A string representing the textual description of the HTTP status code.
69 /// For example:
70 /// - `200` returns `"OK"`.
71 /// - `404` returns `"Not Found"`.
72 /// - Unrecognized codes return `"Unknown"`.
73 #[inline]
74 pub fn phrase(code: usize) -> String {
75 match code {
76 200 => Self::Ok.to_string(),
77 201 => Self::Created.to_string(),
78 204 => Self::NoContent.to_string(),
79 400 => Self::BadRequest.to_string(),
80 401 => Self::Unauthorized.to_string(),
81 403 => Self::Forbidden.to_string(),
82 404 => Self::NotFound.to_string(),
83 500 => Self::InternalServerError.to_string(),
84 501 => Self::NotImplemented.to_string(),
85 502 => Self::BadGateway.to_string(),
86 _ => Self::Unknown.to_string(),
87 }
88 }
89
90 #[inline]
91 pub fn same(&self, code_str: &str) -> bool {
92 self.code().to_string() == code_str || self.to_string() == code_str
93 }
94}
95
96impl Display for StatusCode {
97 #[inline]
98 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
99 let res: &str = match self {
100 Self::Ok => "OK",
101 Self::Created => "Created",
102 Self::NoContent => "No Content",
103 Self::BadRequest => "Bad Request",
104 Self::Unauthorized => "Unauthorized",
105 Self::Forbidden => "Forbidden",
106 Self::NotFound => "Not Found",
107 Self::InternalServerError => "Internal Server Error",
108 Self::NotImplemented => "Not Implemented",
109 Self::BadGateway => "Bad Gateway",
110 Self::Unknown => "Unknown",
111 };
112 write!(f, "{}", res)
113 }
114}
115
116impl FromStr for StatusCode {
117 type Err = ();
118
119 #[inline]
120 fn from_str(code_str: &str) -> Result<Self, Self::Err> {
121 match code_str {
122 _code_str if Self::Ok.same(_code_str) => Ok(Self::Ok),
123 _code_str if Self::Created.same(_code_str) => Ok(Self::Created),
124 _code_str if Self::NoContent.same(_code_str) => Ok(Self::NoContent),
125 _code_str if Self::BadRequest.same(_code_str) => Ok(Self::BadRequest),
126 _code_str if Self::Unauthorized.same(_code_str) => Ok(Self::Unauthorized),
127 _code_str if Self::Forbidden.same(_code_str) => Ok(Self::Forbidden),
128 _code_str if Self::NotFound.same(_code_str) => Ok(Self::NotFound),
129 _code_str if Self::InternalServerError.same(_code_str) => Ok(Self::InternalServerError),
130 _code_str if Self::NotImplemented.same(_code_str) => Ok(Self::NotImplemented),
131 _code_str if Self::BadGateway.same(_code_str) => Ok(Self::BadGateway),
132 _ => Ok(Self::Unknown),
133 }
134 }
135}
136
137impl Default for StatusCode {
138 #[inline]
139 fn default() -> Self {
140 Self::Ok
141 }
142}