iban-check 0.1.0

A dependency-free IBAN validation library for Rust.
Documentation
use std::fmt;

/// Represents all possible validation errors for an IBAN.
#[derive(Debug, Clone, PartialEq)]
pub enum ValidationError {
    /// The input string is empty or contains only whitespace.
    Empty,
    /// The country code (first two characters) is unknown or invalid.
    InvalidCountryCode,
    /// The IBAN length does not match the expected length for the country.
    InvalidLength { expected: usize, found: usize },
    /// An invalid character was found at the given position.
    InvalidCharacter { character: char, position: usize },
    /// The Mod-97-10 checksum is invalid.
    InvalidChecksum,
}

impl fmt::Display for ValidationError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Empty => write!(f, "IBAN is empty"),
            Self::InvalidCountryCode => write!(f, "IBAN contains an unknown country code"),
            Self::InvalidLength { expected, found } => {
                write!(
                    f,
                    "IBAN has invalid length: expected {} characters, found {}",
                    expected, found
                )
            }
            Self::InvalidCharacter { character, position } => {
                write!(f, "Invalid character '{}' at position {}", character, position)
            }
            Self::InvalidChecksum => write!(f, "IBAN checksum is invalid"),
        }
    }
}

impl std::error::Error for ValidationError {}