Skip to main content

Pattern

Struct Pattern 

Source
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 of a, b or c
  • [!abc] matches any character except a, b and c
  • [\[] matches [. The list of escapable characters is [, ], {, }, *, ?, \, /, | and ‘:’
    • [abc\[] matches any of a, b, c or [
  • [[: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 of a or bc
    • 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

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. /dir will not match dir. Note that using a crate::Walker will not cause this problem as a base directory is used.
  • Absolute patterns can be matched against named drives in Windows, e.g. \dir will match against C:\dir (but not the opposite)
  • Supported syntaxes for Windows drives are C:\ and \\?\C:\
  • Other verbatim paths such as \\?\server\share or \\.\device are unsupported
  • Paths starting with \\?\C:\ are normalized like any other path

Implementations§

Source§

impl Pattern

Source

pub fn new(input: &str) -> Result<Self, ParsingError>

Parse a pattern with the default options

Source

pub fn new_with_opts( input: &str, opts: PatternOpts, ) -> Result<Self, ParsingError>

Parse a pattern

Examples found in repository?
examples/walker.rs (lines 6-11)
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}
Source

pub fn is_absolute(&self) -> bool

Check if the pattern is absolute (only matches absolute paths)

Source

pub fn prefix(&self) -> Option<PathPrefix>

Get the path prefix

Source

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.

Source

pub fn match_against(&self, path: &Path) -> PatternMatchResult

Source

pub fn common_root_dir(&self) -> &Path

Get the common root directory for all possible matches of this pattern

Source

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/b matches /a/b but cannot match any descendant
  • /a/**/b matches /a/b and may match some descendants
  • /a/b/** matches /a/b and may match some descendants

Trait Implementations§

Source§

impl Clone for Pattern

Source§

fn clone(&self) -> Pattern

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Pattern

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.