Skip to main content

Responder

Struct Responder 

Source
pub struct Responder<T: JsonRpcResponse = Value> { /* private fields */ }
Expand description

The context to respond to an incoming request.

This context is provided to request handlers and serves a dual role:

  1. Respond to the request - Use respond or respond_with_result to send the response
  2. Send other messages - Use the ConnectionTo parameter passed to your handler, which provides send_request, send_notification, and spawn

§Example

connection.on_receive_request(async |req: ProcessRequest, responder, cx| {
    // Send a notification while processing
    cx.send_notification(StatusUpdate {
        message: "processing".into(),
    })?;

    // Do some work...
    let result = process(&req.data)?;

    // Respond to the request
    responder.respond(ProcessResponse { result })
}, agent_client_protocol::on_receive_request!())

§Event Loop Considerations

Like all handlers, request handlers run on the event loop. Use spawn for expensive operations to avoid blocking the connection.

See the Event Loop and Concurrency section for more details.

Implementations§

Source§

impl Responder<Value>

Source

pub fn cast<T: JsonRpcResponse>(self) -> Responder<T>

Cast this request context to a different response type.

The provided type T will be serialized to JSON before sending.

Source§

impl<T: JsonRpcResponse> Responder<T>

Source

pub fn method(&self) -> &str

Method of the incoming request

Source

pub fn id(&self) -> Value

ID of the incoming request/response as a JSON value

Source

pub fn erase_to_json(self) -> Responder<Value>

Convert to a Responder that expects a JSON value and which checks (dynamically) that the JSON value it receives can be converted to T.

Source

pub fn wrap_method(self, method: String) -> Responder<T>

Return a new Responder with a different method name.

Source

pub fn wrap_params<U: JsonRpcResponse>( self, wrap_fn: impl FnOnce(&str, Result<U, Error>) -> Result<T, Error> + Send + 'static, ) -> Responder<U>

Return a new Responder that expects a response of type U.

wrap_fn will be invoked with the method name and the result to transform type U into type T before sending.

Source

pub fn respond_with_result( self, response: Result<T, Error>, ) -> Result<(), Error>

Respond to the JSON-RPC request with either a value (Ok) or an error (Err).

Source

pub fn respond(self, response: T) -> Result<(), Error>

Respond to the JSON-RPC request with a value.

Source

pub fn respond_with_internal_error( self, message: impl ToString, ) -> Result<(), Error>

Respond to the JSON-RPC request with an internal error containing a message.

Source

pub fn respond_with_error(self, error: Error) -> Result<(), Error>

Respond to the JSON-RPC request with an error.

Trait Implementations§

Source§

impl<T: JsonRpcResponse> Debug for Responder<T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Responder<T>

§

impl<T = Value> !RefUnwindSafe for Responder<T>

§

impl<T> Send for Responder<T>

§

impl<T = Value> !Sync for Responder<T>

§

impl<T> Unpin for Responder<T>

§

impl<T> UnsafeUnpin for Responder<T>

§

impl<T = Value> !UnwindSafe for Responder<T>

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