schwifty/
country_specific.rs1use crate::country::Country;
2
3impl Country {
4 pub(crate) fn custom_validation(&self, input: &str) -> bool {
5 use Country::*;
6 match self {
7 Albania => {
8 let check_digit = self.check_digits(input);
9 let account_number = &input[4..=11];
10
11 let mut total = 0;
12 for (ch, w) in account_number.chars().zip([9, 7, 3, 1, 9, 7, 3, 1].iter()) {
13 let ch = ch.to_digit(10).unwrap();
14 total += ch * w;
15 }
16
17 total % 10 == check_digit
18 }
19 Belgium => {
20 let check_digits = self.check_digits(input) as u128;
21 let check_number: u128 = input[4..=13].parse().unwrap();
22 check_number % 97 == check_digits
23 }
24 CzechRepublic => {
25 let account_number = &input[14..];
26
27 let mut total = 0;
28 for (ch, w) in account_number
29 .chars()
30 .zip([6, 3, 7, 9, 10, 5, 8, 4, 2, 1].iter())
31 {
32 let ch = ch.to_digit(10).unwrap();
33 total += ch * w;
34 }
35
36 if total % 11 != 0 {
37 return false;
38 }
39
40 let branch_number = &input[8..=13];
41 let mut total = 0;
42 for (ch, w) in branch_number.chars().zip([10, 5, 8, 4, 2, 1].iter()) {
43 let ch = ch.to_digit(10).unwrap();
44 total += ch * w;
45 }
46
47 total % 11 == 0
48 }
49 _ => true,
50 }
51 }
52
53 pub(crate) fn check_digits(&self, input: &str) -> u32 {
54 use Country::*;
55 let end = input.len() - 1;
56 let (start, stop) = match self {
57 Albania => (12, 12),
58 Belgium | Montenegro => (end - 1, end),
59 _ => unreachable!(),
60 };
61
62 input[start..=stop].parse().unwrap()
63 }
64}