vaultparser 0.4.0

A fast, pure-Rust library and visual tool for extracting tabular transaction ledgers from PDF bank statements
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
use crate::config::ExtractionConfig;

/// Supported out-of-the-box bank statement templates.
///
/// Each preset contains standard column coordinates and label mappings
/// for common Indian banks.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum BankPreset {
    /// HDFC Bank India statement template.
    Hdfc,
    /// State Bank of India (SBI) statement template.
    Sbi,
    /// Canara Bank statement template.
    Canara,
    /// Union Bank of India statement template.
    Union,
    /// UCO Bank statement template.
    Uco,
    /// Indian Bank statement template.
    Indian,
    /// H P State Co-operative Bank statement template.
    Hpscb,
    /// ICICI Bank statement template.
    Icici,
    /// Punjab National Bank (PNB) statement template.
    Pnb,
    /// Kotak Mahindra Bank statement template.
    Kotak,
    /// Axis Bank statement template.
    Axis,
    /// Bank of Baroda statement template.
    Bob,
    /// YES Bank statement template.
    Yes,
    /// IDFC FIRST Bank statement template.
    Idfc,
    /// IndusInd Bank statement template.
    Indusind,
    /// Himachal Pradesh Gramin Bank statement template.
    Hpgb,
    /// Ujjivan Small Finance Bank statement template.
    Ujjivan,
}

impl std::fmt::Display for BankPreset {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.name())
    }
}

impl BankPreset {
    /// Returns the human-readable display name of the bank preset.
    #[must_use]
    pub fn name(&self) -> &'static str {
        match self {
            Self::Hdfc => "HDFC Bank India",
            Self::Sbi => "State Bank of India (SBI)",
            Self::Canara => "Canara Bank",
            Self::Union => "Union Bank of India",
            Self::Uco => "UCO Bank",
            Self::Indian => "Indian Bank",
            Self::Hpscb => "H P State Co-operative Bank",
            Self::Icici => "ICICI Bank",
            Self::Pnb => "Punjab National Bank (PNB)",
            Self::Kotak => "Kotak Mahindra Bank",
            Self::Axis => "Axis Bank",
            Self::Bob => "Bank of Baroda",
            Self::Yes => "YES Bank",
            Self::Idfc => "IDFC FIRST Bank",
            Self::Indusind => "IndusInd Bank",
            Self::Hpgb => "Himachal Pradesh Gramin Bank",
            Self::Ujjivan => "Ujjivan Small Finance Bank",
        }
    }

    /// Returns the pre-configured [`ExtractionConfig`] for the specific bank.
    ///
    /// # Examples
    /// ```
    /// use vaultparser::BankPreset;
    ///
    /// let config = BankPreset::Hdfc.config();
    /// assert!(config.filter_only_date);
    /// assert_eq!(config.col_mappings.len(), config.col_guides.len() + 1);
    /// ```
    #[must_use]
    pub fn config(&self) -> ExtractionConfig {
        let mut config = ExtractionConfig::default();
        match self {
            Self::Hdfc => {
                config.col_guides = vec![0.11, 0.42, 0.52, 0.62, 0.75, 0.88];
                config.col_mappings = vec![
                    "date".to_string(),
                    "description".to_string(),
                    "reference".to_string(),
                    "skip".to_string(),
                    "debit".to_string(),
                    "credit".to_string(),
                    "balance".to_string(),
                ];
                config.filter_only_date = true;
            }
            Self::Sbi => {
                config.col_guides = vec![0.10, 0.20, 0.52, 0.64, 0.76, 0.88];
                config.col_mappings = vec![
                    "date".to_string(),
                    "skip".to_string(),
                    "description".to_string(),
                    "reference".to_string(),
                    "debit".to_string(),
                    "credit".to_string(),
                    "balance".to_string(),
                ];
                config.filter_only_date = true;
                config.y_tolerance = 15.0;
            }
            Self::Canara => {
                config.col_guides = vec![0.12, 0.48, 0.60, 0.74, 0.87];
                config.col_mappings = vec![
                    "date".to_string(),
                    "description".to_string(),
                    "reference".to_string(),
                    "debit".to_string(),
                    "credit".to_string(),
                    "balance".to_string(),
                ];
                config.filter_only_date = true;
            }
            Self::Union => {
                config.col_guides = vec![0.11, 0.42, 0.52, 0.62, 0.75, 0.88];
                config.col_mappings = vec![
                    "date".to_string(),
                    "description".to_string(),
                    "reference".to_string(),
                    "skip".to_string(),
                    "debit".to_string(),
                    "credit".to_string(),
                    "balance".to_string(),
                ];
                config.filter_only_date = true;
            }
            Self::Uco => {
                config.col_guides = vec![0.12, 0.24, 0.60, 0.74, 0.87];
                config.col_mappings = vec![
                    "date".to_string(),
                    "reference".to_string(),
                    "description".to_string(),
                    "debit".to_string(),
                    "credit".to_string(),
                    "balance".to_string(),
                ];
                config.filter_only_date = true;
            }
            Self::Indian => {
                config.col_guides = vec![0.08, 0.18, 0.45, 0.58, 0.71, 0.80];
                config.col_mappings = vec![
                    "date".to_string(),
                    "value_date".to_string(),
                    "description".to_string(),
                    "chq_no".to_string(),
                    "debit".to_string(),
                    "credit".to_string(),
                    "balance".to_string(),
                ];
                config.filter_only_date = true;
                config.y_tolerance = 15.0;
            }
            Self::Hpscb => {
                config.col_guides = vec![0.08, 0.16, 0.38, 0.41, 0.65, 0.74, 0.82];
                config.col_mappings = vec![
                    "s_no".to_string(),
                    "date".to_string(),
                    "value_date".to_string(),
                    "chq_no".to_string(),
                    "description".to_string(),
                    "debit".to_string(),
                    "credit".to_string(),
                    "balance".to_string(),
                ];
                config.filter_only_date = true;
                config.y_tolerance = 15.0;
            }
            Self::Icici => {
                config.col_guides = vec![0.08, 0.44, 0.55, 0.62, 0.88];
                config.col_mappings = vec![
                    "date".to_string(),
                    "description".to_string(),
                    "chq_no".to_string(),
                    "debit".to_string(),
                    "credit".to_string(),
                    "balance".to_string(),
                ];
                config.filter_only_date = true;
                config.y_tolerance = 12.0;
            }
            Self::Pnb => {
                config.col_guides = vec![0.14, 0.23, 0.33, 0.40, 0.47, 0.53];
                config.col_mappings = vec![
                    "date".to_string(),
                    "debit".to_string(),
                    "credit".to_string(),
                    "balance".to_string(),
                    "skip".to_string(),
                    "chq_no".to_string(),
                    "description".to_string(),
                ];
                config.filter_only_date = true;
                config.y_tolerance = 12.0;
            }
            Self::Kotak => {
                config.col_guides = vec![0.15, 0.44, 0.65, 0.79];
                config.col_mappings = vec![
                    "date".to_string(),
                    "description".to_string(),
                    "reference".to_string(),
                    "debit".to_string(),
                    "balance".to_string(),
                ];
                config.filter_only_date = true;
                config.y_tolerance = 12.0;
            }
            Self::Axis => {
                config.col_guides = vec![0.12, 0.42, 0.55, 0.68, 0.82];
                config.col_mappings = vec![
                    "date".to_string(),
                    "description".to_string(),
                    "chq_no".to_string(),
                    "debit".to_string(),
                    "credit".to_string(),
                    "balance".to_string(),
                ];
                config.filter_only_date = true;
                config.y_tolerance = 12.0;
            }
            Self::Bob => {
                config.col_guides = vec![0.10, 0.20, 0.50, 0.65, 0.78, 0.88];
                config.col_mappings = vec![
                    "date".to_string(),
                    "value_date".to_string(),
                    "description".to_string(),
                    "chq_no".to_string(),
                    "debit".to_string(),
                    "credit".to_string(),
                    "balance".to_string(),
                ];
                config.filter_only_date = true;
                config.y_tolerance = 15.0;
            }
            Self::Yes => {
                config.col_guides = vec![0.12, 0.45, 0.58, 0.72, 0.85];
                config.col_mappings = vec![
                    "date".to_string(),
                    "description".to_string(),
                    "reference".to_string(),
                    "debit".to_string(),
                    "credit".to_string(),
                    "balance".to_string(),
                ];
                config.filter_only_date = true;
                config.y_tolerance = 12.0;
            }
            Self::Idfc => {
                config.col_guides = vec![0.11, 0.40, 0.54, 0.68, 0.82];
                config.col_mappings = vec![
                    "date".to_string(),
                    "description".to_string(),
                    "reference".to_string(),
                    "debit".to_string(),
                    "credit".to_string(),
                    "balance".to_string(),
                ];
                config.filter_only_date = true;
                config.y_tolerance = 12.0;
            }
            Self::Indusind => {
                config.col_guides = vec![0.12, 0.44, 0.56, 0.70, 0.84];
                config.col_mappings = vec![
                    "date".to_string(),
                    "description".to_string(),
                    "chq_no".to_string(),
                    "debit".to_string(),
                    "credit".to_string(),
                    "balance".to_string(),
                ];
                config.filter_only_date = true;
                config.y_tolerance = 12.0;
            }
            Self::Hpgb => {
                config.col_guides = vec![0.06, 0.21, 0.45, 0.58, 0.74];
                config.col_mappings = vec![
                    "date".to_string(),
                    "reference".to_string(),
                    "description".to_string(),
                    "debit".to_string(),
                    "credit".to_string(),
                    "balance".to_string(),
                ];
                config.filter_only_date = true;
                config.y_tolerance = 10.0;
            }
            Self::Ujjivan => {
                config.col_guides = vec![0.125, 0.279, 0.529, 0.669, 0.806];
                config.col_mappings = vec![
                    "date".to_string(),
                    "value_date".to_string(),
                    "description".to_string(),
                    "debit".to_string(),
                    "credit".to_string(),
                    "balance".to_string(),
                ];
                config.filter_only_date = true;
                config.y_tolerance = 8.0;
                config.y_top_trim = 0.0;
                config.y_bottom_trim = 0.98;
            }
        }
        config
    }

    /// Returns the lowercase machine key for this bank preset.
    #[must_use]
    pub fn key(&self) -> &'static str {
        match self {
            Self::Hdfc => "hdfc",
            Self::Sbi => "sbi",
            Self::Canara => "canara",
            Self::Union => "union",
            Self::Uco => "uco",
            Self::Indian => "indian",
            Self::Hpscb => "hpscb",
            Self::Icici => "icici",
            Self::Pnb => "pnb",
            Self::Kotak => "kotak",
            Self::Axis => "axis",
            Self::Bob => "bob",
            Self::Yes => "yes",
            Self::Idfc => "idfc",
            Self::Indusind => "indusind",
            Self::Hpgb => "hpgb",
            Self::Ujjivan => "ujjivan",
        }
    }

    /// Returns a list of all supported bank presets.
    #[must_use]
    pub fn all() -> &'static [BankPreset] {
        &[
            Self::Hdfc,
            Self::Sbi,
            Self::Canara,
            Self::Union,
            Self::Uco,
            Self::Indian,
            Self::Hpscb,
            Self::Icici,
            Self::Pnb,
            Self::Kotak,
            Self::Axis,
            Self::Bob,
            Self::Yes,
            Self::Idfc,
            Self::Indusind,
            Self::Hpgb,
            Self::Ujjivan,
        ]
    }

    /// Attempts to parse a case-insensitive string into a [`BankPreset`].
    /// Returns `None` if the name is unrecognized.
    ///
    /// # Examples
    /// ```
    /// use vaultparser::BankPreset;
    ///
    /// assert_eq!(BankPreset::from_str("hdfc"), Some(BankPreset::Hdfc));
    /// assert_eq!(BankPreset::from_str("HDFC"), Some(BankPreset::Hdfc));
    /// assert_eq!(BankPreset::from_str("nope"), None);
    /// ```
    #[allow(clippy::should_implement_trait)]
    #[must_use]
    pub fn from_str(s: &str) -> Option<Self> {
        s.parse::<Self>().ok()
    }
}

impl std::str::FromStr for BankPreset {
    type Err = crate::error::ExtractorError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "hdfc" => Ok(Self::Hdfc),
            "sbi" => Ok(Self::Sbi),
            "canara" => Ok(Self::Canara),
            "union" => Ok(Self::Union),
            "uco" => Ok(Self::Uco),
            "indian" => Ok(Self::Indian),
            "hpscb" => Ok(Self::Hpscb),
            "icici" => Ok(Self::Icici),
            "pnb" => Ok(Self::Pnb),
            "kotak" => Ok(Self::Kotak),
            "axis" => Ok(Self::Axis),
            "bob" | "baroda" => Ok(Self::Bob),
            "yes" => Ok(Self::Yes),
            "idfc" => Ok(Self::Idfc),
            "indusind" => Ok(Self::Indusind),
            "hpgb" | "himachal pradesh gramin bank" | "himachal pradesh gramin" | "hpgramin" => {
                Ok(Self::Hpgb)
            }
            "ujjivan" | "ujjivan small finance bank" | "ujjivan small finance" => Ok(Self::Ujjivan),
            _ => Err(crate::error::ExtractorError::InvalidConfig(format!(
                "Unknown bank preset: '{}'",
                s
            ))),
        }
    }
}