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
impl Pattern
Sourcepub const MAX_SEGMENTS: usize = 32
pub const MAX_SEGMENTS: usize = 32
The most segments a pattern may have, matching the path limit on the wire.
Sourcepub const MAX_INTERSECTIONS: usize = 1024
pub const MAX_INTERSECTIONS: usize = 1024
The most patterns one exact intersection may produce.
Sourcepub fn new(
segments: impl IntoIterator<Item = Segment>,
) -> Result<Pattern, InvalidPattern>
pub fn new( segments: impl IntoIterator<Item = Segment>, ) -> Result<Pattern, InvalidPattern>
A pattern from its segments, validating the grammar and moving ** before adjacent * segments.
Sourcepub fn literal(path: &str) -> Result<Pattern, InvalidPattern>
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.
Sourcepub fn subtree(path: &str) -> Result<Pattern, InvalidPattern>
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 **.
Sourcepub fn head(&self) -> &str
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.
Sourcepub fn is_literal(&self) -> bool
pub fn is_literal(&self) -> bool
Whether the pattern has no wildcards, so it matches exactly one path.
Sourcepub fn as_prefix(&self) -> Option<&str>
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.
Sourcepub fn has_globstar(&self) -> bool
pub fn has_globstar(&self) -> bool
Whether the pattern has a **, so it matches paths of more than one length.
Sourcepub fn matches(&self, path: &str) -> bool
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.
Sourcepub fn contains(&self, other: &Pattern) -> bool
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.
Sourcepub fn specificity(&self) -> Specificity
pub fn specificity(&self) -> Specificity
How much of a path this pattern pins down. See Specificity.
Sourcepub fn rebase(&self, root: &str) -> Patterns
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.
Sourcepub fn intersect(&self, other: &Pattern) -> Result<Patterns, IntersectionError>
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.
Sourcepub fn captures(&self, matched: &Pattern) -> Option<Vec<Pattern>>
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 *.
Sourcepub fn rooted(&self, root: &str) -> Result<Pattern, InvalidPattern>
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<'de> Deserialize<'de> for Pattern
Available on crate feature serde only.
impl<'de> Deserialize<'de> for Pattern
serde only.Source§fn deserialize<D>(
deserializer: D,
) -> Result<Pattern, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
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.
impl Eq for Pattern
Source§impl Extend<Pattern> for Patterns
impl Extend<Pattern> for Patterns
Source§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = Pattern>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = Pattern>,
Source§fn extend_one(&mut self, item: T)
fn extend_one(&mut self, item: T)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl FromIterator<Pattern> for Patterns
impl FromIterator<Pattern> for Patterns
Source§impl FromStr for Pattern
impl FromStr for Pattern
Source§fn from_str(text: &str) -> Result<Pattern, InvalidPattern>
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
type Err = InvalidPattern
Source§impl Ord for Pattern
impl Ord for Pattern
Source§fn cmp(&self, other: &Pattern) -> Ordering
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) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialOrd for Pattern
impl PartialOrd for Pattern
Source§impl Serialize for Pattern
Available on crate feature serde only.
impl Serialize for Pattern
serde only.