mod search;
use crate::{
Bank, BankCode, BankKind, BankService, Branch, BranchRail, IsoSubdivision, ReportedStatus,
RoutingCode,
};
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum LocationField {
Centre,
District,
City,
State,
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum BranchTextField {
Any,
ReportedBankName,
BranchName,
Address,
Contact,
Centre,
District,
City,
State,
Micr,
Swift,
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum TextMatch {
Exact,
Prefix,
Contains,
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum SearchCase {
ExactBytes,
AsciiInsensitive,
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum BranchOrder {
Ifsc,
ReportedBankName,
BranchName,
StateCityBranch,
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum BankOrder {
Code,
Name,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct Text<'a, F> {
value: &'a str,
field: F,
kind: TextMatch,
case: SearchCase,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[must_use]
pub struct BranchTextQuery<'a>(Text<'a, BranchTextField>);
impl<'a> BranchTextQuery<'a> {
pub const fn new(value: &'a str) -> Self {
Self(Text {
value,
field: BranchTextField::Any,
kind: TextMatch::Contains,
case: SearchCase::AsciiInsensitive,
})
}
pub const fn with_field(mut self, field: BranchTextField) -> Self {
self.0.field = field;
self
}
pub const fn with_match(mut self, kind: TextMatch) -> Self {
self.0.kind = kind;
self
}
pub const fn with_case(mut self, case: SearchCase) -> Self {
self.0.case = case;
self
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[must_use]
pub struct BankNameQuery<'a> {
value: &'a str,
kind: TextMatch,
case: SearchCase,
}
impl<'a> BankNameQuery<'a> {
pub const fn new(value: &'a str) -> Self {
Self {
value,
kind: TextMatch::Contains,
case: SearchCase::AsciiInsensitive,
}
}
pub const fn with_match(mut self, kind: TextMatch) -> Self {
self.kind = kind;
self
}
pub const fn with_case(mut self, case: SearchCase) -> Self {
self.case = case;
self
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
#[must_use]
pub struct BranchQuery<'a> {
ifsc_prefix: Option<&'a str>,
routing: Option<RoutingCode>,
location: Option<(LocationField, &'a str)>,
iso: Option<IsoSubdivision>,
micr: Option<&'a str>,
swift: Option<&'a str>,
rail: Option<(BranchRail, bool)>,
exact: Option<bool>,
custom: Option<bool>,
sublet_bank: Option<BankCode>,
text: Option<BranchTextQuery<'a>>,
order: Option<BranchOrder>,
}
impl<'a> BranchQuery<'a> {
pub const fn new() -> Self {
Self {
ifsc_prefix: None,
routing: None,
location: None,
iso: None,
micr: None,
swift: None,
rail: None,
exact: None,
custom: None,
sublet_bank: None,
text: None,
order: None,
}
}
pub const fn with_ifsc_prefix(mut self, value: &'a str) -> Self {
self.ifsc_prefix = Some(value);
self
}
pub const fn with_routing_code(mut self, code: RoutingCode) -> Self {
self.routing = Some(code);
self
}
pub const fn with_location(mut self, field: LocationField, value: &'a str) -> Self {
self.location = Some((field, value));
self
}
pub const fn with_iso_subdivision(mut self, subdivision: IsoSubdivision) -> Self {
self.iso = Some(subdivision);
self
}
pub const fn with_micr(mut self, micr: &'a str) -> Self {
self.micr = Some(micr);
self
}
pub const fn with_swift(mut self, swift: &'a str) -> Self {
self.swift = Some(swift);
self
}
pub const fn with_reported_rail(mut self, rail: BranchRail, reported: bool) -> Self {
self.rail = Some((rail, reported));
self
}
pub const fn with_exact_sublet(mut self, present: bool) -> Self {
self.exact = Some(present);
self
}
pub const fn with_custom_sublet(mut self, present: bool) -> Self {
self.custom = Some(present);
self
}
pub const fn with_sublet_bank(mut self, bank: BankCode) -> Self {
self.sublet_bank = Some(bank);
self
}
pub const fn with_text(mut self, text: BranchTextQuery<'a>) -> Self {
self.text = Some(text);
self
}
pub const fn with_order(mut self, order: BranchOrder) -> Self {
self.order = Some(order);
self
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
#[must_use]
pub struct BankQuery<'a> {
kind: Option<BankKind>,
service: Option<(BankService, ReportedStatus)>,
name: Option<BankNameQuery<'a>>,
profile: Option<bool>,
order: Option<BankOrder>,
}
impl<'a> BankQuery<'a> {
pub const fn new() -> Self {
Self {
kind: None,
service: None,
name: None,
profile: None,
order: None,
}
}
pub const fn with_kind(mut self, kind: BankKind) -> Self {
self.kind = Some(kind);
self
}
pub const fn with_reported_service(
mut self,
service: BankService,
status: ReportedStatus,
) -> Self {
self.service = Some((service, status));
self
}
pub const fn with_name(mut self, name: BankNameQuery<'a>) -> Self {
self.name = Some(name);
self
}
pub const fn with_profile(mut self, present: bool) -> Self {
self.profile = Some(present);
self
}
pub const fn with_order(mut self, order: BankOrder) -> Self {
self.order = Some(order);
self
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[must_use]
pub struct Page {
offset: usize,
limit: usize,
}
impl Page {
pub const fn new() -> Self {
Self {
offset: 0,
limit: 100,
}
}
pub const fn with_offset(mut self, offset: usize) -> Self {
self.offset = offset;
self
}
pub const fn with_limit(mut self, limit: usize) -> Self {
self.limit = limit;
self
}
pub const fn offset(self) -> usize {
self.offset
}
pub const fn limit(self) -> usize {
self.limit
}
}
impl Default for Page {
fn default() -> Self {
Self::new()
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[must_use]
pub struct SearchOptions {
page: Page,
max_text_bytes: usize,
}
impl SearchOptions {
pub const fn new() -> Self {
Self {
page: Page::new(),
max_text_bytes: 256,
}
}
pub const fn with_page(mut self, page: Page) -> Self {
self.page = page;
self
}
pub const fn with_max_text_bytes(mut self, limit: usize) -> Self {
self.max_text_bytes = limit;
self
}
pub const fn page(self) -> Page {
self.page
}
pub const fn max_text_bytes(self) -> usize {
self.max_text_bytes
}
}
impl Default for SearchOptions {
fn default() -> Self {
Self::new()
}
}
pub struct BranchMatches {
iter: std::vec::IntoIter<Branch>,
total: usize,
offset: usize,
end: usize,
}
impl BranchMatches {
pub fn total_matches(&self) -> usize {
self.total
}
pub fn offset(&self) -> usize {
self.offset
}
pub fn next_offset(&self) -> Option<usize> {
(self.end < self.total).then_some(self.end)
}
}
impl Iterator for BranchMatches {
type Item = Branch;
fn next(&mut self) -> Option<Branch> {
self.iter.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
impl ExactSizeIterator for BranchMatches {}
impl core::iter::FusedIterator for BranchMatches {}
pub struct BankMatches {
iter: std::vec::IntoIter<Bank>,
total: usize,
offset: usize,
end: usize,
}
impl BankMatches {
pub fn total_matches(&self) -> usize {
self.total
}
pub fn offset(&self) -> usize {
self.offset
}
pub fn next_offset(&self) -> Option<usize> {
(self.end < self.total).then_some(self.end)
}
}
impl Iterator for BankMatches {
type Item = Bank;
fn next(&mut self) -> Option<Bank> {
self.iter.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
impl ExactSizeIterator for BankMatches {}
impl core::iter::FusedIterator for BankMatches {}