1use crate::helpers::{from_u16_helper::FromU16, to_u16_helper::ToU16};
3use num_enum::{IntoPrimitive, TryFromPrimitive};
4use strum::EnumProperty;
5use strum_macros::{Display, EnumIter, EnumProperty};
6
7#[derive(
8 Display, IntoPrimitive, TryFromPrimitive, EnumProperty, EnumIter, Debug, Copy, Clone, PartialEq,
9)]
10#[repr(u16)]
11pub enum ResponsesInformationalCodes {
12 #[strum(props(
13 Description = "The server has received the initial part of the request, the headers, and asks the client to continue request, proceed to send the body of the request, a POST request"
14 ))]
15 ContinueRequest = 100,
16 #[strum(props(
17 Description = "The server is complying with a request to switch protocols, used in WebSocket connections"
18 ))]
19 SwitchingProtocols = 101,
20 #[strum(props(
21 Description = "Indicates the server is processing the request but has not yet finished, used to prevent timeout errors in asynchronous operations, webdav RFC 2518"
22 ))]
23 Processing = 102,
24 #[strum(props(
25 Description = "Experimental: The server provides preliminary hints to the client, such as preloading resources while the final response is being prepared"
26 ))]
27 EarlyHints = 103,
28 #[strum(props(
29 Description = "The connection was forcibly closed by a peer, possibly due to a protocol error, a timeout, or a network issue"
30 ))]
31 ConnectionResetByPeer = 104,
32 #[strum(props(
33 Description = "The server could not resolve the domain name provided in the request, indicating a DNS lookup failure, The requested hostname cannot be resolved to an IP address"
34 ))]
35 NameNotResolved = 105,
36 #[strum(props(
37 Description = "The server did not provide a response, possibly due to a timeout or a connection issue, The server didn’t send any response within the timeout period. This status code is not specified in any RFCs, but it is used in some scenarios to indicate that the server closed the connection without sending any response"
38 ))]
39 NoResponse = 106,
40 #[strum(props(
41 Description = "The server indicates that the client should retry the request with appropriate changes or additional information, new or different credentials, use a different protocol or in a different location"
42 ))]
43 RetryWith = 107,
44 #[strum(props(
45 Description = "The response returned by the server is stale and should be revalidated, indicating that the cached response is outdated or expired"
46 ))]
47 ResponseIsStale = 108,
48 #[strum(props(
49 Description = "The server attempted to validate a cached response but failed, indicating the cached response is invalid or expired"
50 ))]
51 RevalidationFailed = 109,
52}
53
54impl ToU16 for ResponsesInformationalCodes {
55 fn to_u16(self) -> u16 {
56 self.into() }
58}
59
60impl FromU16 for ResponsesInformationalCodes {
61 fn from_u16(code: u16) -> Option<Self> {
62 Self::try_from(code).ok() }
64}
65
66impl Into<(u16, &'static str)> for ResponsesInformationalCodes {
67 fn into(self) -> (u16, &'static str) {
68 let code: u16 = self.to_u16();
69 let description = self.get_str("Description").unwrap_or("No description");
70 (code, description) }
72}
73
74pub fn continue_request() -> (u16, &'static str) {
75 (100, "The server has received the initial part of the request, the headers, and asks the client to continue request, proceed to send the body of the request, a POST request")
76}
77
78pub fn switching_protocols() -> (u16, &'static str) {
79 (101, "The server is complying with a request to switch protocols, used in WebSocket connections")
80}
81
82pub fn processing() -> (u16, &'static str) {
83 (102, "Indicates the server is processing the request but has not yet finished, used to prevent timeout errors in asynchronous operations, webdav RFC 2518")
84}
85
86pub fn early_hints() -> (u16, &'static str) {
87 (103, "Experimental: The server provides preliminary hints to the client, such as preloading resources while the final response is being prepared")
88}
89
90pub fn connection_reset_by_peer() -> (u16, &'static str) {
91 (104, "The connection was forcibly closed by a peer, possibly due to a protocol error, a timeout, or a network issue")
92}
93
94pub fn name_not_resolved() -> (u16, &'static str) {
95 (105, "The server could not resolve the domain name provided in the request, indicating a DNS lookup failure, The requested hostname cannot be resolved to an IP address")
96}
97
98pub fn no_response() -> (u16, &'static str) {
99 (106, "The server did not provide a response, possibly due to a timeout or a connection issue, The server didn’t send any response within the timeout period. This status code is not specified in any RFCs, but it is used in some scenarios to indicate that the server closed the connection without sending any response")
100}
101
102pub fn retry_with() -> (u16, &'static str) {
103 (107, "The server indicates that the client should retry the request with appropriate changes or additional information, new or different credentials, use a different protocol or in a different location")
104}
105
106pub fn response_is_stale() -> (u16, &'static str) {
107 (108, "The response returned by the server is stale and should be revalidated, indicating that the cached response is outdated or expired")
108}
109
110pub fn revalidation_failed() -> (u16, &'static str) {
111 (109, "The server attempted to validate a cached response but failed, indicating the cached response is invalid or expired")
112}
113
114#[cfg(test)]
115mod tests {
116 use super::*;
117
118 #[test]
119 fn test_generated_function_continue_request() {
120 let response = ResponsesInformationalCodes::ContinueRequest;
121 let (code, description) = response.into();
122 assert_eq!(code, 100);
123 assert_eq!(description, "The server has received the initial part of the request, the headers, and asks the client to continue request, proceed to send the body of the request, a POST request");
124 }
125
126 #[test]
127 fn test_to_16_switching_protocols() {
128 let response = ResponsesInformationalCodes::SwitchingProtocols;
129 let code = response.to_u16();
130 assert_eq!(code, 101);
131 }
132
133 #[test]
134 fn test_early_hints() {
135 assert_eq!(early_hints(), (103, "Experimental: The server provides preliminary hints to the client, such as preloading resources while the final response is being prepared"))
136 }
137
138 #[test]
139 fn test_from_u16_processing() {
140 let response = ResponsesInformationalCodes::from_u16(102);
141 assert_eq!(response, Some(ResponsesInformationalCodes::Processing));
142 }
143}