use core::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CharacterClass {
Digit,
AlphanumericUppercase,
}
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::AlphanumericUppercase => {
write!(f, "a digit (0-9) or an uppercase letter (A-Z)")
}
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CnpjError {
Empty,
InvalidLength {
found: usize,
},
InvalidCharacter {
character: char,
position: u8,
expected: CharacterClass,
},
InvalidCheckDigits {
position: u8,
expected: u8,
found: u8,
},
RepeatedDigits,
}
impl fmt::Display for CnpjError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CnpjError::Empty => f.write_str("CNPJ input is empty"),
CnpjError::InvalidLength { found } => write!(
f,
"CNPJ must contain exactly 14 characters once formatting is removed, found {found}"
),
CnpjError::InvalidCharacter {
character,
position,
expected,
} => write!(
f,
"invalid character '{character}' at position {position} of 14: expected {expected}"
),
CnpjError::InvalidCheckDigits {
position,
expected,
found,
} => write!(
f,
"invalid check digit at position {position} of 14: expected {expected}, found {found}"
),
CnpjError::RepeatedDigits => {
f.write_str("CNPJ cannot consist of a single character repeated 14 times")
}
}
}
}
impl core::error::Error for CnpjError {}