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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
use std::path::PathBuf;
use std::fs::File;
use std::io::Write;
use itertools::Itertools;
use glob::glob;
pub struct UseBuilder {
mod_stmts: Vec<String>,
use_stmts: Vec<String>,
unused: bool,
}
impl UseBuilder {
pub fn new() -> Self {
Self{mod_stmts: Vec::new(), use_stmts: Vec::new(), unused: true}
}
pub fn mod_file(&mut self, file: PathBuf) -> &mut Self {
self.file(file, None, None);
self
}
pub fn use_file(&mut self, file: PathBuf, use_stmt: String) -> &mut Self {
self.file(file, Some(use_stmt), None);
self
}
pub fn mod_file_alias(&mut self, file: PathBuf, alias: &str) -> &mut Self {
self.file(file, None, Some(alias.to_string()));
self
}
pub fn use_file_alias(&mut self, file: PathBuf, use_stmt: String, alias: &str) -> &mut Self {
self.file(file, Some(use_stmt), Some(alias.to_string()));
self
}
pub fn use_crate(&mut self, use_stmt: String) -> &mut Self {
self.use_stmts.push(format!("use {};", use_stmt));
self
}
pub fn mod_glob(&mut self, globstring: &str) -> &mut Self {
for entry in glob(globstring).expect("Failed to read glob pattern") {
match entry {
Ok(path) => self.file(path, None, None),
Err(e) => panic!("Could not resolve glob pattern: {:?}", e),
}
}
self
}
pub fn use_glob(&mut self, globstring: &str, use_stmt: String) -> &mut Self {
for entry in glob(globstring).expect("Failed to read glob pattern") {
match entry {
Ok(path) => self.file(path, Some(use_stmt.clone()), None),
Err(e) => panic!("Could not resolve glob pattern: {:?}", e),
}
}
self
}
pub fn mod_glob_alias(&mut self, globstring: &str, alias: &str) -> &mut Self {
for entry in glob(globstring).expect("Failed to read glob pattern") {
match entry {
Ok(path) => self.file(path, None, Some(alias.to_string())),
Err(e) => panic!("Could not resolve glob pattern: {:?}", e),
}
}
self
}
pub fn use_glob_alias(&mut self, globstring: &str, use_stmt: String, alias: &str) -> &mut Self {
for entry in glob(globstring).expect("Failed to read glob pattern") {
match entry {
Ok(path) => self.file(path, Some(use_stmt.clone()), Some(alias.to_string())),
Err(e) => panic!("Could not resolve glob pattern: {:?}", e),
}
}
self
}
pub fn allow_unused(&mut self) -> &mut Self {
self.unused = true;
self
}
pub fn warn_unused(&mut self) -> &mut Self {
self.unused = false;
self
}
pub fn write_to_file_use(&mut self, path: PathBuf) -> &mut Self {
self.write_to_file(path,
if self.unused {
self.use_stmts.iter()
.map(|s| format!("#[allow(unused_imports)]\n{}", s)).join("\n")
} else {
self.use_stmts.join("\n")
}.as_bytes());
self
}
pub fn write_to_file_mod(&mut self, path: PathBuf) -> &mut Self {
self.write_to_file(path, self.mod_stmts.join("\n").as_bytes());
self
}
pub fn write_to_file_all(&mut self, path: PathBuf) -> &mut Self {
self.write_to_file(path,
self.mod_stmts.iter().chain(std::iter::once(
&if self.unused {
self.use_stmts.iter()
.map(|s| format!("#[allow(unused_imports)]\n{}", s)).join("\n")
} else {
self.use_stmts.join("\n")
}
)).join("\n").as_bytes());
self
}
fn file(&mut self, file: PathBuf, use_stmt: Option<String>, alias: Option<String>) {
let file = match file.canonicalize() {
Ok(file) => file,
Err(err) => panic!("Could not canonicalize file '{}': {}",
file.to_string_lossy(), err),
};
let mod_opt = {
if alias.is_some() {
alias
} else {
file.file_stem().map(|o| (*o.to_string_lossy()).to_string())
}
};
match (mod_opt, file.extension()) {
(Some(mod_name), Some(ext)) if ext == "rs" => {
self.mod_stmts.push(format!("#[path = \"{}\"]\nmod {};",
file.to_string_lossy(),
mod_name));
if let Some(use_stmt) = use_stmt {
self.use_stmts.push(format!("use {}::{};",
mod_name,
use_stmt));
}
},
(Some(_), _) => panic!("Invalid file '{}'. Probable cause: \
file is not a rust file.",
file.to_string_lossy()),
(None, _) => panic!("Invalid file '{}'. Probable cause: is not a regular file.",
file.to_string_lossy()),
}
}
fn write_to_file(&mut self, path: PathBuf, contents: &[u8]) {
match File::create(&path) {
Ok(mut file) => {
if let Err(err) = file.write_all(contents) {
panic!("Could not write to file '{}': {}", path.to_string_lossy(), err);
}
},
Err(err) => panic!("Could not open file '{}' for writing: {}",
path.to_string_lossy(), err),
}
}
}