49 lines
1.0 KiB
Rust
49 lines
1.0 KiB
Rust
use lazy_static::lazy_static;
|
|
use std::collections::HashMap;
|
|
|
|
lazy_static! {
|
|
static ref COUNTRIES_FR: HashMap<String, String> =
|
|
serde_json::from_str(include_str!("../../assets/iso-3166_country_french.json")).unwrap();
|
|
}
|
|
|
|
pub fn is_code_valid(code: &str) -> bool {
|
|
rust_iso3166::ALL_ALPHA2.contains(&code)
|
|
}
|
|
|
|
#[derive(serde::Serialize, Debug, Copy, Clone)]
|
|
pub struct CountryCode {
|
|
code: &'static str,
|
|
en: &'static str,
|
|
fr: &'static str,
|
|
}
|
|
|
|
/// Get the entire list of countries
|
|
pub fn get_list() -> Vec<CountryCode> {
|
|
rust_iso3166::ALL
|
|
.iter()
|
|
.map(|c| CountryCode {
|
|
code: c.alpha2,
|
|
en: c.name,
|
|
fr: COUNTRIES_FR
|
|
.get(c.alpha2)
|
|
.map(|s| s.as_str())
|
|
.unwrap_or(c.name),
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use crate::utils::countries_utils::is_code_valid;
|
|
|
|
#[test]
|
|
fn test_code_fr() {
|
|
assert!(is_code_valid("FR"))
|
|
}
|
|
|
|
#[test]
|
|
fn test_code_bad() {
|
|
assert!(!is_code_valid("ZZ"))
|
|
}
|
|
}
|