Skip to main content

Module countries

Module countries 

Source
Expand description

ISO 3166-1 alpha-2 country codes.

CountryCode is a two-uppercase-letter code validated at construction time. It can be read from an environment variable (ENVVAR_COUNTRY_CODE) for locale-aware behaviour in library consumers.

Internally the two letters are packed into a u16 via CountryCode::to_numeric and unpacked with CountryCode::try_from, which allows compact storage when needed.

§Examples

use rfham_core::country::CountryCode;
use std::str::FromStr;

let us = CountryCode::from_str("US").unwrap();
assert_eq!(us.to_string(), "US");

// Numeric round-trip
let n: u16 = us.to_numeric();
let back = CountryCode::try_from(n).unwrap();
assert_eq!(us, back);

Invalid codes are rejected:

use rfham_core::country::CountryCode;

assert!("us".parse::<CountryCode>().is_err());   // must be uppercase
assert!("USA".parse::<CountryCode>().is_err());  // must be exactly 2 chars
assert!("1X".parse::<CountryCode>().is_err());   // must be letters

Structs§

CountryCode

Constants§

ENVVAR_COUNTRY_CODE

Functions§

country_code_uk
country_code_us

Type Aliases§

CountryCodeNumeric