Current section

Files

Jump to
rbt_weave crates weave-content src countries.rs
Raw

crates/weave-content/src/countries.rs

//! ISO 3166-1 alpha-2 country code validation, special jurisdiction scopes,
//! and display names.
use std::collections::HashMap;
use std::sync::OnceLock;
/// Static country names map loaded from JSON at compile time.
const COUNTRY_NAMES_JSON: &str = include_str!("../countries.json");
/// Special jurisdiction scope definition.
/// Valid in jurisdiction fields and case directory paths, but NOT as nationality codes.
struct SpecialScope {
/// Uppercase canonical code (e.g., "INT", "EU", "MULTINATIONAL").
code: &'static str,
/// Display name for HTML output (e.g., "International", "European Union").
display: &'static str,
}
/// All special jurisdiction scopes. Add new scopes here.
/// Code must be uppercase; display is the human-readable name.
const SPECIAL_SCOPES: &[SpecialScope] = &[
SpecialScope {
code: "INT",
display: "International",
},
SpecialScope {
code: "EU",
display: "European Union",
},
SpecialScope {
code: "MULTINATIONAL",
display: "Multinational",
},
];
/// Lazily parsed country names map (uppercase ISO alpha-2 → English name).
fn country_names_map() -> &'static HashMap<String, String> {
static MAP: OnceLock<HashMap<String, String>> = OnceLock::new();
MAP.get_or_init(|| serde_json::from_str(COUNTRY_NAMES_JSON).unwrap_or_default())
}
/// Check whether `code` is a valid ISO 3166-1 alpha-2 country code
/// (case-insensitive lookup against `countries.json`).
/// Does NOT accept special scopes — use [`is_valid_jurisdiction_code`] for that.
pub fn is_valid_country_code(code: &str) -> bool {
let upper = code.to_uppercase();
country_names_map().contains_key(&upper)
}
/// Check whether `code` is a valid jurisdiction code: either a valid
/// ISO 3166-1 alpha-2 country code or a special scope.
pub fn is_valid_jurisdiction_code(code: &str) -> bool {
let upper = code.to_uppercase();
SPECIAL_SCOPES.iter().any(|s| s.code == upper) || country_names_map().contains_key(&upper)
}
/// Check whether `code` is a special jurisdiction scope (not a country).
pub fn is_special_scope(code: &str) -> bool {
let upper = code.to_uppercase();
SPECIAL_SCOPES.iter().any(|s| s.code == upper)
}
/// Map an ISO 3166-1 alpha-2 code (or special scope) to its display name.
/// Returns the uppercased code itself if not found (graceful fallback).
pub fn country_name(code: &str) -> String {
let upper = code.to_uppercase();
for scope in SPECIAL_SCOPES {
if scope.code == upper {
return scope.display.to_string();
}
}
country_names_map().get(&upper).cloned().unwrap_or(upper)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn valid_codes() {
assert!(is_valid_country_code("id"));
assert!(is_valid_country_code("ID"));
assert!(is_valid_country_code("us"));
assert!(is_valid_country_code("GB"));
assert!(is_valid_country_code("de"));
}
#[test]
fn invalid_codes() {
assert!(!is_valid_country_code("Indonesian"));
assert!(!is_valid_country_code("British"));
assert!(!is_valid_country_code("XX"));
assert!(!is_valid_country_code(""));
assert!(!is_valid_country_code("USA"));
// Special scopes are NOT valid country codes
assert!(!is_valid_country_code("INT"));
assert!(!is_valid_country_code("EU"));
assert!(!is_valid_country_code("MULTINATIONAL"));
}
#[test]
fn valid_jurisdiction_codes() {
assert!(is_valid_jurisdiction_code("ID"));
assert!(is_valid_jurisdiction_code("gb"));
assert!(is_valid_jurisdiction_code("INT"));
assert!(is_valid_jurisdiction_code("int"));
assert!(is_valid_jurisdiction_code("EU"));
assert!(is_valid_jurisdiction_code("eu"));
assert!(is_valid_jurisdiction_code("MULTINATIONAL"));
assert!(is_valid_jurisdiction_code("multinational"));
}
#[test]
fn invalid_jurisdiction_codes() {
assert!(!is_valid_jurisdiction_code("XX"));
assert!(!is_valid_jurisdiction_code(""));
assert!(!is_valid_jurisdiction_code("Indonesian"));
}
#[test]
fn special_scope_check() {
assert!(is_special_scope("INT"));
assert!(is_special_scope("int"));
assert!(is_special_scope("EU"));
assert!(is_special_scope("eu"));
assert!(is_special_scope("MULTINATIONAL"));
assert!(!is_special_scope("ID"));
assert!(!is_special_scope(""));
}
#[test]
fn country_name_lookup() {
assert_eq!(country_name("id"), "Indonesia");
assert_eq!(country_name("US"), "United States");
assert_eq!(country_name("gb"), "United Kingdom");
}
#[test]
fn country_name_special_scopes() {
assert_eq!(country_name("int"), "International");
assert_eq!(country_name("INT"), "International");
assert_eq!(country_name("eu"), "European Union");
assert_eq!(country_name("EU"), "European Union");
assert_eq!(country_name("multinational"), "Multinational");
assert_eq!(country_name("MULTINATIONAL"), "Multinational");
}
#[test]
fn country_name_fallback() {
assert_eq!(country_name("XX"), "XX");
assert_eq!(country_name("zz"), "ZZ");
}
}