Skip to main content

Selector

Struct Selector 

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

Selector.

Selectors are used to match identifiers. Like identifiers, they consist of six components, which can be set to specific values or left empty to act as wildcards. Each component can be set to a glob as supported by globset, which allows for powerful matching capabilities.

Selectors are no means to an end, but rather a building block to associate data or functions to identifiers via the construction of a Matcher, which uses an efficient algorithm to match an arbitrary set of selectors in linear time. While it’s recommended to use Selector::builder together with the associated methods to create a new selector, selectors can also be created from a structured string representation with Selector::from_str, which is used internally for serializing them to persistent storage:

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

This ensures blazing fast cloning and editing. Additionally, selectors are guaranteed to not contain backslashes or path traversals in components. An empty component is interpreted as a wildcard, and thus matches all values in that component for any given selector.

§Examples

Create a selector:

use zrx_id::Selector;

// Create selector builder
let builder = Selector::builder().location("**/*.md");

// Create selector from builder
let selector = builder.build()?;
assert_eq!(selector.as_str(), "zrs:::::**/*.md:");

Create a selector from a string:

use zrx_id::Selector;

// Create selector from string
let selector: Selector = "zrs:::::**/*.md:".parse()?;

Implementations§

Source§

impl Selector

Source

pub fn builder<'a>() -> Builder<'a>

Creates a selector builder.

§Examples
use zrx_id::Selector;

// Create selector builder
let builder = Selector::builder();
Source

pub fn to_builder(&self) -> Builder<'_>

Creates a builder from the selector.

This method creates a builder from the current selector, which allows to modify components and build a new selector from an existing one.

§Examples
use zrx_id::Selector;

// Create selector from string
let selector: Selector = "zrs:::::**/*.md:".parse()?;

// Create selector builder
let builder = selector.to_builder().location("index.md");

// Create selector from builder
let selector = builder.build()?;
assert_eq!(selector.as_str(), "zrs:::::index.md:");
Source§

impl Selector

Source

pub fn as_str(&self) -> &str

Returns the string representation.

§Examples
use zrx_id::Selector;

// Create selector from string
let selector: Selector = "zrs:::::**/*.md:".parse()?;

// Obtain string representation
assert_eq!(selector.as_str(), "zrs:::::**/*.md:");
Source§

impl Selector

Source

pub fn provider(&self) -> Option<Cow<'_, str>>

Returns the provider component, if any.

Source

pub fn resource(&self) -> Option<Cow<'_, str>>

Returns the resource component, if any.

Source

pub fn variant(&self) -> Option<Cow<'_, str>>

Returns the variant component, if any.

Source

pub fn context(&self) -> Option<Cow<'_, str>>

Returns the context component, if any.

Source

pub fn location(&self) -> Option<Cow<'_, str>>

Returns the location component, if any.

Source

pub fn fragment(&self) -> Option<Cow<'_, str>>

Returns the fragment component, if any.

Trait Implementations§

Source§

impl AsRef<Format<7>> for Selector

Source§

fn as_ref(&self) -> &Format<7>

Returns the formatted string.

Note that it’s normally not necessary to access the formatted string directly, as all components can be accessed via the respective methods. We need to access the underlying formatted string in our internal APIs, e.g., to compute the Specificity for the given Selector.

Source§

impl Clone for Selector

Source§

fn clone(&self) -> Selector

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 Selector

Source§

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

Formats the selector for debugging.

Source§

impl Display for Selector

Source§

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

Formats the selector for display.

Source§

impl From<Selector> for Term

Source§

fn from(selector: Selector) -> Self

Creates a term from a selector.

Source§

impl FromStr for Selector

Source§

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

Attempts to create a selector 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::Prefix if the prefix isn’t zrs. Low-level format errors are returned as part of Error::Format.

§Examples
use zrx_id::Selector;

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

type Err = Error

The associated error which can be returned from parsing.
Source§

impl Hash for Selector

Source§

fn hash<H>(&self, state: &mut H)
where H: Hasher,

Hashes the selector.

Since selectors are immutable, we can use a precomputed hash for fast hashing. This is especially useful when selectors are used as keys in hash maps or hash sets, where hashing is a frequent operation, as the performance gains are significant.

1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for Selector

Source§

fn cmp(&self, other: &Self) -> Ordering

Orders two selectors.

§Examples
use zrx_id::Selector;

// Create and compare selectors
let a: Selector = "zrs:::::**/*.md:".parse()?;
let b: Selector = "zrs:::::**/*.rs:".parse()?;
assert!(a < b);
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Selector

Source§

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

Compares two selectors for equality.

§Examples
use zrx_id::Selector;

// Create and compare selectors
let a: Selector = "zrs:::::**/*.md:".parse()?;
let b: Selector = "zrs:::::**/*.md:".parse()?;
assert_eq!(a, b);
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Selector

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

Orders two selectors.

§Examples
use zrx_id::Selector;

// Create and compare selectors
let a: Selector = "zrs:::::**/*.md:".parse()?;
let b: Selector = "zrs:::::**/*.rs:".parse()?;
assert!(a < b);
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl ToSpecificity for Selector

Source§

fn to_specificity(&self) -> Specificity

Computes the specificity of the selector.

§Examples
use zrx_id::selector;
use zrx_id::specificity::ToSpecificity;

// Create selector and compute specificity
let selector = selector!(location = "**/*.md")?;
assert_eq!(selector.to_specificity(), (0, 1, 1, 3).into());
Source§

impl TryFrom<Id> for Selector

Source§

fn try_from(id: Id) -> Result<Self>

Attempts to create a selector from an identifier.

An Id can be converted into a Selector because all identifiers are also valid selectors, as they represent exact matches. However, the reverse is not true, as selectors can contain wildcards, as well as optional components, which identifiers cannot.

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

// Create selector from identifier
let id: Id = "zri:file:::docs:index.md:".parse()?;
let selector: Selector = id.try_into()?;
Source§

type Error = Error

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

impl TryFrom<Term> for Selector

Source§

fn try_from(term: Term) -> Result<Self>

Attempts to create a selector from a term.

§Examples
use zrx_id::expression::Term;
use zrx_id::{Id, Selector};

// Create selector from identifier
let id: Id = "zri:file:::docs:index.md:".parse()?;
let selector: Selector = Term::from(id).try_into()?;
Source§

type Error = Error

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

impl TryToSelector for Selector

Source§

fn try_to_selector(&self) -> Result<Cow<'_, Selector>>

Attempts to convert to a selector.

Source§

impl Eq for Selector

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<K, V> TryAsStorage<K> for V
where K: Key, V: Value,

Source§

fn try_as_storage( item: &(dyn Any + 'static), ) -> Result<<V as TryAsStorage<K>>::Target<'_>, Error>

Attempts to convert into a storage reference.

§Errors

The following errors might be returned:

§Examples
use std::any::Any;
use zrx_storage::convert::TryAsStorage;
use zrx_storage::Storage;

// Create storage and initial state
let mut storage = Storage::default();
storage.insert("key", 42);

// Obtain type-erased reference
let item: &dyn Any = &storage;

// Obtain storage reference
let storage = <i32>::try_as_storage(item)?;
Source§

type Target<'a> = &'a Storage<K, V>

Target type of conversion.
Source§

impl<K, V> TryAsStorageMut<K> for V
where K: Key, V: Value,

Source§

fn try_as_storage_mut( item: &mut (dyn Any + 'static), ) -> Result<<V as TryAsStorageMut<K>>::Target<'_>, Error>

Attempts to convert into a mutable storage reference.

§Errors

The following errors might be returned:

§Examples
use std::any::Any;
use zrx_storage::convert::TryAsStorageMut;
use zrx_storage::Storage;

// Create storage and initial state
let mut storage = Storage::default();
storage.insert("key", 42);

// Obtain mutable type-erased reference
let item: &mut dyn Any = &mut storage;

// Obtain mutable storage reference
let storage = <i32>::try_as_storage_mut(item)?;
Source§

type Target<'a> = &'a mut Storage<K, V>

Target type of conversion.
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.
Source§

impl<T> Id for T
where T: Clone + Debug + Display + Eq + Hash + Ord + Send + Sync + 'static,

Source§

impl<T> Key for T
where T: Clone + Debug + Eq + Hash + Ord + 'static,

Source§

impl<T> Value for T
where T: Debug + Eq + 'static,