Skip to main content

Specificity

Struct Specificity 

Source
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

Source§

fn clone(&self) -> Specificity

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 Specificity

Source§

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

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

impl Default for Specificity

Source§

fn default() -> Specificity

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

impl From<(u16, u16, u16, u16)> for Specificity

Source§

fn from((a, b, c, l): (u16, u16, u16, u16)) -> Self

Creates a specificity from a tuple.

Source§

impl<T> From<T> for Specificity
where T: ToSpecificity,

Source§

fn from(value: T) -> Self

Creates a specificity from a value.

Source§

impl Ord for Specificity

Source§

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) -> 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 Specificity

Source§

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

Tests for self and other values to be equal, and is used by ==.
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 Specificity

Source§

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());
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 Copy for Specificity

Source§

impl Eq for Specificity

Source§

impl StructuralPartialEq for Specificity

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<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> Value for T
where T: Debug + Eq + 'static,