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
#![forbid(unsafe_code)]
#![warn(missing_debug_implementations)]
#![warn(missing_docs)]
pub use crate::country::Country;
pub use crate::error::ValidationError;
use std::str::FromStr;
pub mod country;
pub(crate) mod country_specific;
pub mod error;
#[allow(clippy::all)]
pub(crate) mod u256 {
uint::construct_uint! {
pub(crate) struct U256(4);
}
}
#[derive(Debug)]
#[non_exhaustive]
pub struct Iban {
pub country: Country,
pub(crate) raw: String,
}
impl Iban {
pub fn account_number(&self) -> String {
self.country.account_number(&self.raw)
}
pub fn bank_code(&self) -> String {
self.country.bank_code(&self.raw)
}
pub fn country_code(&self) -> String {
self.country.to_string()
}
pub fn raw(&self) -> &str {
&self.raw
}
}
pub fn validate<I: AsRef<str>>(input: I) -> Result<Iban, ValidationError> {
let input = input.as_ref();
let input: String = input.split_whitespace().collect();
if input.len() > 34 {
return Err(ValidationError::TooLong);
};
if !input.chars().all(|ch| ch.is_alphanumeric()) {
return Err(ValidationError::InvalidChar);
};
let country_code = &input[0..2];
let country = match Country::from_str(country_code) {
Ok(c) => c,
Err(_) => return Err(ValidationError::InvalidCountryCode),
};
if input.len() != country.length() {
return Err(ValidationError::InvalidLength);
}
if !country.format().is_match(&input) {
return Err(ValidationError::InvalidFormat);
}
if !country.custom_validation(&input) {
return Err(ValidationError::CountryCheckFailed);
}
let (start, rest) = input.split_at(4);
let mut rearranged = String::with_capacity(34);
rearranged.push_str(rest);
rearranged.push_str(start);
let mut integer_string = String::with_capacity(34);
for ch in rearranged.chars() {
if ch.is_numeric() {
integer_string.push(ch);
} else {
let x = ch.to_digit(36).unwrap().to_string();
integer_string.push_str(&x)
}
}
let integer = u256::U256::from_dec_str(&integer_string).unwrap();
if integer % 97 != 1.into() {
return Err(ValidationError::InvalidIban);
}
Ok(Iban {
country,
raw: input,
})
}