pub struct KeFormat<'s, Storage: IKeFormatStorage<'s> + 's = Vec<Segment<'s>>> { /* private fields */ }
Expand description
§Building and parsing Key Expressions
A common issue in REST API is the association of meaning to sections of the URL, and respecting that API in a convenient manner.
The same issue arises naturally when designing a KE space, and KeFormat
was designed to help you with this,
both in constructing and in parsing KEs that fit the formats you’ve defined.
kedefine
also allows you to define formats at compile time, allowing a more performant, but more importantly safer and more convenient use of said formats,
as the keformat
and kewrite
macros will be able to tell you if you’re attempting to set fields of the format that do not exist.
§The format syntax
KE formats are defined following a syntax that extends the keyexpr
syntax. In addition to existing chunk types, KE formmats support “specification” chunks.
These chunks must follow the one of the following syntaxes: ${id:pattern}
, ${id:pattern#default}
, $#{id:pattern}#
, or $#{id:pattern#default}#
, where:
id
is the chunk identifier: it cannot contain the:
character, and is used to name the chunk in accessors.pattern
must be a valid KE (and therefore cannot contain#
) and defines the range of values that the chunk may adopt.default
(optional) is used as the chunk value when formatting if the builder wasn’t supplied with a value forid
.
§Formatting
To use a format to build a Key Expression, its formatter must be constructed.
A formatter functions like as an id
-value map which can be KeFormatter::build
into a OwnedKeyExpr
once all specs have a value.
The formatter will notably prevent you from setting values for a spec that isn’t included by its pattern.
§Parsing
KeFormat
can also be used to parse any keyexpr
that intersects with it, using KeFormat::parse
.
The parser will then assign subsections of the keyexpr
to each spec, and the resulting Parsed
result can then be queried
for each spec’s assigned value.
Specs are considered greedy and evaluated left-to-right: if your format would allow ambiguous parsings, chunks will be consumed
by the leftmost specs first. For example ${a:**}/-/${b:**}
parsing hey/-/-/there
would assign hey/-
to a
and there
to b
,
(even though you might have expected a
to only consume hey
and b
to consume the remaining -/there
).
A good way to avoid ambiguities when working with formats that contain multiple **
specs is to separate such specs using verbatim chunks
(chunks that start with an @
), as **
is incapable of consuming these chunks.
Implementations§
Source§impl<'s, Storage: IKeFormatStorage<'s> + 's> KeFormat<'s, Storage>
impl<'s, Storage: IKeFormatStorage<'s> + 's> KeFormat<'s, Storage>
Source§impl<'s> KeFormat<'s, Vec<Segment<'s>>>
impl<'s> KeFormat<'s, Vec<Segment<'s>>>
Sourcepub fn new<S: AsRef<str> + ?Sized>(value: &'s S) -> ZResult<Self>
pub fn new<S: AsRef<str> + ?Sized>(value: &'s S) -> ZResult<Self>
Construct a new KeFormat
, using a vector to store its state-machine and parser results.
Sourcepub fn noalloc_new<const N: usize>(
value: &'s str,
) -> ZResult<KeFormat<'s, [Segment<'s>; N]>>
pub fn noalloc_new<const N: usize>( value: &'s str, ) -> ZResult<KeFormat<'s, [Segment<'s>; N]>>
Construct a new `KeFormat, using a stack-allocated array to store its state-machine and parser results.
N
is simply the number of specifications in value
. If this number of specs isn’t known at compile-time, use KeFormat::new
instead.
If you know value
at compile time, using kedefine
instead is advised,
as it will provide more features and construct higher performance formats than this constructor.
Source§impl<'s, Storage: IKeFormatStorage<'s> + 's> KeFormat<'s, Storage>
impl<'s, Storage: IKeFormatStorage<'s> + 's> KeFormat<'s, Storage>
Sourcepub fn formatter(&'s self) -> KeFormatter<'s, Storage>
pub fn formatter(&'s self) -> KeFormatter<'s, Storage>
Constructs a new formatter for the format.
Trait Implementations§
Source§impl<'s, Storage: IKeFormatStorage<'s>> Debug for KeFormat<'s, Storage>
impl<'s, Storage: IKeFormatStorage<'s>> Debug for KeFormat<'s, Storage>
Source§impl<'s, Storage: IKeFormatStorage<'s> + 's> Display for KeFormat<'s, Storage>
impl<'s, Storage: IKeFormatStorage<'s> + 's> Display for KeFormat<'s, Storage>
Source§impl<'s, S1: IKeFormatStorage<'s> + 's, S2: IKeFormatStorage<'s> + 's> PartialEq<KeFormat<'s, S2>> for KeFormat<'s, S1>
impl<'s, S1: IKeFormatStorage<'s> + 's, S2: IKeFormatStorage<'s> + 's> PartialEq<KeFormat<'s, S2>> for KeFormat<'s, S1>
Source§impl<'s, Storage: IKeFormatStorage<'s> + 's> TryFrom<&KeFormat<'s, Storage>> for OwnedKeyExpr
impl<'s, Storage: IKeFormatStorage<'s> + 's> TryFrom<&KeFormat<'s, Storage>> for OwnedKeyExpr
impl<'s, Storage: Copy + IKeFormatStorage<'s> + 's> Copy for KeFormat<'s, Storage>
Auto Trait Implementations§
impl<'s, Storage> Freeze for KeFormat<'s, Storage>where
Storage: Freeze,
impl<'s, Storage> RefUnwindSafe for KeFormat<'s, Storage>where
Storage: RefUnwindSafe,
impl<'s, Storage> Send for KeFormat<'s, Storage>where
Storage: Send,
impl<'s, Storage> Sync for KeFormat<'s, Storage>where
Storage: Sync,
impl<'s, Storage> Unpin for KeFormat<'s, Storage>where
Storage: Unpin,
impl<'s, Storage> UnwindSafe for KeFormat<'s, Storage>where
Storage: UnwindSafe,
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> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more