aboutsummaryrefslogtreecommitdiff
path: root/src/tlds.rs
blob: 4d8a9c3ed5647f070edc7c22de895b1f811e9edc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use std::collections::HashMap;
use std::sync::OnceLock;

/// whois server overrides from Lists.toml ("io:whois.nic.io" style entries)
#[derive(Debug, Clone, Default)]
pub struct WhoisOverrides {
    map: HashMap<String, String>,
}

impl WhoisOverrides {
    pub fn get_server(&self, tld: &str) -> Option<&str> {
        self.map.get(&tld.to_lowercase()).map(|s| s.as_str())
    }
}

struct NamedList {
    name: String,
    tlds: Vec<String>,
}

struct ParsedLists {
    lists: Vec<NamedList>,
    whois_overrides: WhoisOverrides,
}

// 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()))
    } else {
        (entry.to_string(), None)
    }
}

// parse more 
fn parse_list(entries: &[toml::Value], overrides: &mut HashMap<String, String>) -> Vec<String> {
    entries
        .iter()
        .filter_map(|v| v.as_str())
        .map(|entry| {
            let (tld, server) = parse_entry(entry);
            if let Some(s) = server {
                overrides.insert(tld.to_lowercase(), s);
            }
            tld
        })
        .collect()
}

static PARSED_LISTS: OnceLock<ParsedLists> = OnceLock::new();

fn parsed_lists() -> &'static ParsedLists {
    PARSED_LISTS.get_or_init(|| {
        let raw: toml::Value =
            toml::from_str(include_str!("../Lists.toml")).expect("Lists.toml must be valid TOML");

        let table = raw.as_table().expect("Lists.toml must be a TOML table");

        // 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();
        let mut lists = Vec::new();

        for name in &ordered_names {
            if let Some(toml::Value::Array(arr)) = table.get(*name) {
                let tlds = parse_list(arr, &mut overrides);
                lists.push(NamedList {
                    name: name.to_string(),
                    tlds,
                });
            }
        }

        ParsedLists {
            lists,
            whois_overrides: WhoisOverrides { map: overrides },
        }
    })
}


pub fn list_names() -> Vec<&'static str> {
    parsed_lists()
        .lists
        .iter()
        .map(|l| l.name.as_str())
        .collect()
}

pub fn default_list_name() -> &'static str {
    list_names().first().copied().unwrap_or("standard")
}

pub fn get_tlds(name: &str) -> Option<Vec<&'static str>> {
    let lower = name.to_lowercase();
    parsed_lists()
        .lists
        .iter()
        .find(|l| l.name == lower)
        .map(|l| l.tlds.iter().map(String::as_str).collect())
}


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())
}


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());
    // 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) {
            if !result.contains(&found) {
                result.push(found);
            }
        }
    }
    // then add the rest lol
    for tld in &tlds {
        if !result.contains(tld) {
            result.push(tld);
        }
    }
    result
}