Skip to main content

Context

Struct Context 

Source
pub struct Context<'a, D> { /* private fields */ }
Expand description

The context passed to every handler.

Generic over D (the service dependency type) so handlers can access the repository, read-model store, or custom dependencies the service is configured with.

§Example

pub async fn handle(
    ctx: &Context<'_, Repo>,
) -> Result<Value, HandlerError> {
    let user_id = ctx.user_id()?;
    let input = ctx.input::<CreateOrderInput>()?;
    let repo = ctx.repo();
    // ... commit via repo.commit(&mut agg).await? ...
}

Implementations§

Source§

impl<'a, D> Context<'a, D>

Source

pub fn input<T: DeserializeOwned>(&self) -> Result<T, HandlerError>

Deserialize the input payload into a typed struct.

Source

pub fn raw_input(&self) -> &Value

Get the raw JSON input.

Source

pub fn command_name(&self) -> &str

Get the command name.

Source

pub fn message_name(&self) -> &str

Get the message name.

Source

pub fn message(&self) -> &Message

Get the full message, including id, raw payload bytes, and metadata.

Source

pub fn session(&self) -> &Session

Get the session.

Source

pub fn user_id(&self) -> Result<&str, HandlerError>

Get the user ID from the session. Returns Unauthorized if not present.

Source

pub fn role(&self) -> Option<&str>

Get the user role from the session.

Source

pub fn dependencies(&self) -> &D

Get a reference to the service dependencies.

Source

pub fn repo(&self) -> &D::Repo
where D: HasRepo,

Get the aggregate repository for handlers whose dependencies expose one.

Source

pub fn read_model_store(&self) -> &D::ReadModelStore

Get the read-model store for handlers whose dependencies expose one.

Source

pub fn has_field(&self, field: &str) -> bool

Check if the raw input contains a field.

Source

pub fn has_fields(&self, fields: &[&str]) -> bool

Check if the raw input contains all specified fields.

Auto Trait Implementations§

§

impl<'a, D> Freeze for Context<'a, D>

§

impl<'a, D> RefUnwindSafe for Context<'a, D>
where D: RefUnwindSafe,

§

impl<'a, D> Send for Context<'a, D>
where D: Sync,

§

impl<'a, D> Sync for Context<'a, D>
where D: Sync,

§

impl<'a, D> Unpin for Context<'a, D>

§

impl<'a, D> UnsafeUnpin for Context<'a, D>

§

impl<'a, D> UnwindSafe for Context<'a, D>
where D: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> AggregateBuilder for T

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

Source§

fn queued(self) -> QueuedRepository<Self, InMemoryLockManager>

Wrap with the default async lock manager. Pair with .aggregate::<T>() for per-aggregate serialization over the async repository surface.
Source§

fn queued_with<L: LockManager>( self, lock_manager: L, ) -> QueuedRepository<Self, L>

Wrap with a custom async lock manager.
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.