use crate::{BankCode, Ifsc, SubletMatch, SubletSource, SubletTarget};
use super::{search, snapshot};
pub(crate) fn find_exact_sublet(ifsc: Ifsc) -> Option<SubletMatch> {
let snapshot = snapshot();
let index = search(snapshot.sublets.count, |index| {
snapshot
.record(snapshot.sublets, index)
.expect("valid exact-sublet row")[..11]
.cmp(&ifsc.0)
})?;
let bytes = snapshot.record(snapshot.sublets, index)?;
let mut code = [0; 4];
code.copy_from_slice(&bytes[11..15]);
Some(SubletMatch {
source: SubletSource::Exact,
prefix: core::str::from_utf8(&bytes[..11]).expect("exact sublet IFSC"),
target: SubletTarget::Bank(BankCode(code)),
})
}
pub(crate) struct CustomView {
bytes: &'static [u8],
}
impl CustomView {
pub(crate) fn matches(&self, ifsc: Ifsc) -> bool {
let length = self.bytes[11] as usize;
ifsc.0.get(..length) == self.bytes.get(..length)
}
pub(crate) fn view(&self) -> SubletMatch {
let length = self.bytes[11] as usize;
let prefix = core::str::from_utf8(&self.bytes[..length]).expect("custom sublet prefix");
let value = snapshot()
.text(self.bytes, 16)
.expect("custom sublet target");
let target = if self.bytes[12] == 0 {
let mut code = [0; 4];
code.copy_from_slice(value.as_bytes());
SubletTarget::Bank(BankCode(code))
} else {
SubletTarget::ReportedName(value)
};
SubletMatch {
source: SubletSource::CustomPrefix,
prefix,
target,
}
}
}
pub(crate) fn custom_sublet(index: u32) -> Option<CustomView> {
let snapshot = snapshot();
Some(CustomView {
bytes: snapshot.record(snapshot.custom_sublets, index)?,
})
}
pub(crate) fn custom_sublet_count() -> u32 {
snapshot().custom_sublets.count
}
pub(crate) fn exact_sublet_count() -> u32 {
snapshot().sublets.count
}