From 63b633e335a258c8e41de17d824142ae47ae700b Mon Sep 17 00:00:00 2001 From: jianse Date: Fri, 13 Sep 2024 09:52:52 +0000 Subject: [PATCH] Let the get_files function skip excluded directories --- utils/src/lib.rs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/utils/src/lib.rs b/utils/src/lib.rs index 85ee206..64c6113 100644 --- a/utils/src/lib.rs +++ b/utils/src/lib.rs @@ -14,10 +14,21 @@ pub struct FileEntry { #[cfg_attr(all(debug_assertions, not(feature = "debug-embed")), allow(unused))] pub fn get_files(folder_path: String, matcher: PathMatcher) -> impl Iterator { + let temp_folder_path = folder_path.clone(); + walkdir::WalkDir::new(&folder_path) .follow_links(true) .sort_by_file_name() .into_iter() + .filter_entry(move |e|{ + let rel_path = path_to_str(e.path().strip_prefix(&temp_folder_path).unwrap()); + if e.file_type().is_dir() { + // dir must be explicitly excluded + !matcher.is_path_excluded(&rel_path) + } else{ + matcher.is_path_included(&rel_path) + } + }) .filter_map(|e| e.ok()) .filter(|e| e.file_type().is_file()) .filter_map(move |e| { @@ -29,11 +40,7 @@ pub fn get_files(folder_path: String, matcher: PathMatcher) -> impl Iterator bool { !self.exclude_matcher.is_match(path) && (self.include_matcher.is_empty() || self.include_matcher.is_match(path)) } + pub fn is_path_excluded(&self, path: &str) -> bool { + self.exclude_matcher.is_match(path) + } } #[cfg(not(feature = "include-exclude"))] @@ -182,4 +192,7 @@ impl PathMatcher { pub fn is_path_included(&self, _path: &str) -> bool { true } + pub fn is_path_excluded(&self, _path: &str) -> bool { + false + } }