JrRequestCx

Struct JrRequestCx 

Source
pub struct JrRequestCx<T: JrResponsePayload = 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 connection_cx to access the underlying JrConnectionCx, giving access to send_request, send_notification, and spawn

§Example

connection.on_receive_request(async |req: ProcessRequest, request_cx, 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
    request_cx.respond(ProcessResponse { result })
})

§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 JrRequestCx<Value>

Source

pub fn cast<T: JrResponsePayload>(self) -> JrRequestCx<T>

Cast this request context to a different response type

Source§

impl<T: JrResponsePayload> JrRequestCx<T>

Source

pub fn method(&self) -> &str

Method of the incoming request

Source

pub fn id(&self) -> Value

ID of the incoming request as a JSON value

Source

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

Convert to a JrRequestCx 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) -> JrRequestCx<T>

Return a new JrResponse that expects a response of type U and serializes it.

Source

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

Return a new JrResponse that expects a response of type U and serializes it.

wrap_fn will be invoked with the method name and the result of the wrapped function.

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: JrResponsePayload> Debug for JrRequestCx<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 JrRequestCx<T>

§

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

§

impl<T> Send for JrRequestCx<T>

§

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

§

impl<T> Unpin for JrRequestCx<T>

§

impl<T = Value> !UnwindSafe for JrRequestCx<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, 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