diff options
Diffstat (limited to 'src/tlds.rs')
| -rw-r--r-- | src/tlds.rs | 63 |
1 files changed, 8 insertions, 55 deletions
diff --git a/src/tlds.rs b/src/tlds.rs index 95697d0..4d8a9c3 100644 --- a/src/tlds.rs +++ b/src/tlds.rs @@ -13,7 +13,6 @@ impl WhoisOverrides { } } -/// a named TLD list from Lists.toml struct NamedList { name: String, tlds: Vec<String>, @@ -24,7 +23,7 @@ struct ParsedLists { whois_overrides: WhoisOverrides, } -/// parse a single entry: "tld" or "tld:whois_server" +// parse "tld" or "tld:whois_server" fn parse_entry(entry: &str) -> (String, Option<String>) { if let Some(pos) = entry.find(':') { (entry[..pos].to_string(), Some(entry[pos + 1..].to_string())) @@ -33,7 +32,7 @@ fn parse_entry(entry: &str) -> (String, Option<String>) { } } -/// parse entries, pull out TLD names and whois overrides +// parse more fn parse_list(entries: &[toml::Value], overrides: &mut HashMap<String, String>) -> Vec<String> { entries .iter() @@ -57,7 +56,7 @@ fn parsed_lists() -> &'static ParsedLists { let table = raw.as_table().expect("Lists.toml must be a TOML table"); - // Build list names in the order build.rs discovered them + // Build list names in the order build.rs found em let ordered_names: Vec<&str> = env!("HOARDOM_LIST_NAMES").split(',').collect(); let mut overrides = HashMap::new(); @@ -80,7 +79,7 @@ fn parsed_lists() -> &'static ParsedLists { }) } -/// list names from Lists.toml, in order + pub fn list_names() -> Vec<&'static str> { parsed_lists() .lists @@ -89,12 +88,10 @@ pub fn list_names() -> Vec<&'static str> { .collect() } -/// first list name (the default) pub fn default_list_name() -> &'static str { list_names().first().copied().unwrap_or("standard") } -/// get TLDs for a list name (case insensitive), None if not found pub fn get_tlds(name: &str) -> Option<Vec<&'static str>> { let lower = name.to_lowercase(); parsed_lists() @@ -104,19 +101,19 @@ pub fn get_tlds(name: &str) -> Option<Vec<&'static str>> { .map(|l| l.tlds.iter().map(String::as_str).collect()) } -/// get TLDs for a list name, falls back to default if not found + pub fn get_tlds_or_default(name: &str) -> Vec<&'static str> { get_tlds(name).unwrap_or_else(|| get_tlds(default_list_name()).unwrap_or_default()) } -/// the builtin whois overrides from Lists.toml + pub fn whois_overrides() -> &'static WhoisOverrides { &parsed_lists().whois_overrides } pub fn apply_top_tlds(tlds: Vec<&'static str>, top: &[String]) -> Vec<&'static str> { let mut result: Vec<&'static str> = Vec::with_capacity(tlds.len()); - // first add the top ones in the order specified + // add the top ones in the order specified for t in top { let lower = t.to_lowercase(); if let Some(&found) = tlds.iter().find(|&&tld| tld.to_lowercase() == lower) { @@ -125,7 +122,7 @@ pub fn apply_top_tlds(tlds: Vec<&'static str>, top: &[String]) -> Vec<&'static s } } } - // then add the rest + // then add the rest lol for tld in &tlds { if !result.contains(tld) { result.push(tld); @@ -133,47 +130,3 @@ pub fn apply_top_tlds(tlds: Vec<&'static str>, top: &[String]) -> Vec<&'static s } result } - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_parse_entry_bare() { - let (tld, server) = parse_entry("com"); - assert_eq!(tld, "com"); - assert_eq!(server, None); - } - - #[test] - fn test_parse_entry_with_override() { - let (tld, server) = parse_entry("io:whois.nic.io"); - assert_eq!(tld, "io"); - assert_eq!(server, Some("whois.nic.io".to_string())); - } - - #[test] - fn test_whois_overrides_populated() { - let overrides = whois_overrides(); - // io should have an override since our Lists.toml has "io:whois.nic.io" - assert!(overrides.get_server("io").is_some()); - // com should not (it has RDAP) - assert!(overrides.get_server("com").is_none()); - } - - #[test] - fn test_top_tlds_reorder() { - let tlds = vec!["com", "net", "org", "ch", "de"]; - let top = vec!["ch".to_string(), "de".to_string()]; - let result = apply_top_tlds(tlds, &top); - assert_eq!(result, vec!["ch", "de", "com", "net", "org"]); - } - - #[test] - fn test_top_tlds_missing_ignored() { - let tlds = vec!["com", "net"]; - let top = vec!["swiss".to_string()]; - let result = apply_top_tlds(tlds, &top); - assert_eq!(result, vec!["com", "net"]); - } -} |
