Skip to main content

Pattern

Struct Pattern 

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

A pattern over broadcast paths: literal segments, * for one segment, prefix*suffix for one segment with a known start and end, and at most one ** for any run of segments. Every segment kind matches whole segments, and a pattern is exact: foo matches only foo, and a subtree is foo/**.

Build one with FromStr ("a/*/**".parse()), new from segments, or literal and subtree from a path. Equality and ordering are by text, which is canonical: two patterns match the same paths when they are equal, and only then. Construction moves ** before adjacent * segments.

Implementations§

Source§

impl Pattern

Source

pub const MAX_SEGMENTS: usize = 32

The most segments a pattern may have, matching the path limit on the wire.

Source

pub const MAX_INTERSECTIONS: usize = 1024

The most patterns one exact intersection may produce.

Source

pub fn new( segments: impl IntoIterator<Item = Segment>, ) -> Result<Pattern, InvalidPattern>

A pattern from its segments, validating the grammar and moving ** before adjacent * segments.

Source

pub fn all() -> Pattern

The pattern matching every path: **.

Source

pub fn literal(path: &str) -> Result<Pattern, InvalidPattern>

The pattern matching exactly path.

The path is normalized like a broadcast path (slashes trimmed and collapsed), so /foo//bar/ is foo/bar. Fails when a segment is * or **, or contains *: those are wildcards, and a path using them cannot be named by a pattern.

Source

pub fn subtree(path: &str) -> Result<Pattern, InvalidPattern>

The pattern matching path and every path beneath it: path/**.

Normalizes and validates path like literal. The empty path yields **.

Source

pub fn as_str(&self) -> &str

The canonical text: segments joined by /, wildcards as * and **.

Source

pub fn segments(&self) -> &[Segment]

The segments, in order.

Source

pub fn head(&self) -> &str

The literal segments before the first wildcard, as a path.

Every matching path starts with it, so it is where a tree walk starts. Empty when the pattern starts with a wildcard; the whole pattern when it has none.

Source

pub fn is_literal(&self) -> bool

Whether the pattern has no wildcards, so it matches exactly one path.

Source

pub fn as_prefix(&self) -> Option<&str>

The covered prefix if this pattern is prefix-shaped: zero or more literals then **.

** covers every path (the empty prefix). foo/** covers foo and everything beneath it. A literal, a *, or a ** that is not last is None.

Source

pub fn has_globstar(&self) -> bool

Whether the pattern has a **, so it matches paths of more than one length.

Source

pub fn matches(&self, path: &str) -> bool

Whether path is in the set this pattern describes.

The path is normalized like a broadcast path: slashes are trimmed and collapsed.

Source

pub fn contains(&self, other: &Pattern) -> bool

Whether every path other matches, this pattern matches too.

This is the authorization check: a grant contains a request when the request cannot name a path outside it. A pattern contains itself.

Source

pub fn overlaps(&self, other: &Pattern) -> bool

Whether some path matches both patterns.

Source

pub fn specificity(&self) -> Specificity

How much of a path this pattern pins down. See Specificity.

Source

pub fn rebase(&self, root: &str) -> Patterns

The patterns that, relative to root, match exactly the paths this pattern matches beneath root.

This is how a grant or an advertisement is presented inside a rooted view. It is a set because ** may consume the root or stop short of it: **/a rebased at a is both the empty pattern (the root itself) and **/a (deeper paths ending in a). Empty when nothing under root matches. The root is normalized like a broadcast path.

Source

pub fn intersect(&self, other: &Pattern) -> Result<Patterns, IntersectionError>

The patterns matching exactly the paths both patterns match.

This is how a claim is clamped to a scope: the covered paths inside the grant, as patterns of their own. It is a set because two partial segments or two ** runs can meet in more than one way: ab* and *b meet at ab*b and at ab, and a/** and **/a meet at a/**/a and at a. Empty when the two do not overlap.

Source

pub fn captures(&self, matched: &Pattern) -> Option<Vec<Pattern>>

What each wildcard of this pattern stands for in matched, a pattern this one contains; None when it does not.

One capture per non-literal segment (*, prefix*suffix, **), in order, the way a regex match exposes its groups: foo/*/chat against foo/alice/chat captures alice, and foo/** against foo/alice/chat captures alice/chat. A capture is a pattern because matched may be one: foo/** against foo/alice/** captures alice/**. When matched has a ** that this pattern’s own segments straddle (**/* against a/**, where the last segment is a or anything after it), the segments it straddles cannot be pinned and capture themselves: ** then *.

Source

pub fn rooted(&self, root: &str) -> Result<Pattern, InvalidPattern>

This pattern placed beneath a literal root: the same paths, named from the root’s parent. The inverse of rebase for a single pattern.

The root is normalized and validated like literal, and the result must fit MAX_SEGMENTS.

Trait Implementations§

Source§

impl AsRef<str> for Pattern

Source§

fn as_ref(&self) -> &str

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for Pattern

Source§

fn clone(&self) -> Pattern

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Pattern

Source§

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

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

impl Default for Pattern

Source§

fn default() -> Pattern

The empty pattern, which matches only the empty path.

Source§

impl<'de> Deserialize<'de> for Pattern

Available on crate feature serde only.
Source§

fn deserialize<D>( deserializer: D, ) -> Result<Pattern, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,

Reads the canonical text, so a persisted pattern is validated on the way in.

Source§

impl Display for Pattern

Source§

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

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

impl Eq for Pattern

Source§

impl Extend<Pattern> for Patterns

Source§

fn extend<I>(&mut self, iter: I)
where I: IntoIterator<Item = Pattern>,

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: T)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl From<Pattern> for Patterns

Source§

fn from(pattern: Pattern) -> Patterns

Converts to this type from the input type.
Source§

impl FromIterator<Pattern> for Patterns

Source§

fn from_iter<I>(iter: I) -> Patterns
where I: IntoIterator<Item = Pattern>,

Creates a value from an iterator. Read more
Source§

impl FromStr for Pattern

Source§

fn from_str(text: &str) -> Result<Pattern, InvalidPattern>

Parse a pattern’s text. Unlike a path, slashes are not normalized: a leading, trailing, or doubled / is an error, so a typo cannot silently widen a grant.

Source§

type Err = InvalidPattern

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

impl Hash for Pattern

Source§

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

Feeds this value into the given Hasher. Read more
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 Pattern

Source§

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

Ordered by text, so a sorted list of patterns is deterministic.

1.21.0 (const: unstable) · Source§

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

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

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

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

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

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

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for Pattern

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl PartialOrd for Pattern

Source§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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 Serialize for Pattern

Available on crate feature serde only.
Source§

fn serialize<S>( &self, serializer: S, ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Pattern

Source§

impl TryFrom<&str> for Pattern

Source§

type Error = InvalidPattern

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

fn try_from(text: &str) -> Result<Pattern, InvalidPattern>

Performs the conversion.
Source§

impl TryFrom<String> for Pattern

Source§

type Error = InvalidPattern

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

fn try_from(text: String) -> Result<Pattern, InvalidPattern>

Performs the conversion.

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> MaybeSend for T
where T: Send,

Source§

impl<T> MaybeSend for T
where T: Send + ?Sized,

Source§

impl<T> MaybeSend for T
where T: Send,

Source§

impl<T> MaybeSync for T
where T: Sync,

Source§

impl<T> MaybeSync for T
where T: Sync,

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<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more