cemtexer 0.1.3

An utility for generating and validating Australian Banking Association Cemtex file format
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
#![allow(clippy::single_char_pattern)]

//! Validation utility functions for csv files
use lazy_static::lazy_static;
use regex::Regex;
use serde::{Deserialize, Deserializer};
use std::ops::Sub;

use crate::parser_utils::*;
use crate::types::*;

lazy_static! {
    static ref RE_AMOUNT: Regex = Regex::new(r"^[[:digit:]]{1,8}\.[[:digit:]]{2}$").unwrap();
}

/// Custom deserialisation function for the comment field, blank filled
pub fn optional_comment<'de, D>(de: D) -> Result<Option<String>, D::Error>
where
    D: Deserializer<'de>,
    Option<String>: Deserialize<'de>,
{
    match Option::<String>::deserialize(de) {
        Ok(Some(comment)) => Ok(Some(comment)),
        Ok(None) => Ok(Some("                  ".to_string())),
        _ => unreachable!(),
    }
}

/// Custom deserialisation function for the tax withold field, zero filled
pub fn optional_tax_withhold<'de, D>(de: D) -> Result<Option<String>, D::Error>
where
    D: Deserializer<'de>,
    Option<String>: Deserialize<'de>,
{
    match Option::<String>::deserialize(de) {
        Ok(Some(comment)) => Ok(Some(comment)),
        Ok(None) => Ok(Some("00000000".to_string())),
        _ => unreachable!(),
    }
}

/// Left adjust any field as mandated by .aba format
pub fn left_adjust(i: &String, size: usize, strat: FillStrategy) -> String {
    let mut res: String = String::new();

    match strat {
        FillStrategy::Zero => {
            if i.len().lt(&size) {
                let fill = zero_fill(i.to_string(), size);
                res.push_str(i);
                res.push_str(&fill);
                res
            } else {
                i.to_string()
            }
        }
        FillStrategy::Blank => {
            if i.len().lt(&size) {
                let fill = blank_fill(i.to_string(), size);
                res.push_str(i);
                res.push_str(&fill);
                res
            } else {
                i.to_string()
            }
        }
    }
}

/// Right adjust any field as mandated by .aba format
pub fn right_adjust(i: &String, size: usize, strat: FillStrategy) -> String {
    let mut res: String = String::new();

    match strat {
        FillStrategy::Zero => {
            if i.len().lt(&size) {
                let fill = zero_fill(i.to_string(), size);
                res.push_str(&fill);
                res.push_str(i);
                res
            } else {
                i.to_string()
            }
        }
        FillStrategy::Blank => {
            if i.len().lt(&size) {
                let fill = blank_fill(i.to_string(), size);
                res.push_str(&fill);
                res.push_str(i);
                res
            } else {
                i.to_string()
            }
        }
    }
}

fn zero_fill(i: String, size: usize) -> String {
    let remaining = size.sub(i.len());
    "0".repeat(remaining)
}

fn blank_fill(i: String, size: usize) -> String {
    let remaining = size.sub(i.len());
    " ".repeat(remaining)
}

pub fn normalise_amount(i: String) -> String {
    if i.contains(".") && RE_AMOUNT.is_match(&i) {
        i.replace(".", "")
    } else {
        i
    }
}

pub fn validate_csv_bank_name(i: &str, res: &mut Vec<String>) -> bool {
    if i.is_empty() || i.len().gt(&3usize) {
        res.push(
            "Bank name field must be in the excat format of 3 upppercase characters".to_string(),
        );
        return false;
    }

    let file = include_str!("../data/institution");
    if !file.contains(i) {
        res.push("Bank name field is not valid".to_string());
        return false;
    }
    true
}

pub fn validate_csv_user_name(i: &str, res: &mut Vec<String>) -> bool {
    if i.is_empty() || i.len().gt(&26usize) {
        res.push("User name field must not be empty and exceed 26 characters".to_string());
        return false;
    }
    true
}

pub fn validate_csv_apca_number(i: &str, res: &mut Vec<String>) -> bool {
    if i.len().gt(&6usize) || !validate_number(i) {
        res.push("APCA code field must be in the excat format of 6 digits".to_string());
        return false;
    }
    true
}

pub fn validate_csv_file_description(i: &str, res: &mut Vec<String>) -> bool {
    if i.is_empty() || i.len().gt(&12usize) {
        res.push("File description field must not be empty and exceed 12 characters".to_string());
        return false;
    }
    true
}

pub fn validate_csv_settle_date(i: &str, res: &mut Vec<String>) -> bool {
    if i.is_empty() || i.len().gt(&6usize) || !validate_date_format(i) {
        res.push("Settlement date field must be in the exact format of DDMMYY".to_string());
        return false;
    }
    true
}

pub fn validate_bsb(i: &str, res: &mut Vec<String>, bsb_type: BsbType) -> bool {
    if i.is_empty() || !i.len().eq(&7usize) {
        match bsb_type {
            BsbType::DetailBsb => res
                .push("BSB field must be in the format of xxx-xxx where x are digits".to_string()),
            BsbType::DetailTraceBsb => res.push(
                "Trace BSB code field must be in the excat format of xxx-xxx where x are digits"
                    .to_string(),
            ),
        }
        return false;
    }

    let file = include_str!("../data/bsb");
    if !file.contains(i) {
        res.push("BSB code is not valid".to_string());
        return false;
    }
    true
}

pub fn validate_account_number(i: &str, res: &mut Vec<String>, bsb_type: BsbType) -> bool {
    if i.is_empty() || i.len().gt(&9usize) || !validate_number(i.trim_start_matches(' ')) {
        match bsb_type {
            BsbType::DetailBsb => {
                res.push("Account number field must not be empty and exceed 9 digits".to_string())
            }
            BsbType::DetailTraceBsb => res.push(
                "Trace account number field must not be empty and exceed 9 digits".to_string(),
            ),
        }
        return false;
    }
    true
}

pub fn validate_csv_trace_account_name(i: &str, res: &mut Vec<String>) -> bool {
    if i.is_empty() || i.len().gt(&16usize) {
        res.push("Trace ccount name field must not be empty and exceed 12 characters".to_string());
        return false;
    }
    true
}

pub fn validate_csv_client_name(i: &str, res: &mut Vec<String>) -> bool {
    if i.is_empty() || i.len().gt(&32usize) {
        res.push("Account name field must not be empty and exceed 32 characters".to_string());
        return false;
    }
    true
}

pub fn validate_csv_amount(i: &str, res: &mut Vec<String>) -> bool {
    if i.is_empty()
        || i.replace(".", "").len().gt(&10usize)
        || !validate_number(&i.replace(".", ""))
    {
        res.push(
            "Amount field must be digits and must not be empty and exceed 10 digits".to_string(),
        );
        false
    } else if i.contains(".") {
        match RE_AMOUNT.is_match(i) {
            true => true,
            false => {
                res.push("Amount field must be in valid two deicmal format".to_string());
                false
            }
        }
    } else {
        true
    }
}

pub fn validate_csv_comment(i: &str, res: &mut Vec<String>) -> bool {
    if i.len().gt(&18usize) || i.starts_with('0') || i.starts_with('-') {
        res.push("Comment field must not exceed 18 characters or start with 0 and -".to_string());
        return false;
    }
    true
}

pub fn validate_csv_tax_withhold(i: &str, res: &mut Vec<String>) -> bool {
    if i.replace(".", "").len().gt(&8usize) || !validate_number(&i.replace(".", "")) {
        res.push("Tax withold field must be digits and must not exceed 8 digits".to_string());
        false
    } else if i.contains(".") {
        match RE_AMOUNT.is_match(i) {
            true => true,
            false => {
                res.push("Tax withold field must be in valid two deicmal format".to_string());
                false
            }
        }
    } else {
        true
    }
}

#[test]
fn test_normalise_amount() {
    let i = "123.45".to_string();
    assert_eq!(normalise_amount(i), "12345");
}

#[test]
fn test_left_adjust() {
    let i = "      ".to_string();
    assert_eq!(left_adjust(&i, 7usize, FillStrategy::Blank), "       ")
}

#[test]
fn test_right_adjust() {
    let i = "000000".to_string();
    assert_eq!(right_adjust(&i, 7usize, FillStrategy::Zero), "0000000")
}

#[test]
fn test_zero_fill() {
    let i = "1".to_string();
    assert_eq!(zero_fill(i, 7usize), "000000")
}

#[test]
fn test_blank_fill() {
    let i = "a".to_string();
    assert_eq!(blank_fill(i, 7usize), "      ")
}

#[test]
fn test_validate_csv_bank_name() {
    let blank: &str = "";
    let too_long: &str = "lolol";
    let non_exist: &str = "lol";
    let mut res: Vec<String> = Vec::new();
    assert_eq!(validate_csv_bank_name(blank, &mut res), false);
    assert_eq!(validate_csv_bank_name(too_long, &mut res), false);
    assert_eq!(validate_csv_bank_name(non_exist, &mut res), false)
}

#[test]
fn test_validate_csv_user_name() {
    let blank: &str = "";
    let too_long: &str = "lololololololololololololol";
    let mut res: Vec<String> = Vec::new();
    assert_eq!(validate_csv_user_name(blank, &mut res), false);
    assert_eq!(validate_csv_user_name(too_long, &mut res), false)
}

#[test]
fn test_validate_csv_apca_number() {
    let non_digits: &str = "lololo";
    let too_long: &str = "1234567";
    let mut res: Vec<String> = Vec::new();
    assert_eq!(validate_csv_apca_number(non_digits, &mut res), false);
    assert_eq!(validate_csv_apca_number(too_long, &mut res), false)
}

#[test]
fn test_validate_csv_file_description() {
    let blank: &str = "";
    let too_long: &str = "lolololololol";
    let mut res: Vec<String> = Vec::new();
    assert_eq!(validate_csv_file_description(blank, &mut res), false);
    assert_eq!(validate_csv_file_description(too_long, &mut res), false)
}

#[test]
fn test_validate_csv_settle_date() {
    let blank: &str = "";
    let too_long: &str = "1111111";
    let non_date: &str = "300220";
    let mut res: Vec<String> = Vec::new();
    assert_eq!(validate_csv_settle_date(blank, &mut res), false);
    assert_eq!(validate_csv_settle_date(too_long, &mut res), false);
    assert_eq!(validate_csv_settle_date(non_date, &mut res), false)
}

#[test]
fn test_validate_csv_trace() {
    let blank: &str = "";
    let invalid_bsb: &str = "123-456";
    let too_longbsb: &str = "123-4567";
    let acct: &str = "1234567890";
    let bad_acct: &str = "lol";
    let too_long_trace: &str = "lolololololololol";
    let too_long_client: &str = "lolololololololololololololololol";
    let mut res: Vec<String> = Vec::new();
    assert_eq!(validate_bsb(blank, &mut res, BsbType::DetailBsb), false);
    assert_eq!(
        validate_bsb(invalid_bsb, &mut res, BsbType::DetailBsb),
        false
    );
    assert_eq!(
        validate_bsb(too_longbsb, &mut res, BsbType::DetailBsb),
        false
    );
    assert_eq!(
        validate_account_number(blank, &mut res, BsbType::DetailBsb),
        false
    );
    assert_eq!(
        validate_account_number(acct, &mut res, BsbType::DetailBsb),
        false
    );
    assert_eq!(
        validate_account_number(bad_acct, &mut res, BsbType::DetailBsb),
        false
    );
    assert_eq!(validate_csv_trace_account_name(blank, &mut res), false);
    assert_eq!(
        validate_csv_trace_account_name(too_long_trace, &mut res),
        false
    );
    assert_eq!(validate_csv_client_name(blank, &mut res), false);
    assert_eq!(validate_csv_client_name(too_long_client, &mut res), false);
}

#[test]
fn test_validate_csv_amount() {
    let blank: &str = "";
    let too_long: &str = "11111111111";
    let non_digits: &str = "lol";
    let three_decimal: &str = "123.456";
    let mut res: Vec<String> = Vec::new();
    assert_eq!(validate_csv_amount(blank, &mut res), false);
    assert_eq!(validate_csv_amount(too_long, &mut res), false);
    assert_eq!(validate_csv_amount(non_digits, &mut res), false);
    assert_eq!(validate_csv_amount(three_decimal, &mut res), false);
}

#[test]
fn test_validate_csv_comment() {
    let start_with_hyphen: &str = "-lol";
    let start_with_zero: &str = "0lol";
    let too_long: &str = "lololololololololol";
    let mut res: Vec<String> = Vec::new();
    assert_eq!(validate_csv_comment(start_with_hyphen, &mut res), false);
    assert_eq!(validate_csv_comment(start_with_zero, &mut res), false);
    assert_eq!(validate_csv_comment(too_long, &mut res), false)
}

#[test]
fn test_validate_csv_tax_withhold() {
    let non_digits: &str = "lol";
    let three_decimal: &str = "123.456";
    let too_long: &str = "111111111";
    let mut res: Vec<String> = Vec::new();
    assert_eq!(validate_csv_tax_withhold(non_digits, &mut res), false);
    assert_eq!(validate_csv_tax_withhold(three_decimal, &mut res), false);
    assert_eq!(validate_csv_tax_withhold(too_long, &mut res), false)
}