diff --git a/crates/toolkit/src/pluginutils/create_filter.rs b/crates/toolkit/src/pluginutils/create_filter.rs index 7b7eef9bc..19795e4e1 100644 --- a/crates/toolkit/src/pluginutils/create_filter.rs +++ b/crates/toolkit/src/pluginutils/create_filter.rs @@ -2,35 +2,29 @@ use globset::{GlobBuilder, GlobSet, GlobSetBuilder}; use std::error::Error; pub fn create_filter<'a>( - include_patterns: Option<&'a [&str]>, - exclude_patterns: Option<&'a [&str]>, + include_patterns: Option>, + exclude_patterns: Option>, ) -> Result bool + 'a, Box> { - let include_set = patterns_builder(include_patterns)?; - let exclude_set = patterns_builder(exclude_patterns)?; + let include_set = patterns_builder(include_patterns.as_deref())?; + let exclude_set = patterns_builder(exclude_patterns.as_deref())?; - Ok(move |path: &str| { - let match_include = match include_patterns { - Some(patterns) => patterns.is_empty() || include_set.is_match(path), - None => true, - }; - let match_exclude = match exclude_patterns { - Some(patterns) => !patterns.is_empty() && exclude_set.is_match(path), - None => false, - }; - match_include && !match_exclude + Ok(move |path: &str| { + let match_include = include_set.is_match(path) || include_set.is_empty(); + let match_exclude = exclude_set.is_match(path) && !exclude_set.is_empty(); + + match_include && !match_exclude }) } -fn patterns_builder<'a>(patterns: Option<&'a [&str]>) -> Result> { - let mut builder = GlobSetBuilder::new(); - if let Some(patterns) = patterns { - for pattern in patterns { - builder.add(GlobBuilder::new(pattern.as_ref()).build()?); +fn patterns_builder(patterns: Option<&[&str]>) -> Result> { + let mut builder = GlobSetBuilder::new(); + if let Some(patterns) = patterns { + for &pattern in patterns { + builder.add(GlobBuilder::new(pattern).build()?); + } } - } - Ok(builder.build()?) + builder.build().map_err(Into::into) } - #[cfg(test)] mod tests { use super::*; @@ -46,7 +40,7 @@ mod tests { #[test] fn excludes_items_not_included_if_include_patterns_provided() -> Result<(), Box> { - let filter = create_filter(Some(&["*.y"]), None)?; + let filter = create_filter(Some(vec!["*.y"]), None)?; assert!(!filter("x")); assert!(filter("a.y")); Ok(()) @@ -54,7 +48,7 @@ mod tests { #[test] fn patterns_with_wildcards() -> Result<(), Box> { - let filter = create_filter(Some(&["*.y", "a.?"]), None)?; + let filter = create_filter(Some(vec!["*.y", "a.?"]), None)?; assert!(filter("c.y")); assert!(!filter("c.z")); assert!(filter("a.x")); @@ -64,7 +58,7 @@ mod tests { #[test] fn excludes_items_when_exclude_pattern_provided() -> Result<(), Box> { - let filter = create_filter(None, Some(&["*.tmp"]))?; + let filter = create_filter(None, Some(vec!["*.tmp"]))?; assert!(filter("a.out")); assert!(!filter("b.tmp")); Ok(()) @@ -72,7 +66,7 @@ mod tests { #[test] fn properly_handles_inclusion_and_exclusion_patterns() -> Result<(), Box> { - let filter = create_filter(Some(&["*.js"]), Some(&["*.min.js"]))?; + let filter = create_filter(Some(vec!["*.js"]), Some(vec!["*.min.js"]))?; assert!(filter("app.js")); assert!(!filter("app.min.js")); assert!(!filter("app.ts")); @@ -81,7 +75,7 @@ mod tests { #[test] fn handles_relative_paths_correctly() -> Result<(), Box> { - let filter = create_filter(Some(&["src/*.js"]), Some(&["src/*.test.js"]))?; + let filter = create_filter(Some(vec!["src/*.js"]), Some(vec!["src/*.test.js"]))?; assert!(filter(&PathBuf::from("src/main.js").to_string_lossy())); assert!(!filter( &PathBuf::from("src/main.test.js").to_string_lossy() @@ -92,7 +86,7 @@ mod tests { #[test] fn handles_relative_paths() -> Result<(), Box> { - let filter = create_filter(Some(&["./index.js"]), Some(&["'./foo/../a.js'"]))?; + let filter = create_filter(Some(vec!["./index.js"]), Some(vec!["'./foo/../a.js'"]))?; assert!(!filter(&normalize_path("index.js"))); assert!(!filter(&normalize_path("a.js"))); assert!(!filter(&normalize_path("foo/a.js")));