Skip to main content

iban_check/
error.rs

1use std::fmt;
2
3/// Represents all possible validation errors for an IBAN.
4#[derive(Debug, Clone, PartialEq)]
5pub enum ValidationError {
6    /// The input string is empty or contains only whitespace.
7    Empty,
8    /// The country code (first two characters) is unknown or invalid.
9    InvalidCountryCode,
10    /// The IBAN length does not match the expected length for the country.
11    InvalidLength { expected: usize, found: usize },
12    /// An invalid character was found at the given position.
13    InvalidCharacter { character: char, position: usize },
14    /// The Mod-97-10 checksum is invalid.
15    InvalidChecksum,
16}
17
18impl fmt::Display for ValidationError {
19    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20        match self {
21            Self::Empty => write!(f, "IBAN is empty"),
22            Self::InvalidCountryCode => write!(f, "IBAN contains an unknown country code"),
23            Self::InvalidLength { expected, found } => {
24                write!(
25                    f,
26                    "IBAN has invalid length: expected {} characters, found {}",
27                    expected, found
28                )
29            }
30            Self::InvalidCharacter { character, position } => {
31                write!(f, "Invalid character '{}' at position {}", character, position)
32            }
33            Self::InvalidChecksum => write!(f, "IBAN checksum is invalid"),
34        }
35    }
36}
37
38impl std::error::Error for ValidationError {}