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 Glob
s 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
Auto Trait Implementations
impl<'t> RefUnwindSafe for Glob<'t>
impl<'t> UnwindSafe for Glob<'t>
Blanket Implementations
Mutably borrows from an owned value. Read more