KeFormat

Struct KeFormat 

Source
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 for id.

§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>

Source

pub fn parse(&'s self, target: &'s keyexpr) -> ZResult<Parsed<'s, Storage>>

Parses target according to self. The returned Parsed object can be used to extract the values of the fields in self from target.

Parsing is greedy and done left-to-right. Please refer to KeFormat’s documentation for more details.

§Errors

If target does not intersect with self, an error is returned.

Source§

impl<'s> KeFormat<'s, Vec<Segment<'s>>>

Source

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.

Source

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>

Source

pub fn formatter(&'s self) -> KeFormatter<'s, Storage>

Constructs a new formatter for the format.

Trait Implementations§

Source§

impl<'s, Storage: Clone + IKeFormatStorage<'s> + 's> Clone for KeFormat<'s, Storage>

Source§

fn clone(&self) -> KeFormat<'s, Storage>

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<'s, Storage: IKeFormatStorage<'s>> Debug for KeFormat<'s, Storage>

Source§

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

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

impl<'s, Storage: IKeFormatStorage<'s> + 's> Display for KeFormat<'s, Storage>

Source§

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

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

impl<'s, Storage: Hash + IKeFormatStorage<'s> + 's> Hash for KeFormat<'s, Storage>

Source§

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

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<'s, S1: IKeFormatStorage<'s> + 's, S2: IKeFormatStorage<'s> + 's> PartialEq<KeFormat<'s, S2>> for KeFormat<'s, S1>

Source§

fn eq(&self, other: &KeFormat<'s, S2>) -> 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<'s, Storage: IKeFormatStorage<'s> + 's> TryFrom<&KeFormat<'s, Storage>> for OwnedKeyExpr

Source§

type Error = Box<dyn Error + Sync + Send>

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

fn try_from(value: &KeFormat<'s, Storage>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'s, Storage: IKeFormatStorage<'s> + 's> TryFrom<&KeFormat<'s, Storage>> for String

Source§

type Error = Error

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

fn try_from(value: &KeFormat<'s, Storage>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'s, Storage: IKeFormatStorage<'s> + 's> TryFrom<&'s String> for KeFormat<'s, Storage>

Source§

type Error = Box<dyn Error + Sync + Send>

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

fn try_from(value: &'s String) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'s, Storage: IKeFormatStorage<'s> + 's> TryFrom<&'s str> for KeFormat<'s, Storage>

Source§

type Error = Box<dyn Error + Sync + Send>

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

fn try_from(value: &'s str) -> Result<Self, Self::Error>

Performs the conversion.
Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AsNode<T> for T

Source§

fn as_node(&self) -> &T

Source§

impl<T> AsNodeMut<T> for T

Source§

fn as_node_mut(&mut self) -> &mut T

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> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

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

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
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 = 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.