-
Notifications
You must be signed in to change notification settings - Fork 4
/
build.rs
142 lines (130 loc) · 5 KB
/
build.rs
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
use std::{env, path::PathBuf};
use quote::{format_ident, quote};
use regex::Regex;
#[derive(Debug, serde::Deserialize)]
struct Record {
country_code: String,
//country_name: String,
//domestic_example: String,
//bban_example: String,
//bban_format_swift: String,
//bban_format_regex: String,
//bban_length: usize,
//iban_example: String,
iban_format_swift: String,
//iban_format_regex: String,
iban_length: usize,
bban_bankid_start_offset: Option<usize>,
bban_bankid_stop_offset: Option<usize>,
bban_branchid_start_offset: Option<usize>,
bban_branchid_stop_offset: Option<usize>,
//registry_edition: String,
//country_sepa: String,
//swift_official: String,
bban_checksum_start_offset: Option<usize>,
bban_checksum_stop_offset: Option<usize>,
//country_code_iana: String,
//country_code_iso3166_1_alpha2: String,
//parent_registrar: String,
//currency_iso4217: String,
//central_bank_url: String,
//central_bank_name: String,
//membership: String,
}
fn main() {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=registry.txt");
let mut reader = csv::ReaderBuilder::new()
.delimiter(b'|')
.has_headers(true)
.from_path("./registry.txt")
.expect("failed to create csv reader for registry.txt");
// TODO: `\d+(a|n|c|i)` is also valid, and specifies a maximum length, rather than a fixed length.
let pattern = Regex::new(r"(\d+)!(a|n|c|i)").expect("regex should be valid");
let countries = reader
.deserialize()
.map(|record| record.expect("valid record"))
.map(
|Record {
country_code,
iban_format_swift,
iban_length,
bban_bankid_start_offset,
bban_bankid_stop_offset,
bban_branchid_start_offset,
bban_branchid_stop_offset,
bban_checksum_start_offset,
bban_checksum_stop_offset,
}| {
let captures = pattern
.captures_iter(&iban_format_swift[2..])
.map(|captures| {
(
captures[1].parse::<usize>().unwrap(),
format_ident!(
"{}",
captures[2]
.parse::<char>()
.unwrap()
.to_ascii_uppercase()
.to_string()
),
)
})
.map(|(len, char)| quote! { (#len, CharacterType::#char) });
let captures = iban_format_swift[..2]
.as_bytes()
.iter()
.map(|byte| (1usize, byte.to_ascii_uppercase()))
.map(|(len, char)| quote! { (#len, CharacterType::S(#char)) })
.chain(captures);
let bankid_offset = if let (Some(start), Some(end)) =
(bban_bankid_start_offset, bban_bankid_stop_offset)
{
quote! { Some((#start, #end + 1)) }
} else {
quote! { None }
};
let branch_offset = if let (Some(start), Some(end)) =
(bban_branchid_start_offset, bban_branchid_stop_offset)
{
quote! { Some((#start, #end + 1)) }
} else {
quote! { None }
};
let checksum_offset = if let (Some(start), Some(end)) =
(bban_checksum_start_offset, bban_checksum_stop_offset)
{
quote! { Some((#start, #end + 1)) }
} else {
quote! { None }
};
(
country_code,
quote! {
(
#iban_length,
&[#(#captures),*],
#bankid_offset,
#branch_offset,
#checksum_offset,
)
},
)
},
)
.collect::<Vec<_>>();
let mut map = phf_codegen::Map::new();
for (key, value) in &countries {
map.entry(key.as_str(), value.to_string().as_str());
}
let countries = map.build();
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
std::fs::write(
out_path.join("countries.rs"),
format!(
"#[allow(clippy::type_complexity, clippy::unreadable_literal, clippy::identity_op)]\nstatic COUNTRIES: ::phf::Map<&'static str, (usize, &'static [(usize, CharacterType)], Option<(usize, usize)>, Option<(usize, usize)>, Option<(usize, usize)>)> = {countries};\n",
),
)
.expect("failed to write countries file");
}