Skip to main content

MatchDispatch

Struct MatchDispatch 

Source
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(()),
        }
    })
    .await

Implementations§

Source§

impl MatchDispatch

Source

pub fn new(message: Dispatch) -> Self

Create a new pattern matcher for the given message.

Source

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.

Source

pub async fn if_request<Req: JsonRpcRequest, H>( self, op: impl AsyncFnOnce(Req, Responder<Req::Response>) -> Result<H, Error>, ) -> Self
where H: IntoHandled<(Req, Responder<Req::Response>)>,

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.

Source

pub async fn if_notification<N: JsonRpcNotification, H>( self, op: impl AsyncFnOnce(N) -> Result<H, Error>, ) -> Self
where 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.

Source

pub async fn if_message<R: JsonRpcRequest, N: JsonRpcNotification, H>( self, op: impl AsyncFnOnce(Dispatch<R, N>) -> Result<H, Error>, ) -> Self
where 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.

Source

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.

Source

pub async fn if_ok_response_to<Req: JsonRpcRequest, H>( self, op: impl AsyncFnOnce(Req::Response, ResponseRouter<Req::Response>) -> Result<H, Error>, ) -> Self
where H: IntoHandled<(Req::Response, ResponseRouter<Req::Response>)>,

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.

Source

pub fn done(self) -> Result<Handled<Dispatch>, Error>

Complete matching, returning Handled::No if no match was found.

Source

pub async fn otherwise( self, op: impl AsyncFnOnce(Dispatch) -> Result<(), Error>, ) -> Result<(), Error>

Handle messages that didn’t match any previous handler.

Source

pub fn otherwise_ignore(self) -> Result<(), Error>

Handle messages that didn’t match any previous handler.

Trait Implementations§

Source§

impl Debug for MatchDispatch

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

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

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoMaybeUndefined<T> for T

Source§

impl<T> IntoOption<T> for T

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more