Debian Codegraph @

src/response/values/nr_type.rs

raw
1 //! Notice/Remark Values.
2
3 use super::EnumDisplay;
4 use strum::{EnumIter, EnumString};
5
6 /// IANA registered Notice/Remark types.
7 #[derive(PartialEq, Eq, Debug, EnumIter, EnumString, EnumDisplay)]
8 pub enum NrType {
9 #[strum(serialize = "result set truncated due to authorization")]
10 ResultSetTruncatedDueToAuthorization,
11 #[strum(serialize = "result set truncated due to excessive load")]
12 ResultSetTruncatedDueToExcessiveLoad,
13 #[strum(serialize = "result set truncated due to unexplainable reasons")]
14 ResultSetTruncatedDueToUnexplainableReasons,
15 #[strum(serialize = "object truncated due to authorization")]
16 ObjectTruncatedDueToAuthorization,
17 #[strum(serialize = "object truncated due to excessive load")]
18 ObjectTruncatedDueToExcessiveLoad,
19 #[strum(serialize = "object truncated due to unexplainable reasons")]
20 ObjectTruncatedDueToUnexplainableReasons,
21 #[strum(serialize = "object redacted due to authorization")]
22 ObjectRedactedDueToAuthorization,
23 }
24
25 #[cfg(all(test, feature = "iana_registry_tests"))]
26 mod tests {
27 use super::*;
28 use strum::IntoEnumIterator;
29
30 const IANA_REGISTRY_URL: &str =
31 "https://www.iana.org/assignments/rdap-json-values/rdap-json-values.xml";
32
33 #[test]
34 fn iana_notice_remark_types_match_enum() {
35 let client = reqwest::blocking::Client::builder()
36 .user_agent("icann-rdap-iana-test")
37 .build()
38 .expect("failed to build reqwest client");
39 let resp = client
40 .get(IANA_REGISTRY_URL)
41 .header("Accept", "application/xml")
42 .send()
43 .expect("failed to fetch IANA RDAP JSON values registry");
44 let body = resp.text().expect("failed to read IANA registry body");
45
46 let mut iana_values: std::collections::HashSet<String> = std::collections::HashSet::new();
47 let records: Vec<&str> = body.split("<record").skip(1).collect();
48 for record in records {
49 let type_start = match record.find("<type>") {
50 Some(pos) => pos + 6,
51 None => continue,
52 };
53 let type_end = match record[type_start..].find("</type>") {
54 Some(len) => len,
55 None => continue,
56 };
57 let type_val = record[type_start..type_start + type_end].trim().to_string();
58 if type_val != "notice and remark type" {
59 continue;
60 }
61 let value_start = match record.find("<value>") {
62 Some(pos) => pos + 7,
63 None => continue,
64 };
65 let value_end = match record[value_start..].find("</value>") {
66 Some(len) => len,
67 None => continue,
68 };
69 let value = record[value_start..value_start + value_end]
70 .trim()
71 .to_string();
72 iana_values.insert(value);
73 }
74
75 let mut enum_values: std::collections::HashSet<String> = std::collections::HashSet::new();
76 for variant in NrType::iter() {
77 enum_values.insert(variant.to_string());
78 }
79
80 let mut missing = Vec::new();
81 for value in &iana_values {
82 if !enum_values.contains(value) {
83 missing.push(value.clone());
84 }
85 }
86
87 let mut extra = Vec::new();
88 for value in &enum_values {
89 if !iana_values.contains(value) {
90 extra.push(value.clone());
91 }
92 }
93
94 missing.sort();
95 extra.sort();
96
97 if !missing.is_empty() || !extra.is_empty() {
98 let mut msg = String::from("NrType enum does not match IANA registry:\n");
99 if !missing.is_empty() {
100 msg.push_str("Missing from enum:\n");
101 for v in &missing {
102 msg.push_str(&format!(" - {v}\n"));
103 }
104 }
105 if !extra.is_empty() {
106 msg.push_str("Not in IANA registry:\n");
107 for v in &extra {
108 msg.push_str(&format!(" - {v}\n"));
109 }
110 }
111 panic!("{msg}");
112 }
113 }
114 }