forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstructure_hashmap.rs
54 lines (48 loc) · 1.84 KB
/
structure_hashmap.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
use crate::helpers::{is_source_file, LexicalAbsolute};
use ahash::AHashMap;
use std::path::PathBuf;
use std::{error, fs};
pub fn read_folders(
filter: &Option<regex::Regex>,
path: &str,
recurse: bool,
) -> Result<AHashMap<String, fs::Metadata>, Box<dyn error::Error>> {
let mut map: AHashMap<String, fs::Metadata> = AHashMap::new();
let path_buf = PathBuf::from(path);
let abs_path = path_buf
.to_lexical_absolute()
.map(|x| x.to_str().map(|y| y.to_string()).unwrap_or("".to_string()))
.and_then(|x| fs::metadata(x.to_owned()).map(|m| (x.to_owned(), m)));
for entry in fs::read_dir(path.replace("//", "/"))? {
let path_buf = entry.map(|entry| entry.path())?;
let metadata = fs::metadata(&path_buf)?;
let name = path_buf
.file_name()
.and_then(|x| x.to_str())
.unwrap_or("Unknown")
.to_string();
let path_ext = path_buf.extension().and_then(|x| x.to_str());
if metadata.file_type().is_dir() && recurse {
match read_folders(&filter, &(path.to_owned() + "/" + &name + "/"), recurse) {
Ok(s) => map.extend(s),
Err(e) => println!("Error reading directory: {}", e),
}
}
match path_ext {
Some(extension) if is_source_file(extension) => match abs_path {
Ok((ref path, _))
if filter
.as_ref()
.map(|re| !re.is_match(&name))
.unwrap_or(true) =>
{
map.insert(path.to_owned() + "/" + &name, metadata);
}
Ok(_) => println!("Filtered: {:?}", name),
Err(ref e) => println!("Error reading directory: {}", e),
},
_ => (),
}
}
Ok(map)
}