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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
mod error;

pub use error::Error;

use lazy_static::lazy_static;
use regex::Regex;
use serde::{Deserialize, Serialize};
use smallstr::SmallString;
use std::{collections::HashMap, path::Path};

lazy_static! {
    static ref PARSER: Regex = Regex::new(r"\s*([^~!?,&(){}\[\]\s]+|[~!?,&(){}\[\]])").unwrap();
}

#[derive(Debug, Deserialize, Serialize, Hash)]
pub struct CountryRef<'a> {
    pub aliases: Option<Vec<&'a str>>,
    pub alpha2: &'a str,
    pub alpha3: &'a str,
    pub fifa: &'a str,
    pub ioc: &'a str,
    pub iso_name: &'a str,
    pub numeric: i32,
    pub official: &'a str,
    pub short: &'a str,
    pub emoji: &'a str,
    pub shortcode: &'a str,
}

impl<'a> CountryRef<'a> {
    pub fn to_owned(&self) -> Country {
        let mut alpha2 = self.alpha2.as_bytes().into_iter().copied();
        let mut alpha3 = self.alpha3.as_bytes().into_iter().copied();
        let mut fifa = self.fifa.as_bytes().into_iter().copied();
        let mut ioc = self.ioc.as_bytes().into_iter().copied();

        Country {
            aliases: self
                .aliases
                .as_ref()
                .map(|x| x.into_iter().map(|&x| x.into()).collect()),

            alpha2: [alpha2.next().unwrap_or(b' '), alpha2.next().unwrap_or(b' ')],
            alpha3: [
                alpha3.next().unwrap_or(b' '),
                alpha3.next().unwrap_or(b' '),
                alpha3.next().unwrap_or(b' '),
            ],
            fifa: [
                fifa.next().unwrap_or(b' '),
                fifa.next().unwrap_or(b' '),
                fifa.next().unwrap_or(b' '),
            ],
            ioc: [
                ioc.next().unwrap_or(b' '),
                ioc.next().unwrap_or(b' '),
                ioc.next().unwrap_or(b' '),
            ],
            iso_name: self.iso_name.into(),
            numeric: self.numeric,
            official: self.official.into(),
            short: self.short.into(),
            emoji: self.emoji.chars().next().unwrap_or(' '),
            shortcode: self.shortcode.into(),
        }
    }
}

#[derive(Debug, Deserialize, Serialize, Hash)]
pub struct Country {
    pub aliases: Option<Vec<SmallString<[u8; 23]>>>,
    pub alpha2: [u8; 2],
    pub alpha3: [u8; 3],
    pub fifa: [u8; 3],
    pub ioc: [u8; 3],
    pub iso_name: SmallString<[u8; 23]>,
    pub numeric: i32,
    pub official: SmallString<[u8; 23]>,
    pub short: SmallString<[u8; 23]>,
    pub emoji: char,
    pub shortcode: SmallString<[u8; 23]>,
}

impl Country {
    /// Get the country's alpha2.
    #[must_use]
    #[inline]
    pub fn alpha2(&self) -> &str {
        std::str::from_utf8(&self.alpha2).unwrap()
    }

    /// Get the country's alpha3.
    #[must_use]
    #[inline]
    pub fn alpha3(&self) -> &str {
        std::str::from_utf8(&self.alpha3).unwrap()
    }

    /// Get the country's fifa.
    #[must_use]
    #[inline]
    pub fn fifa(&self) -> &str {
        std::str::from_utf8(&self.fifa).unwrap()
    }

    /// Get the country's ioc.
    #[must_use]
    #[inline]
    pub fn ioc(&self) -> &str {
        std::str::from_utf8(&self.ioc).unwrap()
    }
}

impl PartialEq for Country {
    fn eq(&self, other: &Self) -> bool {
        self.numeric == other.numeric
    }
}

impl Eq for Country {}

impl PartialOrd for Country {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        self.alpha3.partial_cmp(&other.alpha3)
    }
}

impl Ord for Country {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.alpha3.cmp(&other.alpha3)
    }
}

pub struct CountryNameNormalizer {
    countries: Vec<Country>,
    keys: HashMap<String, usize>,
}

impl CountryNameNormalizer {
    pub fn new<P: AsRef<Path>>(data_path: P) -> Result<Self, Error> {
        let cfg = std::fs::read(data_path)?;
        let data: HashMap<&str, CountryRef> = toml::from_slice(&cfg)?;

        let mut countries = Vec::with_capacity(data.len());
        let mut keys = HashMap::new();

        for (_, value) in data.into_iter() {
            let idx = countries.len();
            countries.push(value.to_owned());

            Self::add(&mut keys, value.alpha2, idx);
            Self::add(&mut keys, value.alpha3, idx);
            Self::add(&mut keys, value.fifa, idx);
            Self::add(&mut keys, value.ioc, idx);
            Self::add(&mut keys, value.iso_name, idx);
            Self::add(&mut keys, value.official, idx);
            Self::add(&mut keys, value.short, idx);
            // Self::add(&mut keys, value.emoji.as_str(), idx);

            if let Some(aliases) = &value.aliases {
                for alias in aliases {
                    Self::add(&mut keys, alias, idx);
                }
            }
        }

        Ok(Self { countries, keys })
    }

    pub fn normalize_country(&self, name: &str) -> Option<&Country> {
        let key = normalize_name(name);
        let index = self.keys.get(&key)?;
        self.countries.get(*index)
    }

    fn add(keys: &mut HashMap<String, usize>, w: &str, idx: usize) {
        let s = normalize_name(w);
        if !s.is_empty() {
            keys.insert(s, idx);
        }
    }
}

fn normalize_name(name: &str) -> String {
    let name = name.to_lowercase();

    let iter = PARSER
        .captures_iter(&name)
        .map(|cap| cap.get(1).unwrap().as_str());

    let mut res = String::new();

    for w in iter {
        let w = w.trim();

        if w.is_empty() {
            continue;
        } else if w.len() == 1 {
            match w.chars().next().unwrap() {
                '&' => res.push_str("and "),
                p if p.is_ascii_punctuation() => (),
                ch => {
                    res.push(ch);
                    res.push(' ');
                }
            }
        } else {
            match w {
                "islas" | "minor" | "the" => {}
                "st." => res.push_str("saint "),
                "u.s." => {
                    res.push_str("united states ");
                }
                "u.s.a." => {
                    res.push_str("united states of america ");
                }
                x => {
                    res.push_str(x);
                    res.push(' ');
                }
            }
        }
    }

    res.pop();
    res
}