Skip to content

Commit

Permalink
Try using wax
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Soltis authored and Greg Soltis committed May 12, 2023
1 parent 872d856 commit df8a277
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 12 deletions.
80 changes: 76 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/globwalk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
glob-match = { version = "0.2.1", path = "../glob-match", features = ["unic-segment"] }
itertools.workspace = true
path-slash = "0.2.1"
thiserror.workspace = true
turbopath = { path = "../turbopath" }
walkdir = "2.3.3"
wax = "0.5.0"

[dev-dependencies]
tempdir = "0.3.7"
Expand Down
27 changes: 20 additions & 7 deletions crates/globwalk/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::{io::ErrorKind, path::Path};

use glob_match::glob_match;
use itertools::{
FoldWhile::{Continue, Done},
Itertools,
};
use path_slash::PathExt;
use turbopath::AbsoluteSystemPathBuf;
use wax::{Glob, Pattern};

pub enum WalkType {
Files,
Expand Down Expand Up @@ -39,6 +39,18 @@ pub enum WalkError {
BadPattern(String),
}

fn glob_match(pattern: &str, path: &str) -> Option<bool> {
let glob = match Glob::new(pattern) {
Ok(glob) => glob,
Err(e) => {
println!("{}", e);
return None;
}
};
let result = glob.is_match(path);
Some(result)
}

/// Performs a glob walk, yielding paths that
/// _are_ included in the include list (if it is nonempty)
/// and _not_ included in the exclude list.
Expand Down Expand Up @@ -431,7 +443,8 @@ mod test {
#[test_case("ab{c,d}", None, 1, 1 ; "pattern with curly braces match")]
#[test_case("ab{c,d,*}", None, 5, 5 ; "pattern with curly braces and wildcard match")]
#[test_case("ab{c,d}[", Some(WalkError::BadPattern("ab{c,d}[".into())), 0, 0)]
#[test_case("a{,bc}", None, 2, 2 ; "a followed by comma or b or c")]
// #[test_case("a{,bc}", None, 2, 2 ; "a followed by comma or b or c")]
#[test_case("a{,bc}", Some(WalkError::BadPattern("a{,bc}".into())), 0, 0 ; "a followed by comma or b or c")]
#[test_case("a/{b/c,c/b}", None, 2, 2)]
#[test_case("{a/{b,c},abc}", None, 3, 3)]
#[test_case("{a/ab*}", None, 1, 1)]
Expand All @@ -447,7 +460,7 @@ mod test {
// however in symlink mode, walkdir yields broken symlinks as errors
#[test_case("broken-symlink", None, 1, 1 ; "broken symlinks should be yielded")]
// globs that match across a symlink should not follow the symlink
#[test_case("working-symlink/c/*", None, 1, 1 ; "working symlink should not be followed")]
#[test_case("working-symlink/c/*", None, 0, 0 ; "working symlink should not be followed")]
#[test_case("working-sym*/*", None, 0, 0 ; "working symlink should not be followed 2")]
#[test_case("b/**/f", None, 0, 0)]
fn glob_walk(
Expand Down Expand Up @@ -477,10 +490,10 @@ mod test {
#[test_case("[\\-x]", None, 2 ; "escaped dash and character match")]
#[test_case("[\\-x]", None, 2 ; "escaped dash and character match 2")]
#[test_case("[\\-x]", None, 2 ; "escaped dash and character mismatch")]
#[test_case("[-]", None, 1 ; "bare dash in character class match")]
#[test_case("[x-]", None, 2 ; "trailing dash in character class match 2")]
#[test_case("[-x]", None, 2 ; "leading dash in character class match 2")]
#[test_case("[a-b-d]", None, 3 ; "dash within character class range match 3")]
#[test_case("[-]", Some(WalkError::BadPattern("[-]".into())), 0 ; "bare dash in character class match")]
#[test_case("[x-]", Some(WalkError::BadPattern("[x-]".into())), 0 ; "trailing dash in character class match 2")]
#[test_case("[-x]", Some(WalkError::BadPattern("[-x]".into())), 0 ; "leading dash in character class match 2")]
#[test_case("[a-b-d]", Some(WalkError::BadPattern("[a-b-d]".into())), 0 ; "dash within character class range match 3")]
#[test_case("\\", Some(WalkError::BadPattern("\\".into())), 0 ; "single backslash error")]
#[test_case("a/\\**", None, 0 ; "a followed by escaped double star and subdirectories mismatch")]
#[test_case("a/\\[*\\]", None, 0 ; "a followed by escaped character class and pattern mismatch")]
Expand Down

0 comments on commit df8a277

Please sign in to comment.