pub struct PathPat(pub Vec<SegPat>);Expand description
A path pattern. Unlike a concrete Seg path it can reach every element of
a sequence (SegPat::EachItem), every entry of a mapping
(SegPat::AnyKey), or a whole subtree (SegPat::AnyDepth), so a rule
can constrain each item of a list field (tags:, audience:) or
everything beneath a key.
Tuple Fields§
§0: Vec<SegPat>Implementations§
Source§impl PathPat
impl PathPat
Sourcepub fn key(name: impl Into<String>) -> Self
pub fn key(name: impl Into<String>) -> Self
A single top-level key — the common case (audience, title).
Sourcepub fn each_item_of(name: impl Into<String>) -> Self
pub fn each_item_of(name: impl Into<String>) -> Self
A top-level list field whose each item the rule governs
(audience: as a sequence).
Sourcepub fn subtree_of(name: impl Into<String>) -> Self
pub fn subtree_of(name: impl Into<String>) -> Self
A top-level key and everything nested beneath it, the key itself
included (meta, meta.author, meta.tags.0).
Sourcepub fn matches(&self, path: &[Seg]) -> bool
pub fn matches(&self, path: &[Seg]) -> bool
Whether this pattern matches the concrete fig path. Without an
SegPat::AnyDepth this is a segment-wise match of equal lengths; with
one, the pattern may span any number of segments there.
Sourcepub fn parse(text: &str) -> Result<PathPat, PatternError>
pub fn parse(text: &str) -> Result<PathPat, PatternError>
Read a pattern from the text the schema document format’s at holds:
keys joined by ., * for any key, ** for any number of segments of
any kind, [] for each item of a sequence, [0] for one index. The
empty string is the root.
* and ** are whole segments — au* is the key au*, not a glob —
and a segment is what is written: whitespace is not trimmed. The
grammar has no quoting, as fig’s own parsePath has none, so a key
containing . or [ cannot be addressed and a key literally named *
cannot be addressed as a key. Both limits are inherited from fig and
would be lifted by giving fig’s path grammar quoting first.
use fig_schema::{PathPat, SegPat};
assert_eq!(PathPat::parse("audience[]").unwrap(), PathPat::each_item_of("audience"));
assert_eq!(PathPat::parse("meta.**").unwrap(), PathPat::subtree_of("meta"));
assert_eq!(
PathPat::parse("items[0].title").unwrap(),
PathPat(vec![SegPat::Key("items".into()), SegPat::Index(0), SegPat::Key("title".into())]),
);
assert_eq!(PathPat::parse("").unwrap(), PathPat(vec![]));
assert!(PathPat::parse("a..b").is_err());Sourcepub fn concrete(&self) -> Option<Vec<Seg>>
pub fn concrete(&self) -> Option<Vec<Seg>>
This pattern as a concrete path, if it has no wildcard in it — what a
command that takes a path from a person parses one with, since the
grammar is the same and only *, ** and [] are refused.
use fig_schema::{PathPat, Seg};
let path = PathPat::parse("audience[1]").unwrap().concrete().unwrap();
assert_eq!(path, vec![Seg::Key("audience".into()), Seg::Index(1)]);
assert_eq!(PathPat::parse("audience[]").unwrap().concrete(), None);Sourcepub fn is_subsumed_by(&self, other: &PathPat) -> bool
pub fn is_subsumed_by(&self, other: &PathPat) -> bool
Whether every path this pattern matches is also matched by other —
so a rule at self listed after one at other can never govern
anything. Sound but not complete: a true is always right, and a
false may only mean the question was too hard, which for a lint is
the right way round.