Struct wax::Glob[][src]

pub struct Glob<'t> { /* fields omitted */ }

Implementations

Partitions a glob expression into an invariant PathBuf prefix and variant Glob postfix.

The invariant prefix contains no glob patterns nor other variant components and therefore can be interpretted as a native path. The Glob postfix is variant and contains the remaining components (in particular, patterns) that follow the prefix. For example, the glob expression .local/**/*.log would produce the path .local and glob **/*.log. It is possible for either partition to be empty.

Literal components may be considered variant if they contain characters with casing and the configured case sensitivity differs from the target platform’s file system. For example, the case-insensitive literal expression (?i)photos is considered variant on Unix and invariant on Windows, because the literal photos resolves differently in Unix file system APIs than Glob APIs (which respect the configured case-insensitivity).

Partitioning a glob expression allows any invariant prefix to be used as a native path to establish a working directory or to interpret semantic components that are not recognized by globs, such as parent directory .. components.

Partitioned Globs are never rooted. If the glob expression has a root component, then it is always included in the invariant PathBuf prefix.

Errors

Returns an error if the glob expression cannot be parsed or violates glob component rules.

Examples

To match files in a directory tree while respecting semantic components like .. on platforms that support it, the invariant prefix can be applied to a working directory. In the following example, walk reads and therefore resolves the path ./../site/img, respecting the meaning of the . and .. components.

use std::path::Path;
use wax::Glob;

let directory = Path::new("."); // Working directory.
let (prefix, glob) = Glob::partitioned("../site/img/*.{jpg,png}").unwrap();
for entry in glob.walk(directory.join(prefix), usize::MAX) {
    // ...
}

To match paths against a glob while respecting semantic components, the invariant prefix and candidate path can be canonicalized. The following example canonicalizes both the working directory joined with the prefix as well as the candidate path and then attempts to match the glob if the candidate path contains the prefix.

use dunce; // Avoids UNC paths on Windows.
use std::path::Path;
use wax::Glob;

let path: &Path = /* ... */ // Candidate path.

let directory = Path::new("."); // Working directory.
let (prefix, glob) = Glob::partitioned("../../src/**").unwrap();
let prefix = dunce::canonicalize(directory.join(&prefix)).unwrap();
if dunce::canonicalize(path)
    .unwrap()
    .strip_prefix(&prefix)
    .map(|path| glob.is_match(path))
    .unwrap_or(false)
{
    // ...
}

The above examples illustrate particular approaches, but the invariant prefix can be used to interact with native paths as needed for a given application.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

The associated error which can be returned from parsing.

Parses a string s to return a value of this type. Read more

The type returned in the event of a conversion error.

Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.