ifsc 0.1.1

A fast offline directory for Indian bank branches and IFSCs
Documentation
use core::fmt;

/// An IFSC parsing or directory-query result.
pub type Result<T> = core::result::Result<T, Error>;

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
/// A stable category for an IFSC operation failure.
pub enum ErrorKind {
    /// The input does not match the IFSC grammar.
    InvalidIfsc,
    /// The input is not a four-letter routing code.
    InvalidRoutingCode,
    /// The input is not a four-character bank code.
    InvalidBankCode,
    /// The input is not an Indian ISO 3166-2 subdivision code.
    InvalidIsoSubdivision,
    /// The input exceeds its configured byte limit.
    TooLong,
    /// The input is valid after normalization but is not canonical.
    NotCanonical,
    /// A query term or combination is invalid.
    InvalidQuery,
    /// A requested result page exceeds the supported limit.
    InvalidPage,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
/// A redacted IFSC parsing or query error.
pub struct Error {
    pub(crate) kind: ErrorKind,
}

impl Error {
    /// Constructs an error from its stable category.
    pub(crate) const fn new(kind: ErrorKind) -> Self {
        Self { kind }
    }
    /// Returns the stable error category.
    pub const fn kind(self) -> ErrorKind {
        self.kind
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self.kind {
            ErrorKind::InvalidIfsc => "input is not an IFSC",
            ErrorKind::InvalidRoutingCode => "input is not an IFSC routing code",
            ErrorKind::InvalidBankCode => "input is not a bank code",
            ErrorKind::InvalidIsoSubdivision => "input is not an ISO subdivision code",
            ErrorKind::TooLong => "input exceeds the configured limit",
            ErrorKind::NotCanonical => "input is not in canonical uppercase form",
            ErrorKind::InvalidQuery => "directory query is invalid",
            ErrorKind::InvalidPage => "directory page is invalid",
        })
    }
}

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