pub struct PatternOptions { /* private fields */ }Expand description
Explicit switches that affect glob interpretation.
Two starting points have names. PatternOptions::default is the
conservative reading: **, braces and extglobs are ordinary text until a
switch enables them. PatternOptions::walker is the dialect the ferralk
walker compiles its include and exclude patterns in. Either one can be
adjusted further with the builder methods.
use ferralk_glob::{Pattern, PatternOptions};
// `walker()` is `default()` with three switches turned on.
assert_eq!(
PatternOptions::walker(),
PatternOptions::default()
.recursive_double_star(true)
.braces(true)
.extglob(true),
);
// The same text is a different pattern under each of them.
let walker = Pattern::compile("*.{rs,toml}", PatternOptions::walker())?;
let conservative = Pattern::compile("*.{rs,toml}", PatternOptions::default())?;
assert!(walker.is_match_glob_path("Cargo.toml"));
assert!(!conservative.is_match_glob_path("Cargo.toml"));
// Further switches chain onto either one.
let loose = PatternOptions::walker().match_hidden(true).case_insensitive(true);
assert!(Pattern::compile("*.RS", loose)?.is_match_glob_path(".build.rs"));Implementations§
Source§impl PatternOptions
impl PatternOptions
Sourcepub const fn walker() -> Self
pub const fn walker() -> Self
The dialect ferralk::Walker compiles its include and exclude patterns
in: default with recursive **, braces and extglobs
enabled.
Compile a walker pattern with these options to validate it up front or
to match it again with the walker’s semantics, for instance inside a
visit predicate or over paths that never touched a filesystem. The
walker builds its own options from this constructor, so the two cannot
drift apart.
Every other switch keeps its default. In particular a wildcard does not
cover a leading period, which matches the walker’s default for
includes; chain match_hidden(true) to mirror
Walker::match_hidden(true), or to match an exclude, which the walker
always lets cover a leading period. Case folding and escaping are not
walker settings and stay at their defaults there too.
Options select syntax, and the entry point selects how far a wildcard
reaches. For a root-relative path, Pattern::is_match_glob_path
answers as the walker does under its default
WildcardMode::ComponentScoped, and Pattern::is_match as it does
under WildcardMode::SeparatorCrossing. A few walker rules sit outside
the pattern dialect: the walker reads a trailing / as “directories
only”, rewrites an absolute pattern against its root, and refuses a
pattern no walk can match, such as one with a .. component, which
Pattern::compile accepts as matcher text.
use ferralk_glob::{Pattern, PatternOptions};
let walker = PatternOptions::walker();
let conservative = PatternOptions::default();
// `**` crosses directories only in the walker dialect; by default it
// is two ordinary stars that stay inside one component.
let recursive = Pattern::compile("src/**/*.rs", walker)?;
assert!(recursive.is_match_glob_path("src/lib.rs"));
assert!(recursive.is_match_glob_path("src/parser/token.rs"));
let plain = Pattern::compile("src/**/*.rs", conservative)?;
assert!(!plain.is_match_glob_path("src/lib.rs"));
// Braces are alternatives in the walker dialect and literal text by
// default.
let either = Pattern::compile("*.{rs,toml}", walker)?;
assert!(either.is_match_glob_path("lib.rs"));
assert!(either.is_match_glob_path("Cargo.toml"));
let literal = Pattern::compile("*.{rs,toml}", conservative)?;
assert!(!literal.is_match_glob_path("lib.rs"));
assert!(literal.is_match_glob_path("odd.{rs,toml}"));
// Like the walker, the preset leaves hidden names to an explicit opt-in.
assert!(!recursive.is_match_glob_path("src/.cache/lib.rs"));
let hidden = Pattern::compile("src/**/*.rs", walker.match_hidden(true))?;
assert!(hidden.is_match_glob_path("src/.cache/lib.rs"));Sourcepub const fn recursive_double_star(self, enabled: bool) -> Self
pub const fn recursive_double_star(self, enabled: bool) -> Self
Gives a ** that forms a whole path component recursive,
separator-crossing semantics, as gitignore, Bash globstar, globset,
and fast-glob do.
A star run is a whole component when an unescaped / or an end of the
pattern bounds it on each side: **, **/x, x/**, x/**/y. Braces
are expanded first, so every alternative is judged as the pattern it
expands to, and an extglob alternative takes its group’s position. A
**/ matches zero or more whole components and hands over only at a
component start, so **/x matches x and a/x but not sx, and
a/**/b matches a/b but not a/xb. A trailing /** also accepts the
path without it: x/** matches x. An extglob group reads like its
alternatives written in its place, so both rules hold around groups
too: @(**)/y matches y, and @(x)/** and x/@(**) match x.
Any other run, such as a**, **b, or **.ts, stays ordinary stars:
component-local under Pattern::is_match_glob_path, the position
rule under Pattern::is_match_path, and separator-crossing under
Pattern::is_match. When disabled, every run reads that way.
use ferralk_glob::{Pattern, PatternOptions};
let options = PatternOptions::default().recursive_double_star(true);
let whole = Pattern::compile("**/x", options)?;
assert!(whole.is_match_glob_path("x"));
assert!(whole.is_match_glob_path("a/b/x"));
assert!(!whole.is_match_glob_path("a/sx"));
let attached = Pattern::compile("a**/x", options)?;
assert!(attached.is_match_glob_path("abc/x"));
assert!(!attached.is_match_glob_path("a/b/x"));Allows wildcard tokens to match a leading period in a path component.
Sourcepub const fn case_insensitive(self, enabled: bool) -> Self
pub const fn case_insensitive(self, enabled: bool) -> Self
Performs ASCII-only case-insensitive matching.