Skip to content

Commit

Permalink
feat: update patterns type
Browse files Browse the repository at this point in the history
  • Loading branch information
CCherry07 committed Apr 1, 2024
1 parent c94c32f commit b27e7a9
Showing 1 changed file with 22 additions and 28 deletions.
50 changes: 22 additions & 28 deletions crates/toolkit/src/pluginutils/create_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<&'a str>>,
exclude_patterns: Option<Vec<&'a str>>,
) -> Result<impl Fn(&str) -> bool + 'a, Box<dyn Error>> {
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<GlobSet, Box<dyn Error>> {
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<GlobSet, Box<dyn Error>> {
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::*;
Expand All @@ -46,15 +40,15 @@ mod tests {

#[test]
fn excludes_items_not_included_if_include_patterns_provided() -> Result<(), Box<dyn Error>> {
let filter = create_filter(Some(&["*.y"]), None)?;
let filter = create_filter(Some(vec!["*.y"]), None)?;
assert!(!filter("x"));
assert!(filter("a.y"));
Ok(())
}

#[test]
fn patterns_with_wildcards() -> Result<(), Box<dyn Error>> {
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"));
Expand All @@ -64,15 +58,15 @@ mod tests {

#[test]
fn excludes_items_when_exclude_pattern_provided() -> Result<(), Box<dyn Error>> {
let filter = create_filter(None, Some(&["*.tmp"]))?;
let filter = create_filter(None, Some(vec!["*.tmp"]))?;
assert!(filter("a.out"));
assert!(!filter("b.tmp"));
Ok(())
}

#[test]
fn properly_handles_inclusion_and_exclusion_patterns() -> Result<(), Box<dyn Error>> {
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"));
Expand All @@ -81,7 +75,7 @@ mod tests {

#[test]
fn handles_relative_paths_correctly() -> Result<(), Box<dyn Error>> {
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()
Expand All @@ -92,7 +86,7 @@ mod tests {

#[test]
fn handles_relative_paths() -> Result<(), Box<dyn Error>> {
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")));
Expand Down

0 comments on commit b27e7a9

Please sign in to comment.