const CODES: &[(&str, &str)] = &[
("DZA", "Algeria"),
("AGO", "Angola"),
("BEN", "Benin"),
("BWA", "Botswana"),
("BFA", "Burkina Faso"),
("BDI", "Burundi"),
("CPV", "Cabo Verde"),
("CMR", "Cameroon"),
("CAF", "Central African Republic"),
("TCD", "Chad"),
("COM", "Comoros"),
("COG", "Congo"),
("COD", "Congo (Democratic Republic of the)"),
("CIV", "Côte d'Ivoire"),
("DJI", "Djibouti"),
("EGY", "Egypt"),
("GNQ", "Equatorial Guinea"),
("ERI", "Eritrea"),
("SWZ", "Eswatini"),
("ETH", "Ethiopia"),
("GAB", "Gabon"),
("GMB", "Gambia"),
("GHA", "Ghana"),
("GIN", "Guinea"),
("GNB", "Guinea-Bissau"),
("KEN", "Kenya"),
("LSO", "Lesotho"),
("LBR", "Liberia"),
("LBY", "Libya"),
("MDG", "Madagascar"),
("MWI", "Malawi"),
("MLI", "Mali"),
("MRT", "Mauritania"),
("MUS", "Mauritius"),
("MAR", "Morocco"),
("MOZ", "Mozambique"),
("NAM", "Namibia"),
("NER", "Niger"),
("NGA", "Nigeria"),
("RWA", "Rwanda"),
("STP", "Sao Tome and Principe"),
("SEN", "Senegal"),
("SYC", "Seychelles"),
("SLE", "Sierra Leone"),
("SOM", "Somalia"),
("ZAF", "South Africa"),
("SSD", "South Sudan"),
("SDN", "Sudan"),
("TZA", "Tanzania"),
("TGO", "Togo"),
("TUN", "Tunisia"),
("UGA", "Uganda"),
("ZMB", "Zambia"),
("ZWE", "Zimbabwe"),
("ESH", "Western Sahara"),
("ATG", "Antigua and Barbuda"),
("ARG", "Argentina"),
("BHS", "Bahamas"),
("BRB", "Barbados"),
("BLZ", "Belize"),
("BOL", "Bolivia"),
("BRA", "Brazil"),
("CAN", "Canada"),
("CHL", "Chile"),
("COL", "Colombia"),
("CRI", "Costa Rica"),
("CUB", "Cuba"),
("DMA", "Dominica"),
("DOM", "Dominican Republic"),
("ECU", "Ecuador"),
("SLV", "El Salvador"),
("GRD", "Grenada"),
("GTM", "Guatemala"),
("GUY", "Guyana"),
("HTI", "Haiti"),
("HND", "Honduras"),
("JAM", "Jamaica"),
("MEX", "Mexico"),
("NIC", "Nicaragua"),
("PAN", "Panama"),
("PRY", "Paraguay"),
("PER", "Peru"),
("KNA", "Saint Kitts and Nevis"),
("LCA", "Saint Lucia"),
("VCT", "Saint Vincent and the Grenadines"),
("SUR", "Suriname"),
("TTO", "Trinidad and Tobago"),
("USA", "United States of America"),
("URY", "Uruguay"),
("VEN", "Venezuela"),
("AFG", "Afghanistan"),
("ARM", "Armenia"),
("AZE", "Azerbaijan"),
("BHR", "Bahrain"),
("BGD", "Bangladesh"),
("BTN", "Bhutan"),
("BRN", "Brunei Darussalam"),
("KHM", "Cambodia"),
("CHN", "China"),
("CYP", "Cyprus"),
("GEO", "Georgia"),
("HKG", "Hong Kong"),
("IND", "India"),
("IDN", "Indonesia"),
("IRN", "Iran"),
("IRQ", "Iraq"),
("ISR", "Israel"),
("JPN", "Japan"),
("JOR", "Jordan"),
("KAZ", "Kazakhstan"),
("PRK", "Korea (Democratic People's Republic of)"),
("KOR", "Korea (Republic of)"),
("KWT", "Kuwait"),
("KGZ", "Kyrgyzstan"),
("LAO", "Lao People's Democratic Republic"),
("LBN", "Lebanon"),
("MAC", "Macao"),
("MYS", "Malaysia"),
("MDV", "Maldives"),
("MNG", "Mongolia"),
("MMR", "Myanmar"),
("NPL", "Nepal"),
("OMN", "Oman"),
("PAK", "Pakistan"),
("PSE", "Palestine"),
("PHL", "Philippines"),
("QAT", "Qatar"),
("SAU", "Saudi Arabia"),
("SGP", "Singapore"),
("LKA", "Sri Lanka"),
("SYR", "Syrian Arab Republic"),
("TWN", "Taiwan"),
("TJK", "Tajikistan"),
("THA", "Thailand"),
("TLS", "Timor-Leste"),
("TUR", "Türkiye"),
("TKM", "Turkmenistan"),
("ARE", "United Arab Emirates"),
("UZB", "Uzbekistan"),
("VNM", "Viet Nam"),
("YEM", "Yemen"),
("ALB", "Albania"),
("AND", "Andorra"),
("AUT", "Austria"),
("BLR", "Belarus"),
("BEL", "Belgium"),
("BIH", "Bosnia and Herzegovina"),
("BGR", "Bulgaria"),
("HRV", "Croatia"),
("CZE", "Czechia"),
("DNK", "Denmark"),
("EST", "Estonia"),
("FIN", "Finland"),
("FRA", "France"),
("DEU", "Germany"),
("GRC", "Greece"),
("HUN", "Hungary"),
("ISL", "Iceland"),
("IRL", "Ireland"),
("ITA", "Italy"),
("XKX", "Kosovo"),
("LVA", "Latvia"),
("LIE", "Liechtenstein"),
("LTU", "Lithuania"),
("LUX", "Luxembourg"),
("MLT", "Malta"),
("MDA", "Moldova"),
("MCO", "Monaco"),
("MNE", "Montenegro"),
("NLD", "Netherlands"),
("MKD", "North Macedonia"),
("NOR", "Norway"),
("POL", "Poland"),
("PRT", "Portugal"),
("ROU", "Romania"),
("RUS", "Russian Federation"),
("SMR", "San Marino"),
("SRB", "Serbia"),
("SVK", "Slovakia"),
("SVN", "Slovenia"),
("ESP", "Spain"),
("SWE", "Sweden"),
("CHE", "Switzerland"),
("UKR", "Ukraine"),
("GBR", "United Kingdom"),
("VAT", "Holy See (Vatican City State)"),
("AUS", "Australia"),
("FJI", "Fiji"),
("KIR", "Kiribati"),
("MHL", "Marshall Islands"),
("FSM", "Micronesia (Federated States of)"),
("NRU", "Nauru"),
("NZL", "New Zealand"),
("PLW", "Palau"),
("PNG", "Papua New Guinea"),
("WSM", "Samoa"),
("SLB", "Solomon Islands"),
("TON", "Tonga"),
("TUV", "Tuvalu"),
("VUT", "Vanuatu"),
("ABW", "Aruba"),
("BMU", "Bermuda"),
("CYM", "Cayman Islands"),
("CUW", "Curaçao"),
("FRO", "Faroe Islands"),
("GIB", "Gibraltar"),
("GRL", "Greenland"),
("SXM", "Sint Maarten (Dutch part)"),
("UTO", "Utopia (ICAO specimen)"),
("D", "Germany"), ("EUE", "European Union"),
("RKS", "Kosovo"), ("GBD", "British Overseas Territories Citizen"),
("GBN", "British National (Overseas)"),
("GBO", "British Overseas Citizen"),
("GBP", "British Protected Person"),
("GBS", "British Subject"),
("XXA", "Stateless person (1954 Convention)"),
("XXB", "Refugee (1951 Convention)"),
("XXC", "Refugee (other)"),
("XXX", "Unspecified nationality"),
("UNO", "United Nations Organization"),
("UNA", "United Nations specialized agency"),
(
"UNK",
"Resident of Kosovo to whom a travel document has been issued by the United Nations Interim Administration Mission in Kosovo (UNMIK)",
),
("XOM", "Sovereign Military Order of Malta"),
("XBA", "African Development Bank"),
("XIM", "African Export-Import Bank"),
("XCC", "Caribbean Community (CARICOM)"),
(
"XCO",
"Common Market for Eastern and Southern Africa (COMESA)",
),
("XEC", "Economic Community of West African States (ECOWAS)"),
(
"XPO",
"International Criminal Police Organization (INTERPOL)",
),
("XCE", "Council of Europe"),
("XES", "Organization of Eastern Caribbean States (OECS)"),
("XMP", "Parliamentary Assembly of the Mediterranean (PAM)"),
("XDC", "Southern African Development Community"),
("ANT", "Netherlands Antilles"),
("NTZ", "Neutral Zone"),
("IAO", "International Civil Aviation Organization"),
("AIA", "Anguilla"),
("ALA", "Åland Islands"),
("ASM", "American Samoa"),
("ATA", "Antarctica"),
("ATF", "French Southern Territories"),
("BES", "Bonaire, Sint Eustatius and Saba"),
("BLM", "Saint Barthélemy"),
("BVT", "Bouvet Island"),
("CCK", "Cocos (Keeling) Islands"),
("COK", "Cook Islands"),
("CXR", "Christmas Island"),
("FLK", "Falkland Islands (Malvinas)"),
("GGY", "Guernsey"),
("GLP", "Guadeloupe"),
("GUF", "French Guiana"),
("GUM", "Guam"),
("HMD", "Heard Island and McDonald Islands"),
("IMN", "Isle of Man"),
("IOT", "British Indian Ocean Territory"),
("JEY", "Jersey"),
("MAF", "Saint Martin (French Part)"),
("MNP", "Northern Mariana Islands"),
("MSR", "Montserrat"),
("MTQ", "Martinique"),
("MYT", "Mayotte"),
("NCL", "New Caledonia"),
("NFK", "Norfolk Island"),
("NIU", "Niue"),
("PCN", "Pitcairn"),
("PRI", "Puerto Rico"),
("PYF", "French Polynesia"),
("REU", "Réunion"),
("SGS", "South Georgia and the South Sandwich Islands"),
("SHN", "Saint Helena"),
("SJM", "Svalbard and Jan Mayen Islands"),
("SPM", "Saint Pierre and Miquelon"),
("TCA", "Turks and Caicos Islands"),
("TKL", "Tokelau"),
("UMI", "United States Minor Outlying Islands"),
("VGB", "British Virgin Islands"),
("VIR", "United States Virgin Islands"),
("WLF", "Wallis and Futuna Islands"),
];
#[rustfmt::skip]
const ALTERNATE_NAMES: &[(&str, &str)] = &[
("Turkiye", "TUR"), ("Turkey", "TUR"), ("Republic of Turkey", "TUR"),
("Republic of Korea", "KOR"),
("Czech Republic", "CZE"),
("United Kingdom of Great Britain and Northern Ireland", "GBR"),
("Islamic Republic of Afghanistan", "AFG"),
("Azerbaycan", "AZE"),
("Polska", "POL"),
];
pub fn country_name(code: &str) -> Option<&'static str> {
CODES
.iter()
.find(|&&(c, _)| c == code)
.map(|&(_, name)| name)
}
pub fn code_for_name(name: &str) -> Option<&'static str> {
CODES
.iter()
.find(|&&(_, n)| n.eq_ignore_ascii_case(name))
.map(|&(c, _)| c)
.or_else(|| {
ALTERNATE_NAMES
.iter()
.find(|&&(n, _)| n.eq_ignore_ascii_case(name))
.map(|&(_, c)| c)
})
}
pub fn codes() -> &'static [(&'static str, &'static str)] {
CODES
}
pub fn alternate_names() -> &'static [(&'static str, &'static str)] {
ALTERNATE_NAMES
}
pub fn codes_equivalent(a: &str, b: &str) -> bool {
if a.eq_ignore_ascii_case(b) {
return true;
}
fn name_of(code: &str) -> Option<&'static str> {
CODES
.iter()
.find(|&&(c, _)| c.eq_ignore_ascii_case(code))
.map(|&(_, name)| name)
}
match (name_of(a), name_of(b)) {
(Some(x), Some(y)) => x == y,
_ => false,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn maps_representative_codes_neutrally() {
assert_eq!(country_name("HRV"), Some("Croatia"));
assert_eq!(country_name("SRB"), Some("Serbia"));
assert_eq!(country_name("SVN"), Some("Slovenia"));
assert_eq!(country_name("TWN"), Some("Taiwan"));
assert_eq!(country_name("PSE"), Some("Palestine"));
assert_eq!(country_name("XKX"), Some("Kosovo"));
assert_eq!(country_name("UTO"), Some("Utopia (ICAO specimen)"));
assert_eq!(country_name("GBN"), Some("British National (Overseas)"));
assert_eq!(country_name("XXB"), Some("Refugee (1951 Convention)"));
assert_eq!(country_name("ZZZ"), None);
assert_eq!(country_name(""), None);
}
#[test]
fn maps_seven_additional_codes() {
assert_eq!(country_name("XCE"), Some("Council of Europe"));
assert_eq!(
country_name("XES"),
Some("Organization of Eastern Caribbean States (OECS)")
);
assert_eq!(
country_name("XMP"),
Some("Parliamentary Assembly of the Mediterranean (PAM)")
);
assert_eq!(
country_name("XDC"),
Some("Southern African Development Community")
);
assert_eq!(country_name("ANT"), Some("Netherlands Antilles"));
assert_eq!(country_name("NTZ"), Some("Neutral Zone"));
assert_eq!(
country_name("IAO"),
Some("International Civil Aviation Organization")
);
}
#[test]
fn reverse_lookup_is_case_insensitive_and_round_trips() {
assert_eq!(code_for_name("Croatia"), Some("HRV"));
assert_eq!(code_for_name("CROATIA"), Some("HRV"));
assert_eq!(code_for_name("croatia"), Some("HRV"));
assert_eq!(code_for_name("Taiwan"), Some("TWN"));
assert_eq!(code_for_name("Not A Country"), None);
assert_eq!(code_for_name(""), None);
}
#[test]
fn reverse_lookup_prefers_the_primary_code_for_aliased_names() {
assert_eq!(code_for_name("Kosovo"), Some("XKX"));
assert_eq!(code_for_name("Germany"), Some("DEU"));
}
#[test]
fn part3_authority_codes_have_verified_names() {
assert_eq!(country_name("UNK"), Some("Resident of Kosovo to whom a travel document has been issued by the United Nations Interim Administration Mission in Kosovo (UNMIK)"));
assert_eq!(
country_name("XCO"),
Some("Common Market for Eastern and Southern Africa (COMESA)")
);
assert_eq!(
country_name("XPO"),
Some("International Criminal Police Organization (INTERPOL)")
);
}
#[test]
fn aliased_codes_are_equivalent_to_their_primary_code() {
assert!(codes_equivalent("D", "DEU"));
assert!(codes_equivalent("DEU", "D"));
assert!(codes_equivalent("XKX", "RKS"));
}
#[test]
fn distinct_countries_are_never_equivalent() {
assert!(!codes_equivalent("DEU", "AUT"));
assert!(!codes_equivalent("D", "DNK"));
assert!(!codes_equivalent("ZZZ", "DEU"));
assert!(!codes_equivalent("ZZZ", "QQQ"));
assert!(codes_equivalent("ZZZ", "zzz"));
assert!(!codes_equivalent("", "DEU"));
}
#[test]
fn equivalence_is_case_insensitive_through_the_table_too() {
assert!(codes_equivalent("d", "deu"));
assert!(codes_equivalent("Deu", "D"));
}
#[test]
fn every_code_is_equivalent_to_itself_and_to_its_aliases() {
for &(code, name) in CODES {
assert!(codes_equivalent(code, code), "{code} vs itself");
for &(other, other_name) in CODES {
if name == other_name {
assert!(
codes_equivalent(code, other) && codes_equivalent(other, code),
"{code} and {other} both name {name}"
);
}
}
}
}
#[test]
fn every_table_entry_round_trips_through_both_directions() {
for &(code, name) in CODES {
assert_eq!(
country_name(code),
Some(name),
"country_name({code:?}) should return {name:?}"
);
let reverse = code_for_name(name).expect("name should resolve back to a code");
assert_eq!(country_name(reverse), Some(name));
}
}
#[test]
fn code_for_name_resolves_an_alternate_name_after_codes_declines() {
assert_eq!(code_for_name("Turkiye"), Some("TUR"));
assert_eq!(code_for_name("TURKEY"), Some("TUR"));
assert_eq!(code_for_name("Republic of Turkey"), Some("TUR"));
assert_eq!(code_for_name("REPUBLIC OF KOREA"), Some("KOR"));
assert_eq!(code_for_name("Czech Republic"), Some("CZE"));
assert_eq!(
code_for_name("United Kingdom of Great Britain and Northern Ireland"),
Some("GBR")
);
assert_eq!(
code_for_name("Islamic Republic of Afghanistan"),
Some("AFG")
);
assert_eq!(code_for_name("Azerbaycan"), Some("AZE"));
assert_eq!(code_for_name("Polska"), Some("POL"));
}
#[test]
fn alternate_names_do_not_shadow_the_primary_table() {
for &(name, code) in ALTERNATE_NAMES {
assert!(
!CODES.iter().any(|&(_, n)| n.eq_ignore_ascii_case(name)),
"{name:?} is already resolved by CODES — drop it from ALTERNATE_NAMES"
);
assert!(
country_name(code).is_some(),
"{name:?} maps to {code:?}, which is not a code CODES knows"
);
}
}
#[test]
fn alternate_names_accessor_matches_the_table() {
assert_eq!(alternate_names(), ALTERNATE_NAMES);
assert!(alternate_names().contains(&("Turkey", "TUR")));
}
}