pub struct Specificity(/* private fields */);Expand description
Specificity.
Specificity is an ordering and tie-breaking concept borrowed from CSS, where
more specific selectors take precedence over less specific ones. Specificity
is computable for the likes of Expression, Term, Operand,
Id, Selector, and Glob.
§Representation
Specificity is represented as a 4-tuple (a, b, c, l), which is compared
in lexicographic order, meaning that components are compared in sequence:
a– number of segments with literals only, e.g.src,main.rs.b– number of segments with single-wildcards, e.g.*,?,[abc].c– number of segments with double-wildcards, compared in reverse.l– number of literals across all segments.
§Atoms
In a Segment, atoms are combined with Specificity::min_sum_len -
the structural component a, b, or c is assigned by taking the minimum
across all atoms in the segment, whereas the length component l receives
the sum across all atoms in the segment.
§Ids and selectors
The specificity of an Id or Selector is computed by summing the
specificities of its components, where the specificity of each component is
computed individually and then combined with Specificity::sum. Empty
components receive the Specificity::default, which is (0, 0, 0, 0).
zrs:{git,file}:::{docs}:index.md: # (3, 0, 0, 15)
zrs::::docs:{index,about}.md: # (2, 0, 0, 12)
zrs:::::index.{md,rst}: # (1, 0, 0, 8)
zrs:::::{*}: # (0, 1, 0, 0)§Expressions
An Expression is a combination of multiple Id and Selector
terms, with its specificity computed according to its Operator:
-
Expression::any: takes the minimum. The expression is as specific as its least specific operand, since any operand can match. -
Expression::all: sums specificities. The expression is as specific as the combination of all its operands, since all operands must match. -
Expression::not: contributes nothing, i.e.,(0, 0, 0, 0), since a negation is a guard that filters matches but does not select them.
Alternate groups, e.g. {jpg,png}, are equivalent to Expression::any
at the Atom level and follow the same rules.
§Examples
use zrx_id::specificity::ToSpecificity;
use zrx_id::{selector, Expression};
// Create expression and compute specificity
let expr = Expression::any(|expr| {
expr.with(selector!(location = "**/*.jpg")?)?
.with(selector!(location = "**/*.png")?)
})?;
assert_eq!(expr.to_specificity(), (0, 1, 1, 4).into());Trait Implementations§
Source§impl Clone for Specificity
impl Clone for Specificity
Source§fn clone(&self) -> Specificity
fn clone(&self) -> Specificity
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for Specificity
impl Debug for Specificity
Source§impl Default for Specificity
impl Default for Specificity
Source§fn default() -> Specificity
fn default() -> Specificity
Source§impl<T> From<T> for Specificitywhere
T: ToSpecificity,
impl<T> From<T> for Specificitywhere
T: ToSpecificity,
Source§impl Ord for Specificity
impl Ord for Specificity
Source§fn cmp(&self, other: &Self) -> Ordering
fn cmp(&self, other: &Self) -> Ordering
Orders two specificities.
§Examples
use zrx_id::selector;
use zrx_id::specificity::ToSpecificity;
// Create and compare selectors by specificity
let a = selector!(location = "**/*.md")?;
let b = selector!(location = "*.md")?;
assert!(a.to_specificity() < b.to_specificity());1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialEq for Specificity
impl PartialEq for Specificity
Source§impl PartialOrd for Specificity
impl PartialOrd for Specificity
Source§fn partial_cmp(&self, other: &Self) -> Option<Ordering>
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
Orders two specificities.
§Examples
use zrx_id::selector;
use zrx_id::specificity::ToSpecificity;
// Create and compare selectors by specificity
let a = selector!(location = "**/*.md")?;
let b = selector!(location = "*.md")?;
assert!(a.to_specificity() < b.to_specificity());impl Copy for Specificity
impl Eq for Specificity
impl StructuralPartialEq for Specificity
Auto Trait Implementations§
impl Freeze for Specificity
impl RefUnwindSafe for Specificity
impl Send for Specificity
impl Sync for Specificity
impl Unpin for Specificity
impl UnsafeUnpin for Specificity
impl UnwindSafe for Specificity
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)?;