Skip to main content

Matcher

Struct Matcher 

Source
pub struct Matcher { /* private fields */ }
Expand description

Matcher.

Matchers provide efficient matching of identifiers against an arbitrary set of selectors in linear time, implemented through the use of the globset crate, which compiles globs into deterministic finite automata (DFA). Each [Component] of the matcher receives its own distinct GlobSet.

While components are matched one after another, all registered identifiers in a [Component] are matched in linear time, i.e., O(n), where n is the length of the component value. The Matches returned by each component are intersected, leaving only selectors that match all components. There are theoretical limits on the number of selectors that can be added to a [Component], so it can be necessary to split across multiple matchers if the number of selectors is high, i.e., 10,000 or more.

§Examples

use zrx_id::{Id, Matcher};

// Create matcher builder and add selector
let mut builder = Matcher::builder();
builder.add(&"zrs:::::**/*.md:")?;

// Create matcher from builder
let matcher = builder.build()?;

// Create identifier and match selector
let id: Id = "zri:file:::docs:index.md:".parse()?;
assert!(matcher.is_match(&id)?);

Implementations§

Source§

impl Matcher

Source

pub fn builder() -> Builder

Creates a matcher builder.

§Examples
use zrx_id::Matcher;

// Create matcher builder
let mut builder = Matcher::builder();
Source§

impl Matcher

Source

pub fn is_match<T>(&self, id: &T) -> Result<bool>
where T: TryToId,

Returns whether the given identifier matches any selector.

Components are compared in descending variability and their likelihood for mismatch, starting with the location. This approach effectively tries to short-circuits the comparison. Note that empty components are considered wildcards, so they will always match.

§Errors

Returns Error::Id if the identifier is invalid.

§Examples
use zrx_id::{Id, Matcher};

// Create matcher builder and add selector
let mut builder = Matcher::builder();
builder.add(&"zrs:::::**/*.md:")?;

// Create matcher from builder
let matcher = builder.build()?;

// Create identifier and match selector
let id: Id = "zri:file:::docs:index.md:".parse()?;
assert!(matcher.is_match(&id)?);
Source

pub fn matches<T>(&self, id: &T) -> Result<Matches>
where T: TryToId,

Returns the indices of selectors that match the identifier.

This method compares each component of the identifier against the corresponding component of a selector using the compiled globs, and returns the indices of the matching selectors in the order they were added to the Matcher.

Components are compared in descending variability and their likelihood for mismatch, starting with the location. This approach effectively tries to short-circuit the comparison. Note that empty components are considered wildcards, so they will always match.

§Errors

Returns Error::Id if the identifier is invalid.

§Examples
use zrx_id::{Id, Matcher, Matches};

// Create matcher builder and add selector
let mut builder = Matcher::builder();
builder.add(&"zrs:::::**/*.md:")?;

// Create matcher from builder
let matcher = builder.build()?;

// Create identifier and obtain matched selectors
let id: Id = "zri:file:::docs:index.md:".parse()?;
assert_eq!(matcher.matches(&id)?, Matches::from_iter([0]));

Trait Implementations§

Source§

impl Clone for Matcher

Source§

fn clone(&self) -> Matcher

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 Matcher

Source§

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

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

impl Default for Matcher

Source§

fn default() -> Matcher

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

impl FromStr for Matcher

Source§

fn from_str(value: &str) -> Result<Self>

Attempts to create a matcher from a string.

The string must adhere to the following format and include exactly six : separators, even if some components are empty. All components are optional, which means they can be left empty, which is equivalent to setting them to a ** wildcard.

zrs:<provider>:<resource>:<variant>:<context>:<location>:<fragment>
§Errors

Returns Error::Id if the given string can’t be parsed into a valid selector, from which the matcher is then constructed.

§Examples
use zrx_id::Matcher;

// Create matcher from string
let matcher: Matcher = "zrs:::::**/*.md:".parse()?;
Source§

type Err = Error

The associated error which can be returned from parsing.

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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.