ifsc 0.1.1

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

use crate::{Ifsc, RoutingCode};

use super::{lower_bound, search, snapshot};

#[derive(Clone, Copy)]
pub(crate) struct BranchView {
    bytes: &'static [u8],
}

impl BranchView {
    pub(crate) fn ifsc(self) -> Ifsc {
        let mut value = [0; 11];
        value.copy_from_slice(&self.bytes[..11]);
        Ifsc(value)
    }

    pub(crate) fn flags(self) -> u8 {
        self.bytes[11]
    }

    pub(crate) fn text(self, index: usize) -> Option<&'static str> {
        snapshot().text(self.bytes, 12 + index * 4)
    }
}

pub(crate) fn branch(index: u32) -> Option<BranchView> {
    let snapshot = snapshot();
    Some(BranchView {
        bytes: snapshot.record(snapshot.branches, index)?,
    })
}

pub(crate) fn branch_count() -> u32 {
    snapshot().branches.count
}

pub(crate) fn find_branch(ifsc: Ifsc) -> Option<u32> {
    search(branch_count(), |index| {
        branch(index).expect("valid branch row").ifsc().cmp(&ifsc)
    })
}

pub(crate) fn routing_range(code: RoutingCode) -> Range<u32> {
    let count = branch_count();
    let start = lower_bound(count, |index| {
        branch(index)
            .expect("valid branch row")
            .ifsc()
            .routing_code()
            < code
    });
    let end = lower_bound(count, |index| {
        branch(index)
            .expect("valid branch row")
            .ifsc()
            .routing_code()
            <= code
    });
    start..end
}