use super::Code;
impl Code {
pub fn from_alpha_2_str(code: &str) -> Self {
let bytes = code.as_bytes();
let [a, b] = bytes else {
panic!(
"Unable to parse country code. Expected length of 2 chars. It has length: `{}`",
code.len()
);
};
let couplet: [u8; 2] = [a.to_ascii_uppercase(), b.to_ascii_uppercase()];
let Some(code) = Code::from_alpha_2(couplet) else {
panic!("Unknown country code `{code}`");
};
code
}
}
#[test]
fn alpha2_country_code_matches() {
let code = Code::from_alpha_2(*b"NL").unwrap();
assert_eq!(Code::Nl, code);
assert_eq!("NL", code.into_alpha_2_str());
}
#[test]
fn alpha3_country_code_matches() {
let code = Code::from_alpha_3(*b"NLD").unwrap();
assert_eq!(Code::Nl, code);
}