use crate::{BankCode, BankKind, BankService, Ifsc, ReportedStatus};
use super::{search, snapshot};
#[derive(Clone, Copy)]
pub(crate) struct BankNameView {
bytes: &'static [u8],
}
impl BankNameView {
pub(crate) fn code(self) -> BankCode {
bank_code(self.bytes)
}
pub(crate) fn name(self) -> &'static str {
snapshot().text(self.bytes, 4).expect("bank name")
}
}
pub(crate) fn bank_name(index: u32) -> Option<BankNameView> {
let snapshot = snapshot();
Some(BankNameView {
bytes: snapshot.record(snapshot.names, index)?,
})
}
pub(crate) fn bank_name_count() -> u32 {
snapshot().names.count
}
pub(crate) fn find_bank_name(code: BankCode) -> Option<u32> {
search(bank_name_count(), |index| {
bank_name(index)
.expect("valid bank-name row")
.code()
.cmp(&code)
})
}
#[derive(Clone, Copy)]
pub(crate) struct BankView {
bytes: &'static [u8],
}
impl BankView {
pub(crate) fn code(self) -> BankCode {
bank_code(self.bytes)
}
pub(crate) fn kind(self) -> BankKind {
match self.bytes[4] {
0 => BankKind::DistrictCentralCooperative,
1 => BankKind::Foreign,
2 => BankKind::LocalArea,
3 => BankKind::OtherUrbanCooperative,
4 => BankKind::Payments,
5 => BankKind::Private,
6 => BankKind::PublicSector,
7 => BankKind::RegionalRural,
8 => BankKind::StateCooperative,
9 => BankKind::SmallFinance,
_ => BankKind::ScheduledUrbanCooperative,
}
}
pub(crate) fn reference_ifsc(self) -> Option<Ifsc> {
crate::parse_strict(snapshot().text(self.bytes, 8)?).ok()
}
pub(crate) fn text(self, index: usize) -> Option<&'static str> {
snapshot().text(self.bytes, 12 + index * 4)
}
pub(crate) fn status(self, service: BankService) -> ReportedStatus {
let bit = match service {
BankService::AchCredit => 0,
BankService::AchDebit => 1,
BankService::Apbs => 2,
BankService::NachDebit => 3,
BankService::Upi => 4,
BankService::Imps => 5,
};
if self.bytes[6] & (1 << bit) == 0 {
ReportedStatus::NotReported
} else if self.bytes[5] & (1 << bit) != 0 {
ReportedStatus::Available
} else {
ReportedStatus::Unavailable
}
}
}
pub(crate) fn bank_profile(index: u32) -> Option<BankView> {
let snapshot = snapshot();
Some(BankView {
bytes: snapshot.record(snapshot.banks, index)?,
})
}
pub(crate) fn bank_profile_count() -> u32 {
snapshot().banks.count
}
pub(crate) fn find_bank_profile(code: BankCode) -> Option<u32> {
search(bank_profile_count(), |index| {
bank_profile(index)
.expect("valid bank-profile row")
.code()
.cmp(&code)
})
}
fn bank_code(bytes: &[u8]) -> BankCode {
let mut value = [0; 4];
value.copy_from_slice(&bytes[..4]);
BankCode(value)
}