use core::fmt;
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CountryCodeError {
Empty,
InvalidLength {
found: usize,
},
InvalidCharacter {
character: char,
position: u8,
},
Unassigned {
code: [char; 2],
},
}
impl fmt::Display for CountryCodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CountryCodeError::Empty => f.write_str("country code input is empty"),
CountryCodeError::InvalidLength { found } => {
write!(
f,
"country code must contain exactly 2 characters, found {found}"
)
}
CountryCodeError::InvalidCharacter {
character,
position,
} => write!(
f,
"invalid character '{character}' at position {position} of 2: expected an uppercase letter (A to Z)"
),
CountryCodeError::Unassigned { code } => write!(
f,
"'{}{}' is not an officially assigned ISO 3166-1 alpha-2 code",
code[0], code[1]
),
}
}
}
impl core::error::Error for CountryCodeError {}