pub struct Dispatcher<R> { /* private fields */ }Expand description
Routes a TrustTask<Value> to the handler registered for its Type URI.
Construct with Dispatcher::new, register handlers via Dispatcher::on,
then call Dispatcher::dispatch for each inbound document. R is the
handler’s return type — (), Result<TrustTask<Resp>, ErrorResponse>, or
anything else; the dispatcher only adds RejectReason for the routing-
and deserialization-time failures.
Implementations§
Source§impl<R> Dispatcher<R>
impl<R> Dispatcher<R>
Sourcepub fn new() -> Dispatcher<R>
pub fn new() -> Dispatcher<R>
Build an empty Dispatcher. Add handlers with Self::on.
Sourcepub fn on<P, F>(self, handler: F) -> Dispatcher<R>
pub fn on<P, F>(self, handler: F) -> Dispatcher<R>
Register handler for the Type URI declared by P.
On dispatch, the dispatcher looks up the inbound document’s type
against the registered URIs in canonical form (TypeUri::for_routing),
so a producer emitting either the bare URI or the #request-fragmented
form per SPEC.md §4.4.1 item 1 routes to the same handler.
#response-fragmented URIs are kept distinct and route to whatever
(if anything) is registered for the response variant.
Registering the same canonical Type URI twice replaces the earlier handler.
Sourcepub fn dispatch(&self, doc: TrustTask<Value>) -> Result<R, RejectReason>
pub fn dispatch(&self, doc: TrustTask<Value>) -> Result<R, RejectReason>
Route doc to the handler registered for its type URI.
Returns:
Ok(R)— handler invoked successfully.Err(RejectReason::UnsupportedVersion)— the slug is registered but not at this Type URI’sMAJOR.MINOR(SPEC §5.2 / §8.3).Err(RejectReason::UnsupportedType)— the slug is not registered at all; the caller typically converts this into atrust-task-errorresponse viaTrustTask::reject_with.Err(RejectReason::MalformedRequest)— the URI matched but the payload failed to deserialize againstP.
Sourcepub fn registered_uris(&self) -> Vec<&str>
pub fn registered_uris(&self) -> Vec<&str>
The Type URIs this dispatcher currently routes for, in canonical
form and sorted for stable output. Handy for unsupported_type
error responses that list what the consumer does implement.
Sourcepub fn dispatch_or_reject(
&self,
doc: TrustTask<Value>,
error_id: impl Into<String>,
) -> Result<R, TrustTask<ErrorPayload>>
pub fn dispatch_or_reject( &self, doc: TrustTask<Value>, error_id: impl Into<String>, ) -> Result<R, TrustTask<ErrorPayload>>
Route doc to the registered handler, returning either the
handler’s success value or an ErrorResponse already routed
per SPEC.md §8.1.
Equivalent to calling Self::dispatch and hand-converting any
RejectReason via TrustTask::reject_with — most consumer
code today writes that conversion as a six-line match, so this
is the convenience form.
error_id supplies the id for the error response; the framework
places no constraints on its form beyond uniqueness (SPEC.md §4.3).
UUIDv4 is the recommended default.
The dispatcher only emits RejectReason::UnsupportedType (no
handler registered for this Type URI) and
RejectReason::MalformedRequest (payload failed to deserialize
into the registered P). Both are routed to the original producer
per SPEC.md §8.1 — neither carries the identity_mismatch
transport-routing exception, so the safe default in
TrustTask::reject_with applies.
ErrorResponse is intentionally large (it carries a full
trust-task-error document including a typed payload and
extra-member map). Boxing it in the Err variant would just push
the allocation onto every caller; the convenience this method
provides assumes the caller wants the value back unboxed.
Trait Implementations§
Source§impl<R> Default for Dispatcher<R>
impl<R> Default for Dispatcher<R>
Source§fn default() -> Dispatcher<R>
fn default() -> Dispatcher<R>
Auto Trait Implementations§
impl<R> !RefUnwindSafe for Dispatcher<R>
impl<R> !UnwindSafe for Dispatcher<R>
impl<R> Freeze for Dispatcher<R>
impl<R> Send for Dispatcher<R>
impl<R> Sync for Dispatcher<R>
impl<R> Unpin for Dispatcher<R>
impl<R> UnsafeUnpin for Dispatcher<R>
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> BorrowUnordered for T
impl<T> BorrowUnordered for T
fn as_unordered(&self) -> &Unordered<T>
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Clear for Twhere
T: InitializableFromZeroed + ?Sized,
impl<T> Clear for Twhere
T: InitializableFromZeroed + ?Sized,
Source§impl<T> InitializableFromZeroed for Twhere
T: Default,
impl<T> InitializableFromZeroed for Twhere
T: Default,
Source§unsafe fn initialize(place: *mut T)
unsafe fn initialize(place: *mut T)
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T, U, C> IntoWithContext<U, C> for Twhere
U: FromWithContext<T, C>,
impl<T, U, C> IntoWithContext<U, C> for Twhere
U: FromWithContext<T, C>,
Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<T> ResourceProvider<()> for T
impl<T> ResourceProvider<()> for T
Source§fn get_resource(&self) -> &()
fn get_resource(&self) -> &()
T.