Skip to main content

SubjectScheme

Struct SubjectScheme 

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

How a subject is composed: literal segments and placeholders in braces, as in /tenant/{tenant}/workshop/{workshop}.

A scheme works in both directions. build composes a subject from values, and match_subject takes one apart again, or value takes a single value out of it, which a projection needs to recover the ID of an aggregate that the events themselves do not carry:

use architecturekit::SubjectScheme;

let workshops = SubjectScheme::new("/tenant/{tenant}/workshop/{workshop}");

let subject = workshops.build(&["acme", "event-sourcing"]);
assert_eq!(subject.as_str(), "/tenant/acme/workshop/event-sourcing");

assert_eq!(workshops.value(&subject, "workshop"), Some("event-sourcing"));

EventSourcingDB allows only ASCII letters and digits, underscores, and hyphens in a segment of a subject (see Subject), so this is what the literal segments of a pattern and the values of its placeholders may contain. The names of its placeholders follow the same rule, so that a typo, such as a brace too many, is not taken for a name.

Implementations§

Source§

impl SubjectScheme

Source

pub fn new(pattern: &str) -> Self

Parses a pattern.

§Panics

A malformed pattern, including one with a literal segment or a placeholder name that contains a character EventSourcingDB does not allow in a subject, is a programming error, so this panics while the scheme is being built.

Source

pub fn pattern(&self) -> &str

Returns the pattern the scheme was built from.

Source

pub fn root(&self) -> Subject

Returns the subject that every subject of the scheme lies under: the literal segments before the first placeholder, as /tenant for /tenant/{tenant}/workshop/{workshop}, or / if the pattern starts with a placeholder. Reading or observing recursively from it yields the events of all subjects the scheme describes, and possibly of others under the same root, which match_subject tells apart.

Source

pub fn placeholders(&self) -> &[String]

Returns the placeholder names, in the order build expects their values.

Source

pub fn build<V: AsRef<str>>(&self, values: &[V]) -> Subject

Composes a subject. The values fill the placeholders in the order they appear in the pattern.

§Panics

A wrong number of values, or one that check refuses, such as an empty one or one with a slash or a dot, is a programming error, so this panics. Values that come from outside, such as an ID in a request, may well be like that, so build from them with try_build instead.

Source

pub fn try_build<V: AsRef<str>>( &self, values: &[V], ) -> Result<Subject, SubjectValuesError>

Composes a subject from values that come from outside, such as an ID in a request, as build does, but refuses values that check refuses, rather than panicking:

use architecturekit::SubjectScheme;

let books = SubjectScheme::new("/books/{book_id}");

assert_eq!(books.try_build(&["42"])?.as_str(), "/books/42");
assert!(books.try_build(&["a.b"]).is_err());
§Errors

Fails with what check fails with.

Source

pub fn check<V: AsRef<str>>( &self, values: &[V], ) -> Result<(), SubjectValuesError>

Tells whether the values can compose a subject: one per placeholder, none of them empty, and each made only of the characters EventSourcingDB allows in a segment of a subject, which are A-Z, a-z, 0-9, underscores, and hyphens. It is what build insists on, as an error rather than a panic, for values that come from outside.

The error may reach the caller of an API, so it names the placeholder of a value, but neither the crate nor the pattern, as in: value for “id” must not be empty. The panic of build names both, since it is for the developer.

§Errors

Fails if the values can not compose a subject, as described above.

Source

pub fn match_subject<'scheme, 'subject, S>( &'scheme self, subject: &'subject S, ) -> Option<HashMap<&'scheme str, &'subject str>>
where S: AsRef<str> + ?Sized,

Takes a subject apart, into the value of every placeholder, by its name. It returns None if the subject does not follow the pattern, which is an ordinary case for a projection reading recursively across several schemes. A subject with a value that check refuses does not follow it either, so that this takes apart only what build composes.

It is called match_subject rather than match, which is a keyword in Rust. To take a single value out of a subject, use value, which panics for a name the pattern does not have. It takes a Subject as well as text.

Source

pub fn value<'subject, S>( &self, subject: &'subject S, placeholder: &str, ) -> Option<&'subject str>
where S: AsRef<str> + ?Sized,

Takes the value of a single placeholder out of a subject. It returns None if the subject does not follow the pattern, as match_subject does. To take several values out of the same subject, use match_subject.

§Panics

A placeholder that the pattern does not have, such as one with a typo, is a programming error, so this panics, whatever the subject is.

Trait Implementations§

Source§

impl Clone for SubjectScheme

Source§

fn clone(&self) -> Self

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 SubjectScheme

Source§

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

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

impl Eq for SubjectScheme

Source§

impl PartialEq for SubjectScheme

Source§

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

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

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for SubjectScheme

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
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> 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> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<T> IntoResult<T> for T

Source§

type Err = !

Source§

fn into_result(self) -> Result<T, <T as IntoResult<T>>::Err>

Source§

impl<L> LayerExt<L> for L

Source§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in Layered.
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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, 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