Packages
rbt_weave
0.2.64
0.2.67
0.2.66
0.2.65
0.2.64
0.2.63
0.2.62
0.2.61
0.2.60
0.2.59
0.2.58
0.2.57
0.2.56
0.2.55
0.2.54
0.2.53
0.2.52
0.2.51
0.2.50
0.2.49
0.2.48
0.2.47
0.2.46
0.2.45
0.2.44
0.2.43
0.2.42
0.2.41
0.2.40
0.2.39
0.2.38
0.2.37
0.2.36
0.2.35
0.2.34
0.2.33
0.2.32
0.2.31
0.2.30
0.2.29
0.2.28
0.2.27
0.2.26
0.2.25
0.2.24
0.2.23
0.2.22
0.2.21
0.2.20
0.2.19
0.2.18
0.2.17
0.2.16
0.2.15
0.2.14
0.2.13
0.2.12
0.2.11
0.2.10
0.2.9
0.2.8
0.2.7
0.2.6
0.2.5
0.2.4
0.2.3
0.2.2
0.2.1
0.2.0
0.1.0
Graph conflict detection and image processing NIFs for OSINT knowledge graphs, powered by Rust.
Current section
Files
Jump to
Current section
Files
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");
}
}