Skip to main content

PatternOptions

Struct PatternOptions 

Source
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

Source

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"));
Source

pub const fn braces(self, enabled: bool) -> Self

Enables nested brace alternatives.

Source

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"));
Source

pub const fn extglob(self, enabled: bool) -> Self

Enables Bash-style extglobs.

Source

pub const fn match_hidden(self, enabled: bool) -> Self

Allows wildcard tokens to match a leading period in a path component.

Source

pub const fn case_insensitive(self, enabled: bool) -> Self

Performs ASCII-only case-insensitive matching.

Source

pub const fn escape(self, enabled: bool) -> Self

Enables backslash escapes in patterns.

Trait Implementations§

Source§

impl Clone for PatternOptions

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for PatternOptions

Source§

impl Debug for PatternOptions

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for PatternOptions

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Eq for PatternOptions

Source§

impl PartialEq for PatternOptions

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for PatternOptions

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.