use core::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CharacterClass {
Digit,
Letter,
Alphanumeric,
}
impl fmt::Display for CharacterClass {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CharacterClass::Digit => write!(f, "a digit (0-9)"),
CharacterClass::Letter => write!(f, "an uppercase letter (A-Z)"),
CharacterClass::Alphanumeric => {
write!(f, "a digit (0-9) or an uppercase letter (A-Z)")
}
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum IsinError {
Empty,
InvalidLength {
found: usize,
},
InvalidCharacter {
character: char,
position: u8,
expected: CharacterClass,
},
InvalidCheckDigit {
expected: u8,
found: u8,
},
}
impl fmt::Display for IsinError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
IsinError::Empty => f.write_str("ISIN input is empty"),
IsinError::InvalidLength { found } => {
write!(f, "ISIN must contain exactly 12 characters, found {found}")
}
IsinError::InvalidCharacter {
character,
position,
expected,
} => write!(
f,
"invalid character '{character}' at position {position} of 12: expected {expected}"
),
IsinError::InvalidCheckDigit { expected, found } => write!(
f,
"invalid check digit at position 12 of 12: expected {expected}, found {found}"
),
}
}
}
impl core::error::Error for IsinError {}