use filter_map instead of a for loop
This commit is contained in:
parent
fd26454a72
commit
1431a3bf35
1 changed files with 19 additions and 27 deletions
|
@ -38,41 +38,33 @@ struct FileNode {
|
||||||
children: Vec<FileNode>,
|
children: Vec<FileNode>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Build the filetree
|
|
||||||
fn get_filetree(dir_path: &str, exclusion_list: Vec<String>) -> FileNode {
|
fn get_filetree(dir_path: &str, exclusion_list: Vec<String>) -> FileNode {
|
||||||
let entries = std::fs::read_dir(dir_path).unwrap();
|
let children = std::fs::read_dir(dir_path).unwrap()
|
||||||
|
.filter_map(Result::ok)
|
||||||
|
.filter_map(|entry| {
|
||||||
|
let entry_path = entry.path();
|
||||||
|
let entry_name = entry_path.file_name()?.to_str()?;
|
||||||
|
|
||||||
let mut children = Vec::new();
|
// Exclude element with the exclusion_list
|
||||||
for entry in entries.filter_map(Result::ok) {
|
if exclusion_list.iter().any(|pattern| {
|
||||||
let entry_path = entry.path();
|
Regex::new(pattern)
|
||||||
let entry_name = entry_path.file_name().and_then(|n| n.to_str()).unwrap();
|
.map(|re| re.is_match(entry_name))
|
||||||
|
.unwrap_or(false)
|
||||||
|
}) {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
// We should support regex?
|
|
||||||
if !exclusion_list
|
|
||||||
.iter()
|
|
||||||
.any(|pattern| match Regex::new(pattern) {
|
|
||||||
Ok(re) => re.is_match(entry_name),
|
|
||||||
Err(e) => {
|
|
||||||
eprintln!("{e}");
|
|
||||||
false
|
|
||||||
}
|
|
||||||
})
|
|
||||||
{
|
|
||||||
let filename = entry_name.to_string();
|
|
||||||
if entry_path.is_file() {
|
if entry_path.is_file() {
|
||||||
children.push(FileNode {
|
Some(FileNode {
|
||||||
name: filename,
|
name: entry_name.to_string(),
|
||||||
is_dir: false,
|
is_dir: false,
|
||||||
children: vec![],
|
children: vec![],
|
||||||
});
|
})
|
||||||
} else {
|
} else {
|
||||||
children.push(get_filetree(
|
Some(get_filetree(entry_path.to_str().unwrap(), exclusion_list.clone()))
|
||||||
entry_path.to_str().unwrap(),
|
|
||||||
exclusion_list.to_owned(),
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
}
|
.collect();
|
||||||
|
|
||||||
FileNode {
|
FileNode {
|
||||||
name: Path::new(dir_path)
|
name: Path::new(dir_path)
|
||||||
|
|
Loading…
Reference in a new issue