Skip to main content

dpp_rules/common/
country.rs

1//! ISO 3166-1 alpha-2 country code validation.
2
3/// Officially assigned ISO 3166-1 alpha-2 codes (sorted for binary search).
4///
5/// Excludes exceptionally/transitionally reserved codes (e.g. `EU`, `UK`): only
6/// codes ISO 3166-1 has officially assigned to a country are accepted. Kept as a
7/// `const` so this stays `no_std` + zero-dependency.
8const ISO_3166_1_ALPHA2: &[&str] = &[
9    "AD", "AE", "AF", "AG", "AI", "AL", "AM", "AO", "AQ", "AR", "AS", "AT", "AU", "AW", "AX", "AZ",
10    "BA", "BB", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BL", "BM", "BN", "BO", "BQ", "BR", "BS",
11    "BT", "BV", "BW", "BY", "BZ", "CA", "CC", "CD", "CF", "CG", "CH", "CI", "CK", "CL", "CM", "CN",
12    "CO", "CR", "CU", "CV", "CW", "CX", "CY", "CZ", "DE", "DJ", "DK", "DM", "DO", "DZ", "EC", "EE",
13    "EG", "EH", "ER", "ES", "ET", "FI", "FJ", "FK", "FM", "FO", "FR", "GA", "GB", "GD", "GE", "GF",
14    "GG", "GH", "GI", "GL", "GM", "GN", "GP", "GQ", "GR", "GS", "GT", "GU", "GW", "GY", "HK", "HM",
15    "HN", "HR", "HT", "HU", "ID", "IE", "IL", "IM", "IN", "IO", "IQ", "IR", "IS", "IT", "JE", "JM",
16    "JO", "JP", "KE", "KG", "KH", "KI", "KM", "KN", "KP", "KR", "KW", "KY", "KZ", "LA", "LB", "LC",
17    "LI", "LK", "LR", "LS", "LT", "LU", "LV", "LY", "MA", "MC", "MD", "ME", "MF", "MG", "MH", "MK",
18    "ML", "MM", "MN", "MO", "MP", "MQ", "MR", "MS", "MT", "MU", "MV", "MW", "MX", "MY", "MZ", "NA",
19    "NC", "NE", "NF", "NG", "NI", "NL", "NO", "NP", "NR", "NU", "NZ", "OM", "PA", "PE", "PF", "PG",
20    "PH", "PK", "PL", "PM", "PN", "PR", "PS", "PT", "PW", "PY", "QA", "RE", "RO", "RS", "RU", "RW",
21    "SA", "SB", "SC", "SD", "SE", "SG", "SH", "SI", "SJ", "SK", "SL", "SM", "SN", "SO", "SR", "SS",
22    "ST", "SV", "SX", "SY", "SZ", "TC", "TD", "TF", "TG", "TH", "TJ", "TK", "TL", "TM", "TN", "TO",
23    "TR", "TT", "TV", "TW", "TZ", "UA", "UG", "UM", "US", "UY", "UZ", "VA", "VC", "VE", "VG", "VI",
24    "VN", "VU", "WF", "WS", "YE", "YT", "ZA", "ZM", "ZW",
25];
26
27/// ISO 3166-1 alpha-2: two uppercase ASCII letters that are an officially
28/// assigned country code. Shape-only candidates such as `XX`/`QZ`/`AA` are
29/// rejected because they are not in the assigned set.
30#[must_use]
31pub fn country_code_valid(code: &str) -> bool {
32    code.len() == 2
33        && code.bytes().all(|b| b.is_ascii_uppercase())
34        && ISO_3166_1_ALPHA2.binary_search(&code).is_ok()
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40
41    #[test]
42    fn country_code_rules() {
43        assert!(country_code_valid("DE"));
44        assert!(!country_code_valid("de"));
45        assert!(!country_code_valid("DEU"));
46    }
47
48    #[test]
49    fn assigned_codes_accepted() {
50        for c in ["DE", "FR", "US", "MK", "SS", "AD", "ZW"] {
51            assert!(country_code_valid(c), "{c} is an assigned ISO 3166-1 code");
52        }
53    }
54
55    #[test]
56    fn well_formed_but_unassigned_codes_rejected() {
57        // Correct shape, not officially assigned ⇒ must be rejected.
58        for c in ["XX", "QZ", "AA", "ZZ", "OO"] {
59            assert!(!country_code_valid(c), "{c} is not an assigned code");
60        }
61        // `EU`/`UK` are reserved, not officially assigned.
62        assert!(!country_code_valid("EU"));
63        assert!(!country_code_valid("UK"));
64    }
65
66    #[test]
67    fn table_is_sorted_for_binary_search() {
68        assert!(ISO_3166_1_ALPHA2.windows(2).all(|w| w[0] < w[1]));
69    }
70}