Debian Codegraph @

src/response/values/entity_role.rs

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