ifsc 0.1.1

A fast offline directory for Indian bank branches and IFSCs
Documentation
use crate::{Bank, BankProfile, BankService, ReportedStatus};

#[derive(Clone, Copy)]
pub(super) enum BankFilter {
    All,
    Service(BankService, ReportedStatus),
}

impl BankFilter {
    fn matches(self, bank: Bank) -> bool {
        match self {
            Self::All => true,
            Self::Service(service, status) => bank
                .profile()
                .is_some_and(|profile| profile.reported_status(service) == status),
        }
    }
}

/// An iterator over banks, optionally constrained by a directory filter.
#[derive(Clone)]
pub struct Banks {
    cursor: u32,
    end: u32,
    filter: BankFilter,
}

impl Banks {
    pub(super) fn all() -> Self {
        Self {
            cursor: 0,
            end: crate::snapshot::bank_name_count(),
            filter: BankFilter::All,
        }
    }

    pub(super) fn filtered(filter: BankFilter) -> Self {
        Self {
            filter,
            ..Self::all()
        }
    }
}

impl Iterator for Banks {
    type Item = Bank;

    fn next(&mut self) -> Option<Bank> {
        while self.cursor < self.end {
            let bank = Bank(self.cursor);
            self.cursor += 1;
            if self.filter.matches(bank) {
                return Some(bank);
            }
        }
        None
    }
}

impl core::iter::FusedIterator for Banks {}

/// An exact-size iterator over bank metadata profiles.
#[derive(Clone)]
pub struct BankProfiles {
    cursor: u32,
    end: u32,
}

impl BankProfiles {
    pub(super) fn all() -> Self {
        Self {
            cursor: 0,
            end: crate::snapshot::bank_profile_count(),
        }
    }
}

impl Iterator for BankProfiles {
    type Item = BankProfile;

    fn next(&mut self) -> Option<Self::Item> {
        if self.cursor == self.end {
            return None;
        }
        let profile = BankProfile(self.cursor);
        self.cursor += 1;
        Some(profile)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let remaining = (self.end - self.cursor) as usize;
        (remaining, Some(remaining))
    }
}

impl ExactSizeIterator for BankProfiles {}
impl core::iter::FusedIterator for BankProfiles {}