use core::fmt;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum BranchRail {
Neft,
Rtgs,
Imps,
Upi,
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct BranchRails(pub(crate) u8);
impl BranchRails {
pub const fn contains(self, rail: BranchRail) -> bool {
self.0 & bit(rail) != 0
}
pub const fn is_empty(self) -> bool {
self.0 == 0
}
pub const fn len(self) -> usize {
self.0.count_ones() as usize
}
pub fn iter(self) -> BranchRailIter {
BranchRailIter {
rails: self,
index: 0,
}
}
}
impl IntoIterator for BranchRails {
type Item = BranchRail;
type IntoIter = BranchRailIter;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
pub struct BranchRailIter {
rails: BranchRails,
index: u8,
}
impl Iterator for BranchRailIter {
type Item = BranchRail;
fn next(&mut self) -> Option<Self::Item> {
while self.index < 4 {
let rail = [
BranchRail::Neft,
BranchRail::Rtgs,
BranchRail::Imps,
BranchRail::Upi,
][self.index as usize];
self.index += 1;
if self.rails.contains(rail) {
return Some(rail);
}
}
None
}
fn size_hint(&self) -> (usize, Option<usize>) {
let remaining = (self.index..4)
.filter(|i| {
self.rails.contains(
[
BranchRail::Neft,
BranchRail::Rtgs,
BranchRail::Imps,
BranchRail::Upi,
][*i as usize],
)
})
.count();
(remaining, Some(remaining))
}
}
impl ExactSizeIterator for BranchRailIter {}
impl core::iter::FusedIterator for BranchRailIter {}
const fn bit(rail: BranchRail) -> u8 {
match rail {
BranchRail::Imps => 1,
BranchRail::Rtgs => 2,
BranchRail::Neft => 4,
BranchRail::Upi => 8,
}
}
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct IsoSubdivision(pub(crate) [u8; 5]);
impl IsoSubdivision {
pub fn as_str(&self) -> &str {
core::str::from_utf8(&self.0).expect("ISO subdivision invariant")
}
pub fn parse(input: &str) -> crate::Result<Self> {
if input.len() > 64 {
return Err(crate::Error::new(crate::ErrorKind::TooLong));
}
let input = input.trim_matches(|c: char| c.is_ascii_whitespace());
if input.len() != 5
|| !input.is_ascii()
|| !input[..2].eq_ignore_ascii_case("IN")
|| input.as_bytes()[2] != b'-'
|| !input[3..].bytes().all(|b| b.is_ascii_alphanumeric())
{
return Err(crate::Error::new(crate::ErrorKind::InvalidIsoSubdivision));
}
let mut bytes = [0; 5];
bytes.copy_from_slice(input.as_bytes());
bytes.make_ascii_uppercase();
Ok(Self(bytes))
}
pub fn parse_strict(input: &str) -> crate::Result<Self> {
let value = Self::parse(input)?;
if value.as_str() == input {
Ok(value)
} else {
Err(crate::Error::new(crate::ErrorKind::NotCanonical))
}
}
}
impl AsRef<str> for IsoSubdivision {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl fmt::Display for IsoSubdivision {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl fmt::Debug for IsoSubdivision {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("IsoSubdivision")
.field(&self.as_str())
.finish()
}
}
impl core::str::FromStr for IsoSubdivision {
type Err = crate::Error;
fn from_str(input: &str) -> crate::Result<Self> {
Self::parse(input)
}
}
#[cfg(feature = "serde")]
impl serde::Serialize for IsoSubdivision {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(self.as_str())
}
}
#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for IsoSubdivision {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let value = <std::borrow::Cow<'de, str> as serde::Deserialize>::deserialize(deserializer)?;
Self::parse_strict(&value).map_err(serde::de::Error::custom)
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct Branch(pub(crate) u32);
impl Branch {
fn row(self) -> crate::snapshot::BranchView {
crate::snapshot::branch(self.0).expect("valid branch handle")
}
pub fn ifsc(self) -> crate::Ifsc {
self.row().ifsc()
}
pub fn routing_code(self) -> crate::RoutingCode {
self.ifsc().routing_code()
}
pub fn reported_bank_name(self) -> Option<&'static str> {
self.row().text(0)
}
pub fn name(self) -> &'static str {
self.row().text(1).expect("generated branch name")
}
pub fn centre(self) -> &'static str {
self.row().text(2).expect("generated branch centre")
}
pub fn district(self) -> &'static str {
self.row().text(3).expect("generated branch district")
}
pub fn state(self) -> Option<&'static str> {
self.row().text(4)
}
pub fn address(self) -> &'static str {
self.row().text(5).expect("generated branch address")
}
pub fn contact(self) -> Option<&'static str> {
self.row().text(6)
}
pub fn city(self) -> &'static str {
self.row().text(7).expect("generated branch city")
}
pub fn iso_subdivision(self) -> Option<IsoSubdivision> {
self.row()
.text(8)
.map(|value| IsoSubdivision::parse_strict(value).expect("generated subdivision"))
}
pub fn micr(self) -> Option<&'static str> {
self.row().text(9)
}
pub fn swift(self) -> Option<&'static str> {
self.row().text(10)
}
pub fn reported_rails(self) -> BranchRails {
BranchRails(self.row().flags())
}
pub fn reports(self, rail: BranchRail) -> bool {
self.reported_rails().contains(rail)
}
pub fn routing_bank(self) -> Option<crate::Bank> {
crate::BankCode(self.routing_code().0).bank()
}
pub fn exact_sublet(self) -> Option<crate::SubletMatch> {
crate::directory().exact_sublet(self.ifsc())
}
pub fn custom_sublets(self) -> crate::CustomSubletMatches {
crate::directory().custom_sublets(self.ifsc())
}
}