-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbuild.rs
210 lines (186 loc) · 6.09 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use std::env;
use std::fs::File;
use std::io::{Read, Write};
use std::path::Path;
use codegen::{Block, Function, Scope};
use roxmltree::{Document, Node};
const ALLOW_LINTS: &str = r###"
#[allow(clippy::unreadable_literal)]
#[allow(clippy::match_same_arms)]
#[allow(clippy::too_many_lines)]
"###;
/// EAN.UCC prefix or registration group.
struct Group {
agency: String,
prefix: [u8; 3],
registration_group_element: Vec<u8>,
rules: Vec<Rule>,
}
/// Range length rule.
struct Rule {
min: u32,
max: u32,
length: usize,
}
/// Parse registration group and registrant range length rules.
fn parse_rules(group: Node) -> Vec<Rule> {
group
.descendants()
.filter(|n| n.has_tag_name("Rule"))
.map(|r| {
let range: Vec<u32> = r
.descendants()
.find(|n| n.has_tag_name("Range"))
.unwrap()
.text()
.unwrap()
.split('-')
.map(|i| i.parse().unwrap())
.collect();
let length = r
.descendants()
.find(|n| n.has_tag_name("Length"))
.unwrap()
.text()
.unwrap()
.parse()
.unwrap();
assert!(length < 8, "Segment length can be at most 7.");
Rule {
min: range[0],
max: range[1],
length,
}
})
.collect()
}
/// Parse EAN.UCC prefix and registration group element.
fn parse_group(group: Node) -> Group {
let prefix_str = group
.descendants()
.find(|n| n.has_tag_name("Prefix"))
.unwrap()
.text()
.unwrap();
let mut prefix = [0; 3];
let mut registration_group_element = Vec::new();
for (i, c) in prefix_str.chars().enumerate() {
if i < 3 {
prefix[i] = c.to_digit(10).unwrap() as u8;
}
if i >= 4 {
registration_group_element.push(c.to_digit(10).unwrap() as u8)
}
}
let agency = group
.descendants()
.find(|n| n.has_tag_name("Agency"))
.unwrap()
.text()
.unwrap()
.to_string();
Group {
agency,
prefix,
registration_group_element,
rules: parse_rules(group),
}
}
/// Generate code for EAN.UCC or registration group lookup.
fn codegen_find_group(name: &str, groups: Vec<Group>, check_registration_group: bool) -> Function {
let mut fn_get_group = Function::new(name);
fn_get_group.arg("prefix", "u16");
if check_registration_group {
fn_get_group.arg("registration_group_element", "u32");
}
fn_get_group.arg("segment", "u32");
fn_get_group.ret("Result<Group<'static>, IsbnError>");
let mut match_prefix = if check_registration_group {
Block::new("match (prefix, registration_group_element)")
} else {
Block::new("match prefix")
};
for group in groups {
match_prefix.line(if check_registration_group {
format!(
"({:#X}, {:#X}) =>",
((group.prefix[0] as u16) << 8)
| ((group.prefix[1] as u16) << 4)
| (group.prefix[2] as u16),
{
let mut digits = 0u32;
for &digit in &group.registration_group_element {
digits = (digits << 4) | digit as u32;
}
digits
}
)
} else {
format!(
"{:#X} =>",
((group.prefix[0] as u16) << 8)
| ((group.prefix[1] as u16) << 4)
| (group.prefix[2] as u16)
)
});
let mut let_length_eq_match_segment = Block::new("let length = match segment");
for rule in &group.rules {
let_length_eq_match_segment.line(match rule.length {
0 => format!(
"{} ..= {} => Err(IsbnError::UndefinedRange),",
rule.min, rule.max
),
_ => format!("{} ..= {} => Ok({}),", rule.min, rule.max, rule.length),
});
}
let_length_eq_match_segment.line("_ => Err(IsbnError::InvalidGroup)");
let_length_eq_match_segment.after(";");
let mut ok_group = Block::new("Ok(Group");
ok_group.line(format!("name: \"{}\",", group.agency));
ok_group.line("segment_length: length?");
ok_group.after(")");
let mut segment_match_block = Block::new("");
segment_match_block.push_block(let_length_eq_match_segment);
segment_match_block.push_block(ok_group);
match_prefix.push_block(segment_match_block);
}
match_prefix.line("_ => Err(IsbnError::InvalidGroup)");
fn_get_group.push_block(match_prefix);
fn_get_group
}
fn main() {
let mut f = File::open("./isbn-ranges/RangeMessage.xml").unwrap();
let mut text = String::new();
f.read_to_string(&mut text).unwrap();
let mut options = roxmltree::ParsingOptions::default();
options.allow_dtd = true;
let range_message = Document::parse_with_options(&text, options).unwrap();
let ean_ucc_groups = range_message
.descendants()
.filter(|d| d.tag_name().name() == "EAN.UCC")
.map(parse_group)
.collect();
let registration_groups = range_message
.descendants()
.filter(|d| d.tag_name().name() == "Group")
.map(parse_group)
.collect();
let mut scope = Scope::new();
let impl_isbn = scope.new_impl("Isbn");
impl_isbn.push_fn(codegen_find_group(
"get_ean_ucc_group",
ean_ucc_groups,
false,
));
impl_isbn.push_fn(codegen_find_group(
"get_registration_group",
registration_groups,
true,
));
let out_dir = env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("generated.rs");
let mut f = File::create(&dest_path).unwrap();
f.write_all(ALLOW_LINTS.trim_start().as_bytes()).unwrap();
writeln!(f).unwrap();
f.write_all(scope.to_string().as_bytes()).unwrap();
}