pub enum Dispatch<Req: JsonRpcRequest = UntypedMessage, Notif: JsonRpcMessage = UntypedMessage> {
Request(Req, Responder<Req::Response>),
Notification(Notif),
Response(Result<Req::Response, Error>, ResponseRouter<Req::Response>),
}Expand description
An enum capturing an in-flight request or notification. In the case of a request, also includes the context used to respond to the request.
Type parameters allow specifying the concrete request and notification types.
By default, both are UntypedMessage for dynamic dispatch.
The request context’s response type matches the request’s response type.
Variants§
Request(Req, Responder<Req::Response>)
Incoming request and the context where the response should be sent.
Notification(Notif)
Incoming notification.
Response(Result<Req::Response, Error>, ResponseRouter<Req::Response>)
Incoming response to a request we sent.
The first field is the response result (success or error from the remote). The second field is the context for forwarding the response to its destination (typically a waiting oneshot channel).
Implementations§
Source§impl<Req: JsonRpcRequest, Notif: JsonRpcMessage> Dispatch<Req, Notif>
impl<Req: JsonRpcRequest, Notif: JsonRpcMessage> Dispatch<Req, Notif>
Sourcepub fn map<Req1, Notif1>(
self,
map_request: impl FnOnce(Req, Responder<Req::Response>) -> (Req1, Responder<Req1::Response>),
map_notification: impl FnOnce(Notif) -> Notif1,
) -> Dispatch<Req1, Notif1>
pub fn map<Req1, Notif1>( self, map_request: impl FnOnce(Req, Responder<Req::Response>) -> (Req1, Responder<Req1::Response>), map_notification: impl FnOnce(Notif) -> Notif1, ) -> Dispatch<Req1, Notif1>
Map the request and notification types to new types.
Note: Response variants are passed through unchanged since they don’t contain a parseable message payload.
Sourcepub fn respond_with_error<R: Role>(
self,
error: Error,
cx: ConnectionTo<R>,
) -> Result<(), Error>
pub fn respond_with_error<R: Role>( self, error: Error, cx: ConnectionTo<R>, ) -> Result<(), Error>
Respond to the message with an error.
If this message is a request, this error becomes the reply to the request.
If this message is a notification, the error is sent as a notification.
If this message is a response, the error is forwarded to the waiting handler.
Sourcepub fn erase_to_json(self) -> Result<Dispatch, Error>
pub fn erase_to_json(self) -> Result<Dispatch, Error>
Convert to a Responder that expects a JSON value
and which checks (dynamically) that the JSON value it receives
can be converted to T.
Note: Response variants cannot be erased since their payload is already parsed. This returns an error for Response variants.
Sourcepub fn to_untyped_message(&self) -> Result<UntypedMessage, Error>
pub fn to_untyped_message(&self) -> Result<UntypedMessage, Error>
Convert the message in self to an untyped message.
Note: Response variants don’t have an untyped message representation. This returns an error for Response variants.
Sourcepub fn into_untyped_dispatch(self) -> Result<Dispatch, Error>
pub fn into_untyped_dispatch(self) -> Result<Dispatch, Error>
Convert self to an untyped message context.
Note: Response variants cannot be converted. This returns an error for Response variants.
Source§impl Dispatch
impl Dispatch
Sourcepub fn has_field(&self, field_name: &str) -> bool
pub fn has_field(&self, field_name: &str) -> bool
True if this message has a field with the given name.
Returns false for Response variants.
Sourcepub fn into_notification<N: JsonRpcNotification>(
self,
) -> Result<Result<N, Dispatch>, Error>
pub fn into_notification<N: JsonRpcNotification>( self, ) -> Result<Result<N, Dispatch>, Error>
Try to parse this as a notification of the given type.
§Returns
Ok(Ok(typed))if this is a request/notification of the given typesOk(Err(self))if notErrif has the correct method for the given types but parsing fails
Sourcepub fn into_request<Req: JsonRpcRequest>(
self,
) -> Result<Result<(Req, Responder<Req::Response>), Dispatch>, Error>
pub fn into_request<Req: JsonRpcRequest>( self, ) -> Result<Result<(Req, Responder<Req::Response>), Dispatch>, Error>
Try to parse this as a request of the given type.
§Returns
Ok(Ok(typed))if this is a request/notification of the given typesOk(Err(self))if notErrif has the correct method for the given types but parsing fails