simbld_http/responses/
crawler.rs

1/// The code defines an enum `ResponsesCrawlerCodes` with associated descriptions and conversion methods in Rust.
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 ResponsesCrawlerCodes {
13  #[strum(props(Description = "Parsing error: unfinished header"))]
14  ParsingErrorUnfinishedHeader = 700,
15  #[strum(props(Description = "Parsing error: header"))]
16  ParsingErrorHeader = 710,
17  #[strum(props(Description = "Parsing error: missing HTTP code"))]
18  ParsingErrorMissingHTTPCode = 720,
19  #[strum(props(Description = "Parsing error: body"))]
20  ParsingErrorBody = 730,
21  #[strum(props(Description = "Excluded by robots.txt file"))]
22  ExcludedByRobotsTxtFile = 740,
23  #[strum(props(Description = "Robots temporarily unavailable"))]
24  RobotsTemporarilyUnavailable = 741,
25  #[strum(props(Description = "Excluded by definition of exploration space"))]
26  ExcludedByDefinitionOfExplorationSpace = 760,
27  #[strum(props(Description = "Not allowed by local exploration space"))]
28  NotAllowedByLocalExplorationSpace = 761,
29  #[strum(props(Description = "Incorrect protocol or non-standard system port"))]
30  IncorrectProtocolOrNonStandardSystemPort = 770,
31  #[strum(props(Description = "Excluded by file type exclusions"))]
32  ExcludedByFileTypeExclusions = 780,
33  #[strum(props(Description = "Invalid card - Not a physical card"))]
34  InvalidCard = 781,
35  #[strum(props(
36    Description = "Cannot disable physical card OR Print card request already requested"
37  ))]
38  CannotDisablePhysicalCard = 782,
39  #[strum(props(Description = "Invalid URL"))]
40  InvalidURL = 786,
41  #[strum(props(Description = "No index meta tag"))]
42  NoIndexMetaTag = 2004,
43  #[strum(props(Description = "Programmable redirection"))]
44  ProgrammableRedirection = 3020,
45  #[strum(props(Description = "Redirected to another URL"))]
46  RedirectedToAnotherURL = 3021,
47}
48
49impl ToU16 for ResponsesCrawlerCodes {
50  fn to_u16(self) -> u16 {
51    self.into() // Conversion`Into<u16>`
52  }
53}
54
55impl FromU16 for ResponsesCrawlerCodes {
56  fn from_u16(code: u16) -> Option<Self> {
57    Self::try_from(code).ok() // Conversion`TryFrom<u16>`
58  }
59}
60
61impl Into<(u16, &'static str)> for ResponsesCrawlerCodes {
62  fn into(self) -> (u16, &'static str) {
63    let code: u16 = self.to_u16();
64    let description = self.get_str("Description").unwrap_or("No description");
65    (code, description) // Tuple
66  }
67}
68
69pub fn parsing_error_unfinished_header() -> (u16, &'static str) {
70  (700, "Parsing error: unfinished header")
71}
72
73pub fn parsing_error_header() -> (u16, &'static str) {
74  (710, "Parsing error: header")
75}
76
77pub fn parsing_error_missing_http_code() -> (u16, &'static str) {
78  (720, "Parsing error: missing HTTP code")
79}
80
81pub fn parsing_error_body() -> (u16, &'static str) {
82  (730, "Parsing error: body")
83}
84
85pub fn excluded_by_robots_txt_file() -> (u16, &'static str) {
86  (740, "Excluded by robots.txt file")
87}
88
89pub fn robots_temporarily_unavailable() -> (u16, &'static str) {
90  (741, "Robots temporarily unavailable")
91}
92
93pub fn excluded_by_definition_of_exploration_space() -> (u16, &'static str) {
94  (760, "Excluded by definition of exploration space")
95}
96
97pub fn not_allowed_by_local_exploration_space() -> (u16, &'static str) {
98  (761, "Not allowed by local exploration space")
99}
100
101pub fn incorrect_protocol_or_non_standard_system_port() -> (u16, &'static str) {
102  (770, "Incorrect protocol or non-standard system port")
103}
104
105pub fn excluded_by_file_type_exclusions() -> (u16, &'static str) {
106  (780, "Excluded by file type exclusions")
107}
108
109pub fn invalid_card() -> (u16, &'static str) {
110  (781, "Invalid card - Not a physical card")
111}
112
113pub fn cannot_disable_physical_card() -> (u16, &'static str) {
114  (782, "Cannot disable physical card OR Print card request already requested")
115}
116
117pub fn invalid_url() -> (u16, &'static str) {
118  (786, "Invalid URL")
119}
120
121pub fn no_index_meta_tag() -> (u16, &'static str) {
122  (2004, "No index meta tag")
123}
124
125pub fn programmable_redirection() -> (u16, &'static str) {
126  (3020, "Programmable redirection")
127}
128
129pub fn redirected_to_another_url() -> (u16, &'static str) {
130  (3021, "Redirected to another URL")
131}
132
133#[cfg(test)]
134mod tests {
135  use super::*;
136
137  #[test]
138  fn test_generated_function_parsing_error_unfinished_header() {
139    let response = ResponsesCrawlerCodes::ParsingErrorUnfinishedHeader;
140    let (code, description) = response.into();
141    assert_eq!(code, 700);
142    assert_eq!(description, "Parsing error: unfinished header");
143  }
144
145  #[test]
146  fn test_to_u16_parsing_error_header() {
147    let response = ResponsesCrawlerCodes::ParsingErrorHeader;
148    let code = response.to_u16();
149    assert_eq!(code, 710);
150  }
151
152  #[test]
153  fn test_parsing_error_missing_http_code() {
154    assert_eq!(parsing_error_missing_http_code(), (720, "Parsing error: missing HTTP code"));
155  }
156
157  #[test]
158  fn test_from_u16_invalid_url() {
159    let response = ResponsesCrawlerCodes::from_u16(786);
160    assert_eq!(response, Some(ResponsesCrawlerCodes::InvalidURL));
161  }
162}