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
impl Selector
Sourcepub fn builder<'a>() -> Builder<'a>
pub fn builder<'a>() -> Builder<'a>
Creates a selector builder.
§Examples
use zrx_id::Selector;
// Create selector builder
let builder = Selector::builder();Sourcepub fn to_builder(&self) -> Builder<'_>
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:");Trait Implementations§
Source§impl AsRef<Format<7>> for Selector
impl AsRef<Format<7>> for Selector
Source§fn as_ref(&self) -> &Format<7>
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 FromStr for Selector
impl FromStr for Selector
Source§fn from_str(value: &str) -> Result<Self>
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§impl Hash for Selector
impl Hash for Selector
Source§fn hash<H>(&self, state: &mut H)where
H: Hasher,
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.
Source§impl Ord for Selector
impl Ord for Selector
Source§fn cmp(&self, other: &Self) -> Ordering
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) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialEq for Selector
impl PartialEq for Selector
Source§impl PartialOrd for Selector
impl PartialOrd for Selector
Source§fn partial_cmp(&self, other: &Self) -> Option<Ordering>
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);Source§impl ToSpecificity for Selector
impl ToSpecificity for Selector
Source§fn to_specificity(&self) -> Specificity
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
impl TryFrom<Id> for Selector
Source§fn try_from(id: Id) -> Result<Self>
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§impl TryFrom<Term> for Selector
impl TryFrom<Term> for Selector
Source§impl TryToSelector for Selector
impl TryToSelector for Selector
impl Eq for Selector
Auto Trait Implementations§
impl Freeze for Selector
impl RefUnwindSafe for Selector
impl Send for Selector
impl Sync for Selector
impl Unpin for Selector
impl UnsafeUnpin for Selector
impl UnwindSafe for Selector
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<K, V> TryAsStorage<K> for V
impl<K, V> TryAsStorage<K> for V
Source§fn try_as_storage(
item: &(dyn Any + 'static),
) -> Result<<V as TryAsStorage<K>>::Target<'_>, Error>
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:
Error::Downcast: Item cannot be downcast.
§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§impl<K, V> TryAsStorageMut<K> for V
impl<K, V> TryAsStorageMut<K> for V
Source§fn try_as_storage_mut(
item: &mut (dyn Any + 'static),
) -> Result<<V as TryAsStorageMut<K>>::Target<'_>, Error>
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:
Error::Downcast: Item cannot be downcast.
§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)?;