ifsc 0.1.1

A fast offline directory for Indian bank branches and IFSCs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
mod search;

use crate::{
    Bank, BankCode, BankKind, BankService, Branch, BranchRail, IsoSubdivision, ReportedStatus,
    RoutingCode,
};

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
/// A source location field available to branch filters.
pub enum LocationField {
    /// The clearing or administrative centre.
    Centre,
    /// The district.
    District,
    /// The city.
    City,
    /// The state or union territory name.
    State,
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
/// A branch field available to text matching.
pub enum BranchTextField {
    /// All supported branch text fields.
    Any,
    /// The bank name reported on the branch row.
    ReportedBankName,
    /// The branch name.
    BranchName,
    /// The street address.
    Address,
    /// The contact value.
    Contact,
    /// The clearing or administrative centre.
    Centre,
    /// The district.
    District,
    /// The city.
    City,
    /// The state or union territory name.
    State,
    /// The MICR value.
    Micr,
    /// The SWIFT value.
    Swift,
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
/// How a text query matches a field value.
pub enum TextMatch {
    /// Match the complete field value.
    Exact,
    /// Match the start of the field value.
    Prefix,
    /// Match any substring of the field value.
    Contains,
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
/// Case handling for text comparisons.
pub enum SearchCase {
    /// Compare bytes exactly.
    ExactBytes,
    /// Ignore ASCII letter case.
    AsciiInsensitive,
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
/// Sort order for branch search results.
pub enum BranchOrder {
    /// Sort by IFSC code.
    Ifsc,
    /// Sort by reported bank name, then IFSC.
    ReportedBankName,
    /// Sort by branch name, then IFSC.
    BranchName,
    /// Sort by state, city, branch name, then IFSC.
    StateCityBranch,
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
/// Sort order for bank search results.
pub enum BankOrder {
    /// Sort by bank code.
    Code,
    /// Sort by bank name, then 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]
/// A configurable text condition for branch search.
pub struct BranchTextQuery<'a>(Text<'a, BranchTextField>);
impl<'a> BranchTextQuery<'a> {
    /// Creates a case-insensitive substring query across all branch text fields.
    pub const fn new(value: &'a str) -> Self {
        Self(Text {
            value,
            field: BranchTextField::Any,
            kind: TextMatch::Contains,
            case: SearchCase::AsciiInsensitive,
        })
    }
    /// Selects the branch field to search.
    pub const fn with_field(mut self, field: BranchTextField) -> Self {
        self.0.field = field;
        self
    }
    /// Selects how the query text matches the field.
    pub const fn with_match(mut self, kind: TextMatch) -> Self {
        self.0.kind = kind;
        self
    }
    /// Selects exact-byte or ASCII-insensitive comparison.
    pub const fn with_case(mut self, case: SearchCase) -> Self {
        self.0.case = case;
        self
    }
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[must_use]
/// A configurable text condition for bank-name search.
pub struct BankNameQuery<'a> {
    value: &'a str,
    kind: TextMatch,
    case: SearchCase,
}
impl<'a> BankNameQuery<'a> {
    /// Creates a case-insensitive substring query over bank names.
    pub const fn new(value: &'a str) -> Self {
        Self {
            value,
            kind: TextMatch::Contains,
            case: SearchCase::AsciiInsensitive,
        }
    }
    /// Selects how the query text matches the field.
    pub const fn with_match(mut self, kind: TextMatch) -> Self {
        self.kind = kind;
        self
    }
    /// Selects exact-byte or ASCII-insensitive comparison.
    pub const fn with_case(mut self, case: SearchCase) -> Self {
        self.case = case;
        self
    }
}

#[derive(Clone, Debug, Default, Eq, PartialEq)]
#[must_use]
/// Composable filters and ordering for branch search.
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> {
    /// Creates a query that matches every branch in IFSC order.
    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,
        }
    }
    /// Filters by a syntactically valid IFSC prefix, ignoring ASCII case.
    pub const fn with_ifsc_prefix(mut self, value: &'a str) -> Self {
        self.ifsc_prefix = Some(value);
        self
    }
    /// Filters by an exact routing code.
    pub const fn with_routing_code(mut self, code: RoutingCode) -> Self {
        self.routing = Some(code);
        self
    }
    /// Filters by an exact byte-for-byte source location value.
    pub const fn with_location(mut self, field: LocationField, value: &'a str) -> Self {
        self.location = Some((field, value));
        self
    }
    /// Filters by an ISO 3166-2 subdivision code.
    pub const fn with_iso_subdivision(mut self, subdivision: IsoSubdivision) -> Self {
        self.iso = Some(subdivision);
        self
    }
    /// Filters by an exact MICR value.
    pub const fn with_micr(mut self, micr: &'a str) -> Self {
        self.micr = Some(micr);
        self
    }
    /// Filters by a SWIFT value, ignoring ASCII case.
    pub const fn with_swift(mut self, swift: &'a str) -> Self {
        self.swift = Some(swift);
        self
    }
    /// Filters by whether a branch reports the selected rail.
    pub const fn with_reported_rail(mut self, rail: BranchRail, reported: bool) -> Self {
        self.rail = Some((rail, reported));
        self
    }
    /// Filters by whether an exact sublet mapping exists.
    pub const fn with_exact_sublet(mut self, present: bool) -> Self {
        self.exact = Some(present);
        self
    }
    /// Filters by whether a matching custom sublet rule exists.
    pub const fn with_custom_sublet(mut self, present: bool) -> Self {
        self.custom = Some(present);
        self
    }
    /// Filters branches with an exact or custom mapping to `bank`.
    pub const fn with_sublet_bank(mut self, bank: BankCode) -> Self {
        self.sublet_bank = Some(bank);
        self
    }
    /// Adds a configurable branch text condition.
    pub const fn with_text(mut self, text: BranchTextQuery<'a>) -> Self {
        self.text = Some(text);
        self
    }
    /// Selects the result sort order.
    pub const fn with_order(mut self, order: BranchOrder) -> Self {
        self.order = Some(order);
        self
    }
}

#[derive(Clone, Debug, Default, Eq, PartialEq)]
#[must_use]
/// Composable filters and ordering for bank search.
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> {
    /// Creates a query that matches every bank in code order.
    pub const fn new() -> Self {
        Self {
            kind: None,
            service: None,
            name: None,
            profile: None,
            order: None,
        }
    }
    /// Filters by bank classification.
    pub const fn with_kind(mut self, kind: BankKind) -> Self {
        self.kind = Some(kind);
        self
    }
    /// Filters by a source-reported service status.
    pub const fn with_reported_service(
        mut self,
        service: BankService,
        status: ReportedStatus,
    ) -> Self {
        self.service = Some((service, status));
        self
    }
    /// Adds a configurable bank-name condition.
    pub const fn with_name(mut self, name: BankNameQuery<'a>) -> Self {
        self.name = Some(name);
        self
    }
    /// Filters by whether source-reported bank metadata exists.
    pub const fn with_profile(mut self, present: bool) -> Self {
        self.profile = Some(present);
        self
    }
    /// Selects the result sort order.
    pub const fn with_order(mut self, order: BankOrder) -> Self {
        self.order = Some(order);
        self
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[must_use]
/// Zero-based offset and maximum result count for one search page.
pub struct Page {
    offset: usize,
    limit: usize,
}
impl Page {
    /// Creates the default page with offset zero and limit 100.
    pub const fn new() -> Self {
        Self {
            offset: 0,
            limit: 100,
        }
    }
    /// Sets the zero-based result offset.
    pub const fn with_offset(mut self, offset: usize) -> Self {
        self.offset = offset;
        self
    }
    /// Sets the maximum number of returned results.
    ///
    /// Search rejects limits outside `1..=10_000` with
    /// [`ErrorKind::InvalidPage`](crate::ErrorKind::InvalidPage).
    pub const fn with_limit(mut self, limit: usize) -> Self {
        self.limit = limit;
        self
    }
    /// Returns the zero-based result offset.
    pub const fn offset(self) -> usize {
        self.offset
    }
    /// Returns the maximum number of returned results.
    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]
/// Resource limits and paging controls for a search.
pub struct SearchOptions {
    page: Page,
    max_text_bytes: usize,
}
impl SearchOptions {
    /// Creates options for the default page and 256-byte text limit.
    pub const fn new() -> Self {
        Self {
            page: Page::new(),
            max_text_bytes: 256,
        }
    }
    /// Selects the requested result page.
    pub const fn with_page(mut self, page: Page) -> Self {
        self.page = page;
        self
    }
    /// Sets the maximum accepted byte length for each text query value.
    pub const fn with_max_text_bytes(mut self, limit: usize) -> Self {
        self.max_text_bytes = limit;
        self
    }
    /// Returns the requested result page.
    pub const fn page(self) -> Page {
        self.page
    }
    /// Returns the maximum accepted text query length in bytes.
    pub const fn max_text_bytes(self) -> usize {
        self.max_text_bytes
    }
}
impl Default for SearchOptions {
    fn default() -> Self {
        Self::new()
    }
}

/// An exact-size iterator over one page of branch matches.
///
/// Paging metadata does not change as the iterator is consumed.
pub struct BranchMatches {
    iter: std::vec::IntoIter<Branch>,
    total: usize,
    offset: usize,
    end: usize,
}
impl BranchMatches {
    /// Returns the number of matches before paging.
    pub fn total_matches(&self) -> usize {
        self.total
    }
    /// Returns the requested page offset.
    pub fn offset(&self) -> usize {
        self.offset
    }
    /// Returns the offset of the next page, if more matches remain.
    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 {}
/// An exact-size iterator over one page of bank matches.
///
/// Paging metadata does not change as the iterator is consumed.
pub struct BankMatches {
    iter: std::vec::IntoIter<Bank>,
    total: usize,
    offset: usize,
    end: usize,
}
impl BankMatches {
    /// Returns the number of matches before paging.
    pub fn total_matches(&self) -> usize {
        self.total
    }
    /// Returns the requested page offset.
    pub fn offset(&self) -> usize {
        self.offset
    }
    /// Returns the offset of the next page, if more matches remain.
    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 {}