don't crash on weird directories

This commit is contained in:
Mylloon 2024-12-06 09:57:29 +01:00
parent 5bc8eba61d
commit 2b811359f4
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

View file

@ -30,22 +30,21 @@ impl PartialOrd for FileNode {
}
}
#[once(time = 120)]
#[once(time = 600)]
pub fn get_filetree(
initial_dir: &str,
exclusion_list: &[String],
exclusion_patterns: &[Regex],
) -> FileNode {
gen_filetree(initial_dir, exclusion_list, exclusion_patterns)
gen_filetree(initial_dir, exclusion_list, exclusion_patterns).unwrap()
}
fn gen_filetree(
dir_path: &str,
exclusion_list: &[String],
exclusion_patterns: &[Regex],
) -> FileNode {
let mut children: Vec<FileNode> = std::fs::read_dir(dir_path)
.unwrap()
) -> Result<FileNode, std::io::Error> {
let mut children: Vec<FileNode> = std::fs::read_dir(dir_path)?
.filter_map(Result::ok)
.filter_map(|entry| {
let entry_path = entry.path();
@ -69,10 +68,14 @@ fn gen_filetree(
exclusion_list,
exclusion_patterns,
);
if children_of_children.is_dir && children_of_children.children.is_empty() {
None
if let Ok(coc) = children_of_children {
if coc.is_dir && coc.children.is_empty() {
None
} else {
Some(coc)
}
} else {
Some(children_of_children)
None
}
}
})
@ -80,7 +83,7 @@ fn gen_filetree(
children.sort();
FileNode {
Ok(FileNode {
name: Path::new(dir_path)
.file_name()
.unwrap()
@ -88,7 +91,7 @@ fn gen_filetree(
.to_string(),
is_dir: true,
children,
}
})
}
pub fn excluded(element: &str, exclusion_list: &[String], exclusion_patterns: &[Regex]) -> bool {