simbld_http/responses/
service.rs

1/// The above Rust code defines an enum `ResponsesServiceCodes` with associated error codes and descriptions, along with conversion and helper functions for working with these error codes.
2use 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)]
11
12pub enum ResponsesServiceCodes {
13  #[strum(props(
14    Description = "An error occurred while reading the response or data from the server"
15  ))]
16  ReadingError = 611,
17  #[strum(props(
18    Description = "A connection issue occurred, preventing successful communication with the server"
19  ))]
20  ConnectionError = 612,
21  #[strum(props(
22    Description = "The reading operation exceeded the allowed time limit, resulting in a timeout"
23  ))]
24  ReadingTimeExpired = 613,
25  #[strum(props(
26    Description = "The SSL handshake failed, potentially due to invalid certificates or incompatible protocols"
27  ))]
28  SSLHandshakeFailed = 614,
29  #[strum(props(Description = "A generic error occurred while reading the response or data"))]
30  AnotherReadingError = 615,
31  #[strum(props(
32    Description = "An anomaly was detected in the Full Body Analyzer process, likely due to unexpected input"
33  ))]
34  FBAAnomaly = 616,
35  #[strum(props(
36    Description = "An error in the implementation or logic caused the request to fail"
37  ))]
38  CodingError = 617,
39  #[strum(props(
40    Description = "The server issued a redirect response but did not provide a valid redirect URL"
41  ))]
42  RedirectWithoutRedirectURL = 618,
43  #[strum(props(
44    Description = "The DNS lookup for the specified domain failed, indicating a potential network or configuration issue"
45  ))]
46  DNSLookupFailed = 680,
47  #[strum(props(
48    Description = "The provided URL is syntactically incorrect and cannot be processed"
49  ))]
50  SyntacticallyIncorrectURL = 690,
51  #[strum(props(
52    Description = "The connection to the server was lost unexpectedly during communication"
53  ))]
54  LostConnection = 691,
55  #[strum(props(
56    Description = "The operation timed out while attempting to write data to the server"
57  ))]
58  WriteTimeout = 692,
59  #[strum(props(
60    Description = "The requested operation failed during a selection or matching process"
61  ))]
62  SelectionFailed = 693,
63  #[strum(props(
64    Description = "An error occurred while attempting to write data to the destination"
65  ))]
66  WriteError = 694,
67  #[strum(props(
68    Description = "A block header was incomplete or malformed, preventing further processing"
69  ))]
70  IncompleteBlockHeader = 695,
71  #[strum(props(
72    Description = "An unexpected error occurred, often indicative of an unforeseen issue or bug"
73  ))]
74  UnexpectedError = 699,
75}
76
77impl ToU16 for ResponsesServiceCodes {
78  fn to_u16(self) -> u16 {
79    self.into() // Conversion`Into<u16>`
80  }
81}
82
83impl FromU16 for ResponsesServiceCodes {
84  fn from_u16(code: u16) -> Option<Self> {
85    Self::try_from(code).ok() // Conversion`TryFrom<u16>`
86  }
87}
88
89impl Into<(u16, &'static str)> for ResponsesServiceCodes {
90  fn into(self) -> (u16, &'static str) {
91    let code: u16 = self.to_u16();
92    let description = self.get_str("Description").unwrap_or("No description");
93    (code, description) // Tuple
94  }
95}
96
97pub fn reading_error() -> (u16, &'static str) {
98  (611, "An error occurred while reading the response or data from the server")
99}
100
101pub fn connection_error() -> (u16, &'static str) {
102  (612, "A connection issue occurred, preventing successful communication with the server")
103}
104
105pub fn reading_time_expired() -> (u16, &'static str) {
106  (613, "The reading operation exceeded the allowed time limit, resulting in a timeout")
107}
108
109pub fn ssl_handshake_failed() -> (u16, &'static str) {
110  (
111    614,
112    "The SSL handshake failed, potentially due to invalid certificates or incompatible protocols",
113  )
114}
115
116pub fn another_reading_error() -> (u16, &'static str) {
117  (615, "A generic error occurred while reading the response or data")
118}
119
120pub fn fba_anomaly() -> (u16, &'static str) {
121  (616, "An anomaly was detected in the Full Body Analyzer process, likely due to unexpected input")
122}
123
124pub fn coding_error() -> (u16, &'static str) {
125  (617, "An error in the implementation or logic caused the request to fail")
126}
127
128pub fn redirect_without_redirect_url() -> (u16, &'static str) {
129  (618, "The server issued a redirect response but did not provide a valid redirect URL")
130}
131
132pub fn dns_lookup_failed() -> (u16, &'static str) {
133  (680, "The DNS lookup for the specified domain failed, indicating a potential network or configuration issue")
134}
135
136pub fn syntactically_incorrect_url() -> (u16, &'static str) {
137  (690, "The provided URL is syntactically incorrect and cannot be processed")
138}
139
140pub fn lost_connection() -> (u16, &'static str) {
141  (691, "The connection to the server was lost unexpectedly during communication")
142}
143
144pub fn write_timeout() -> (u16, &'static str) {
145  (692, "The operation timed out while attempting to write data to the server")
146}
147
148pub fn selection_failed() -> (u16, &'static str) {
149  (693, "The requested operation failed during a selection or matching process")
150}
151
152pub fn write_error() -> (u16, &'static str) {
153  (694, "An error occurred while attempting to write data to the destination")
154}
155
156pub fn incomplete_block_header() -> (u16, &'static str) {
157  (695, "A block header was incomplete or malformed, preventing further processing")
158}
159
160pub fn unexpected_error() -> (u16, &'static str) {
161  (699, "An unexpected error occurred, often indicative of an unforeseen issue or bug")
162}
163
164#[cfg(test)]
165mod tests {
166  use super::*;
167
168  #[test]
169  fn test_generated_function_reading_error() {
170    let response = ResponsesServiceCodes::ReadingError;
171    let (code, description) = response.into();
172    assert_eq!(code, 611);
173    assert_eq!(description, "An error occurred while reading the response or data from the server");
174  }
175
176  #[test]
177  fn test_to_u16_connection_error() {
178    let response = ResponsesServiceCodes::ConnectionError;
179    let code = response.to_u16();
180    assert_eq!(code, 612);
181  }
182
183  #[test]
184  fn test_from_u16_reading_time_expired() {
185    let response = ResponsesServiceCodes::from_u16(613);
186    assert_eq!(response, Some(ResponsesServiceCodes::ReadingTimeExpired));
187  }
188
189  #[test]
190  fn test_ssl_handshake_failed() {
191    assert_eq!
192    (ssl_handshake_failed(), (
193      614,
194      "The SSL handshake failed, potentially due to invalid certificates or incompatible protocols",
195    ));
196  }
197}