pub struct GlobPath { /* private fields */ }Expand description
A path-aware glob pattern with globstar support.
§Examples
use kaish_glob::GlobPath;
use std::path::Path;
let pattern = GlobPath::new("**/*.rs").unwrap();
assert!(pattern.matches(Path::new("main.rs")));
assert!(pattern.matches(Path::new("src/main.rs")));
assert!(pattern.matches(Path::new("src/lib/utils.rs")));
assert!(!pattern.matches(Path::new("README.md")));Implementations§
Source§impl GlobPath
impl GlobPath
Sourcepub fn new(pattern: &str) -> Result<GlobPath, PatternError>
pub fn new(pattern: &str) -> Result<GlobPath, PatternError>
Parse a glob pattern into a GlobPath.
Patterns starting with / are anchored to the root.
** matches zero or more directory components.
Sourcepub fn static_prefix(&self) -> Option<PathBuf>
pub fn static_prefix(&self) -> Option<PathBuf>
Get the static prefix of the pattern (directories before any wildcard).
This is useful for optimization: we can start the walk from this prefix instead of the root.
§Examples
use kaish_glob::GlobPath;
use std::path::PathBuf;
let pattern = GlobPath::new("src/lib/**/*.rs").unwrap();
assert_eq!(pattern.static_prefix(), Some(PathBuf::from("src/lib")));
let pattern = GlobPath::new("**/*.rs").unwrap();
assert_eq!(pattern.static_prefix(), None);Sourcepub fn split_static_dir(&self) -> (PathBuf, GlobPath)
pub fn split_static_dir(&self) -> (PathBuf, GlobPath)
Split the pattern into its deepest static directory prefix and the remaining pattern to match beneath it.
Used to start a walk from the literal leading directories instead of
the filesystem root: walking from / is O(filesystem) and skips
hidden intermediate directories, so /tmp/.tmpXXXX/*.txt would match
nothing. At least one segment is always kept in the remaining pattern,
so an all-literal pattern (/a/b/c.txt) walks /a/b and matches
c.txt rather than trying to descend into the file itself. The
returned pattern is unanchored (the anchor is consumed by the caller’s
walk root).
§Examples
use kaish_glob::GlobPath;
use std::path::{Path, PathBuf};
let (dir, rest) = GlobPath::new("/a/b/*.txt").unwrap().split_static_dir();
assert_eq!(dir, PathBuf::from("a/b"));
assert!(rest.matches(Path::new("c.txt")));
// All-literal: the final component stays in the match pattern.
let (dir, rest) = GlobPath::new("/a/b/c.txt").unwrap().split_static_dir();
assert_eq!(dir, PathBuf::from("a/b"));
assert!(rest.matches(Path::new("c.txt")));
// No static prefix (leading wildcard / globstar): empty dir, full pattern.
let (dir, _rest) = GlobPath::new("**/*.rs").unwrap().split_static_dir();
assert_eq!(dir, PathBuf::new());Sourcepub fn is_dir_only(&self) -> bool
pub fn is_dir_only(&self) -> bool
Check if the pattern only matches directories.
Sourcepub fn is_anchored(&self) -> bool
pub fn is_anchored(&self) -> bool
Check if the pattern is anchored (starts with /).
Sourcepub fn has_globstar(&self) -> bool
pub fn has_globstar(&self) -> bool
Check if the pattern contains a globstar (**).
Patterns with globstar require recursive directory traversal. Patterns without globstar only match at a fixed depth.
Sourcepub fn fixed_depth(&self) -> Option<usize>
pub fn fixed_depth(&self) -> Option<usize>
Get the depth of the pattern (number of path components).
Returns None if the pattern contains globstar (variable depth).