Skip to content

Commit 4ad3059

Browse files
committed
Refactor
1 parent 3442c82 commit 4ad3059

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

src/main.rs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,39 @@ fn get_memory() -> u64 {
1313
let current = s.process(sysinfo::get_current_pid().unwrap()).unwrap();
1414
current.memory()
1515
}
16+
fn collect_files(dir: String) -> Vec<path::PathBuf> {
17+
glob(&format!("{}/**/*.ts*", dir))
18+
.unwrap()
19+
.filter_map(|file| file.ok())
20+
.collect()
21+
}
22+
fn parse_file(
23+
file: &path::PathBuf,
24+
tx: &crossbeam::channel::Sender<String>,
25+
) -> Option<Box<tsx::Program>> {
26+
if file.is_dir() {
27+
return None;
28+
}
29+
return match parse_file_typescript(file.to_str().unwrap()) {
30+
Ok(program) => Some(program),
31+
Err(e) => {
32+
tx.send(e.to_string()).unwrap();
33+
None
34+
}
35+
};
36+
}
1637
fn parse_files(dir: String) -> (Vec<Box<tsx::Program>>, Vec<String>) {
1738
rayon::ThreadPoolBuilder::new()
1839
.stack_size(1024 * 1024 * 1024 * 10)
1940
.build_global()
2041
.unwrap();
2142
let (tx, rx) = crossbeam::channel::unbounded();
2243
let mut errors = Vec::new();
23-
let files_to_parse: Vec<Result<path::PathBuf, glob::GlobError>> =
24-
glob(&format!("{}/**/*.ts*", dir)).unwrap().collect();
44+
let files_to_parse = collect_files(dir);
2545
log::info!("Parsing {} files", files_to_parse.len());
2646
let files: Vec<Box<tsx::Program>> = files_to_parse
2747
.par_iter()
28-
.filter_map(|file| {
29-
if let Ok(file) = file {
30-
if file.is_dir() {
31-
return None;
32-
}
33-
return match parse_file_typescript(file.to_str().unwrap()) {
34-
Ok(program) => Some(program),
35-
Err(e) => {
36-
tx.send(e.to_string()).unwrap();
37-
None
38-
}
39-
};
40-
}
41-
None
42-
})
48+
.filter_map(|file| parse_file(file, &tx))
4349
.collect();
4450
drop(tx);
4551
for e in rx.iter() {

0 commit comments

Comments
 (0)