optimizations
Some checks are pending
ci/woodpecker/push/publish Pipeline is pending approval

This commit is contained in:
Mylloon 2024-04-01 16:55:50 +02:00
parent 1431a3bf35
commit 9dafbae3d5
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

View file

@ -38,30 +38,37 @@ struct FileNode {
children: Vec<FileNode>, children: Vec<FileNode>,
} }
fn get_filetree(dir_path: &str, exclusion_list: Vec<String>) -> FileNode { fn compile_patterns(exclusion_list: &[String]) -> Vec<Regex> {
let children = std::fs::read_dir(dir_path).unwrap() exclusion_list
.iter()
.map(|pattern| Regex::new(pattern).unwrap())
.collect()
}
fn get_filetree(dir_path: &str, exclusion_patterns: Vec<Regex>) -> FileNode {
let children = std::fs::read_dir(dir_path)
.unwrap()
.filter_map(Result::ok) .filter_map(Result::ok)
.filter_map(|entry| { .filter_map(|entry| {
let entry_path = entry.path(); let entry_path = entry.path();
let entry_name = entry_path.file_name()?.to_str()?; let entry_name = entry_path.file_name()?.to_string_lossy().to_string();
// Exclude element with the exclusion_list // Exclude element with the exclusion_list
if exclusion_list.iter().any(|pattern| { if exclusion_patterns.iter().any(|re| re.is_match(&entry_name)) {
Regex::new(pattern)
.map(|re| re.is_match(entry_name))
.unwrap_or(false)
}) {
return None; return None;
} }
if entry_path.is_file() { if entry_path.is_file() {
Some(FileNode { Some(FileNode {
name: entry_name.to_string(), name: entry_name,
is_dir: false, is_dir: false,
children: vec![], children: vec![],
}) })
} else { } else {
Some(get_filetree(entry_path.to_str().unwrap(), exclusion_list.clone())) Some(get_filetree(
entry_path.to_str().unwrap(),
exclusion_patterns.to_owned(),
))
} }
}) })
.collect(); .collect();
@ -107,7 +114,8 @@ fn get_content(
fn build_page(info: web::Query<PathRequest>, config: Config) -> String { fn build_page(info: web::Query<PathRequest>, config: Config) -> String {
let cours_dir = "data/cours"; let cours_dir = "data/cours";
let exclusion_list = config.fc.exclude_courses.unwrap(); let exclusion_list = config.fc.exclude_courses.unwrap();
let filetree = get_filetree(cours_dir, exclusion_list.to_owned()); let exclusion_patterns = compile_patterns(&exclusion_list);
let filetree = get_filetree(cours_dir, exclusion_patterns);
config.tmpl.render( config.tmpl.render(
"cours.html", "cours.html",