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
use super::{ValidResult, Validate};
use error::Error;
pub struct BankIdentificationCode {
value: String,
}
impl BankIdentificationCode {
pub fn new(input: &str) -> Self {
BankIdentificationCode {
value: input.into(),
}
}
}
impl Validate for BankIdentificationCode {
fn is_valid(&self) -> ValidResult {
if self.value.is_empty() {
return Err(Error::Empty);
}
if !super::only_digits(&self.value) {
return Err(Error::ExpectedNumbersOnly);
}
match self.value.len() {
9 => Ok(true),
_ => Err(Error::WrongLength { length: 9 }),
}
}
}
impl From<String> for BankIdentificationCode {
fn from(other: String) -> BankIdentificationCode {
BankIdentificationCode { value: other }
}
}
#[cfg(test)]
mod tests {
use error;
use super::*;
fn create_bank_identification_code(s: &str) -> BankIdentificationCode {
BankIdentificationCode::new(s)
}
#[test]
fn test_empty_bank_identification_code() {
match create_bank_identification_code("").is_valid() {
Err(error::Error::Empty) => assert!(true),
_ => assert!(false),
};
}
#[test]
fn test_invalid_bank_identification_code_8_numbers() {
match create_bank_identification_code("01234567").is_valid() {
Err(error::Error::WrongLength { length: _ }) => assert!(true),
_ => assert!(false),
};
}
#[test]
fn test_valid_bank_identification_code_9_zeros() {
assert!(
create_bank_identification_code("000000000")
.is_valid()
.unwrap()
);
}
#[test]
fn test_invalid_bank_identification_code_10_numbers() {
match create_bank_identification_code("0123456789").is_valid() {
Err(error::Error::WrongLength { length: _ }) => assert!(true),
_ => assert!(false),
};
}
#[test]
fn test_invalid_bank_identification_code_with_litters() {
match create_bank_identification_code("0000AZ000").is_valid() {
Err(error::Error::ExpectedNumbersOnly) => assert!(true),
_ => assert!(false),
};
}
#[test]
fn test_convert_from_string() {
let bic: BankIdentificationCode = "000000000".to_owned().into();
assert!(match bic.is_valid() {
Ok(true) => true,
_ => false,
})
}
}