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::Error::method_not_found())
}
Dispatch::Notification(_) | Dispatch::Response(_, _) => Ok(()),
}
})
.awaitImplementations§
Source§impl MatchDispatch
impl MatchDispatch
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 is a request for one of Req’s methods, it is parsed and
op is called with the request and a typed responder. If the method
matches but the params do not parse, matching stops with the parse
error. Other messages, and messages already handled by a previous call,
pass through unchanged.
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 is a notification for one of N’s methods, it is parsed
and op is called with the notification. If the method matches but the
params do not parse, matching stops with the parse error. Other
messages, and messages already handled by a previous call, pass through
unchanged.
Sourcepub async fn if_dispatch<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_dispatch<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>
Finish matching, answering an unmatched request with a method not
found error (-32601) and ignoring an unmatched notification or
response.