pub struct Pattern { /* private fields */ }Expand description
A pattern that can be matched against filesystem paths
§Syntax
The syntax for patterns is similar to Linux’ glob, with a few differences.
- Normal characters behave as expected
?matches any character*matches any suite of characters, or no character at all[abc]matches any ofa,borc[!abc]matches any character excepta,bandc[\[]matches[. The list of escapable characters is[,],{,},*,?,\,/,|and ‘:’[abc\[]matches any ofa,b,cor[
[[:alpha:]]will match any alphabetic character. The list of character classes are::alpha:for any alphabetic character:digit:for any digit:alphanumeric:for any alphabetic character or digit:uppercase:for any uppercase character:lowercase:for any lowercase character:whitespace:for any whitespace character
[![:alpha:]]will match any non-alphabetic character{a|bc}will match any ofaorbc- This can be combined with other matchers, e.g.
{[[:alpha:]][![:digit]]|[[:digit:]]*}will match any alphabetic character followed by a non-digit character, OR a digit followed by anything
- This can be combined with other matchers, e.g.
Matches are performed against path components, e.g. in /path/to/item components are path, to and item.
Matchers cannot match path separators.
In addition, note that ** will match any possible combination of directories. For instance, /**/*.txt will match any of /file.txt, /dir/file.txt, /dir/dir2/file.txt, and so on.
§Platform-specific support
/and\are treated as path separators independently of the platform- Absolute patterns can only be matched against absolute paths. e.g.
/dirwill not matchdir. Note that using acrate::Walkerwill not cause this problem as a base directory is used. - Absolute patterns can be matched against named drives in Windows, e.g.
\dirwill match againstC:\dir(but not the opposite) - Supported syntaxes for Windows drives are
C:\and\\?\C:\ - Other verbatim paths such as
\\?\server\shareor\\.\deviceare unsupported - Paths starting with
\\?\C:\are normalized like any other path
Implementations§
Source§impl Pattern
impl Pattern
Sourcepub fn new(input: &str) -> Result<Self, ParsingError>
pub fn new(input: &str) -> Result<Self, ParsingError>
Parse a pattern with the default options
Sourcepub fn new_with_opts(
input: &str,
opts: PatternOpts,
) -> Result<Self, ParsingError>
pub fn new_with_opts( input: &str, opts: PatternOpts, ) -> Result<Self, ParsingError>
Parse a pattern
Examples found in repository?
5fn main() {
6 let pattern = Pattern::new_with_opts(
7 "**/*.*",
8 PatternOpts {
9 case_insensitive: false,
10 },
11 )
12 .unwrap();
13
14 let walker = Walker::new(pattern, Path::new("/"));
15
16 for path in walker {
17 match path {
18 Ok(path) => println!("OK: {}", path.display()),
19 Err(err) => println!("ERR: {err}"),
20 }
21 }
22}Sourcepub fn is_absolute(&self) -> bool
pub fn is_absolute(&self) -> bool
Check if the pattern is absolute (only matches absolute paths)
Sourcepub fn prefix(&self) -> Option<PathPrefix>
pub fn prefix(&self) -> Option<PathPrefix>
Get the path prefix
Sourcepub fn is_match(&self, path: &Path) -> bool
pub fn is_match(&self, path: &Path) -> bool
Match the pattern against a path
Note that the path should be normalized. For instance, ‘..’ components in the pattern will be matched against literal ‘..’ in the path.
pub fn match_against(&self, path: &Path) -> PatternMatchResult
Sourcepub fn common_root_dir(&self) -> &Path
pub fn common_root_dir(&self) -> &Path
Get the common root directory for all possible matches of this pattern
Sourcepub fn has_wildcard(&self) -> bool
pub fn has_wildcard(&self) -> bool
Check if the component contains a wildcard
Can be useful for e.g. determining if a matching directory should be traversed or not, as the presence or absence of a wildcard indicates whether descendants may match
Example:
/a/bmatches/a/bbut cannot match any descendant/a/**/bmatches/a/band may match some descendants/a/b/**matches/a/band may match some descendants