pub struct MatchDispatch { /* private fields */ }Expand description
Role-agnostic helper for pattern-matching on untyped JSON-RPC messages.
Use this when you already have an unwrapped message and just need to parse it,
such as inside a MatchDispatchFrom callback or when processing messages
that don’t need peer transforms.
For connection handlers where you need proper peer-aware transforms,
use MatchDispatchFrom instead.
§Example
MatchDispatch::new(message)
.if_request(|req: InitializeRequest, responder: agent_client_protocol::Responder<InitializeResponse>| async move {
let response = InitializeResponse::new(req.protocol_version)
.agent_capabilities(AgentCapabilities::new());
responder.respond(response)
})
.await
.otherwise(|message| async move {
match message {
Dispatch::Request(_, responder) => {
responder.respond_with_error(agent_client_protocol::util::internal_error("unknown method"))
}
Dispatch::Notification(_) | Dispatch::Response(_, _) => Ok(()),
}
})
.awaitImplementations§
Source§impl MatchDispatch
impl MatchDispatch
Sourcepub fn from_handled(state: Result<Handled<Dispatch>, Error>) -> Self
pub fn from_handled(state: Result<Handled<Dispatch>, Error>) -> Self
Create a pattern matcher from an existing Handled state.
This is useful when composing with MatchDispatchFrom which applies
peer transforms before delegating to MatchDispatch for parsing.
Sourcepub async fn if_request<Req: JsonRpcRequest, H>(
self,
op: impl AsyncFnOnce(Req, Responder<Req::Response>) -> Result<H, Error>,
) -> Self
pub async fn if_request<Req: JsonRpcRequest, H>( self, op: impl AsyncFnOnce(Req, Responder<Req::Response>) -> Result<H, Error>, ) -> Self
Try to handle the message as a request of type Req.
If the message can be parsed as Req, the handler op is called with the parsed
request and a typed request context. If parsing fails or the message was already
handled by a previous call, this has no effect.
Sourcepub async fn if_notification<N: JsonRpcNotification, H>(
self,
op: impl AsyncFnOnce(N) -> Result<H, Error>,
) -> Selfwhere
H: IntoHandled<N>,
pub async fn if_notification<N: JsonRpcNotification, H>(
self,
op: impl AsyncFnOnce(N) -> Result<H, Error>,
) -> Selfwhere
H: IntoHandled<N>,
Try to handle the message as a notification of type N.
If the message can be parsed as N, the handler op is called with the parsed
notification. If parsing fails or the message was already handled, this has no effect.
Sourcepub async fn if_message<R: JsonRpcRequest, N: JsonRpcNotification, H>(
self,
op: impl AsyncFnOnce(Dispatch<R, N>) -> Result<H, Error>,
) -> Selfwhere
H: IntoHandled<Dispatch<R, N>>,
pub async fn if_message<R: JsonRpcRequest, N: JsonRpcNotification, H>(
self,
op: impl AsyncFnOnce(Dispatch<R, N>) -> Result<H, Error>,
) -> Selfwhere
H: IntoHandled<Dispatch<R, N>>,
Try to handle the message as a typed Dispatch<R, N>.
This attempts to parse the message as either request type R or notification type N,
providing a typed Dispatch to the handler if successful.
Sourcepub async fn if_response_to<Req: JsonRpcRequest, H>(
self,
op: impl AsyncFnOnce(Result<Req::Response, Error>, ResponseRouter<Req::Response>) -> Result<H, Error>,
) -> Self
pub async fn if_response_to<Req: JsonRpcRequest, H>( self, op: impl AsyncFnOnce(Result<Req::Response, Error>, ResponseRouter<Req::Response>) -> Result<H, Error>, ) -> Self
Try to handle the message as a response to a request of type Req.
If the message is a Response variant and the method matches Req, the handler
is called with the result (which may be Ok or Err) and a typed response context.
Use this when you need to handle both success and error responses.
For handling only successful responses, see if_ok_response_to.
Sourcepub async fn if_ok_response_to<Req: JsonRpcRequest, H>(
self,
op: impl AsyncFnOnce(Req::Response, ResponseRouter<Req::Response>) -> Result<H, Error>,
) -> Self
pub async fn if_ok_response_to<Req: JsonRpcRequest, H>( self, op: impl AsyncFnOnce(Req::Response, ResponseRouter<Req::Response>) -> Result<H, Error>, ) -> Self
Try to handle the message as a successful response to a request of type Req.
If the message is a Response variant with an Ok result and the method matches Req,
the handler is called with the parsed response and a typed response context.
Error responses are passed through without calling the handler.
This is a convenience wrapper around if_response_to for the
common case where you only care about successful responses.
Sourcepub fn done(self) -> Result<Handled<Dispatch>, Error>
pub fn done(self) -> Result<Handled<Dispatch>, Error>
Complete matching, returning Handled::No if no match was found.
Sourcepub async fn otherwise(
self,
op: impl AsyncFnOnce(Dispatch) -> Result<(), Error>,
) -> Result<(), Error>
pub async fn otherwise( self, op: impl AsyncFnOnce(Dispatch) -> Result<(), Error>, ) -> Result<(), Error>
Handle messages that didn’t match any previous handler.
Sourcepub fn otherwise_ignore(self) -> Result<(), Error>
pub fn otherwise_ignore(self) -> Result<(), Error>
Handle messages that didn’t match any previous handler.