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
#[macro_use] extern crate failure;
#[macro_use] extern crate lazy_static;
extern crate regex;
use failure::Error;
use regex::Regex;
use std::collections::HashMap;
#[derive(Debug, PartialEq)]
pub struct VATID {
pub state_iso : String,
pub local_vat_id : String
}
lazy_static! {
static ref RX_GENERAL : Regex = Regex::new("^([A-Z]{2})(.+)$").unwrap();
static ref RXS_COUNTRIES: HashMap<String, Regex> = {
let mut m = HashMap::new();
m.insert(String::from("AT"), Regex::new(r"^U[0-9]{8}$").unwrap());
m.insert(String::from("BE"), Regex::new(r"^0[0-9]{9}$").unwrap());
m.insert(String::from("BG"), Regex::new(r"^[0-9]{9,10}$").unwrap());
m.insert(String::from("CY"), Regex::new(r"^[0-9]{8}[A-Za-z]$").unwrap());
m.insert(String::from("CZ"), Regex::new(r"^[0-9]{8,10}$").unwrap());
m.insert(String::from("DE"), Regex::new(r"^[0-9]{9}$").unwrap());
m.insert(String::from("DK"), Regex::new(r"^[0-9]{2} ?[0-9]{2} ?[0-9]{2} ?[0-9]{2}$").unwrap());
m.insert(String::from("EE"), Regex::new(r"^[0-9]{9}$").unwrap());
m.insert(String::from("EL"), Regex::new(r"^[0-9]{9}$").unwrap());
m.insert(String::from("ES"), Regex::new(r"^[A-Za-z0-9][0-9]{7}[A-Za-z0-9]$").unwrap());
m.insert(String::from("FI"), Regex::new(r"^[0-9]{8}$").unwrap());
m.insert(String::from("FR"), Regex::new(r"^[A-Za-z0-9]{2} ?[0-9]{9}$").unwrap());
m.insert(String::from("GB"), Regex::new(r"^[0-9]{3} ?[0-9]{4} ?[0-9]{2}|[0-9]{3} ?[0-9]{4} ?[0-9]{2} ?[0-9]{3}|GD[0-9]{3}|HA[0-9]{3}$").unwrap());
m.insert(String::from("HR"), Regex::new(r"^[0-9]{11}$").unwrap());
m.insert(String::from("IT"), Regex::new(r"^[0-9]{11}$").unwrap());
m.insert(String::from("HU"), Regex::new(r"^[0-9]{8}$").unwrap());
m.insert(String::from("IE"), Regex::new(r"^[0-9][A-Za-z0-9\+\*][0-9]{5}[A-Za-z]{1,2}$").unwrap());
m.insert(String::from("LT"), Regex::new(r"^[0-9]{9}|[0-9]{12}$").unwrap());
m.insert(String::from("LU"), Regex::new(r"^[0-9]{8}$").unwrap());
m.insert(String::from("LV"), Regex::new(r"^[0-9]{11}$").unwrap());
m.insert(String::from("MT"), Regex::new(r"^[0-9]{8}$").unwrap());
m.insert(String::from("NL"), Regex::new(r"^[0-9]{9}B[0-9]{2}$").unwrap());
m.insert(String::from("PL"), Regex::new(r"^[0-9]{10}$").unwrap());
m.insert(String::from("PT"), Regex::new(r"^[0-9]{9}$").unwrap());
m.insert(String::from("RO"), Regex::new(r"^[0-9]{2,10}$").unwrap());
m.insert(String::from("SE"), Regex::new(r"^[0-9]{12}$").unwrap());
m.insert(String::from("SI"), Regex::new(r"^[0-9]{8}$").unwrap());
m.insert(String::from("SK"), Regex::new(r"^[0-9]{10}$").unwrap());
m
};
}
pub fn check(vat_id: &str) -> bool {
match parse(vat_id) {
Ok(_vi) => true,
Err(_e) => false
}
}
pub fn check_by_state(local_vat_id: &str, state: &str) -> bool {
check( &format!("{}{}", state, local_vat_id) )
}
pub fn parse(vat_id : &str) -> Result<VATID, Error> {
let (state_iso, local_vat_id) = match RX_GENERAL.captures(&vat_id.to_uppercase()) {
Some(cp) => (
String::from( cp.get(1).unwrap().as_str() ),
String::from( cp.get(2).unwrap().as_str() )
),
None => bail!("invalid-base-structure")
};
let rx_local = match RXS_COUNTRIES.get(&state_iso) {
Some(rx) => rx,
None => bail!("invalid-state")
};
if !rx_local.is_match(&local_vat_id) {
bail!("invalid-local_vat_id");
}
Ok(VATID {
state_iso : state_iso,
local_vat_id: local_vat_id
})
}