Skip to main content

TypeNotification

Struct TypeNotification 

Source
pub struct TypeNotification<R: Role> { /* private fields */ }
Expand description

Builder for pattern-matching on untyped JSON-RPC notifications.

Similar to MatchDispatch but specifically for notifications (fire-and-forget messages with no response).

§Pattern

The typical pattern is:

  1. Create a TypeNotification from an untyped message
  2. Chain .handle_if() calls for each type you want to try
  3. End with .otherwise() for messages that don’t match any type

§Example

TypeNotification::new(message, cx)
    .handle_if(|notif: SessionNotification| async move {
        // Handle session notifications
        println!("Session update: {:?}", notif);
        Ok(())
    })
    .await
    .otherwise(|untyped: UntypedMessage| async move {
        // Fallback for unrecognized notifications
        println!("Unknown notification: {}", untyped.method);
        Ok(())
    })
    .await

Since notifications don’t expect responses, handlers only receive the parsed notification (not a request context).

Implementations§

Source§

impl<R: Role> TypeNotification<R>

Source

pub fn new(request: UntypedMessage, cx: &ConnectionTo<R>) -> Self

Create a new pattern matcher for the given untyped notification message.

Source

pub async fn handle_if<N: JsonRpcNotification>( self, op: impl AsyncFnOnce(N) -> Result<(), Error>, ) -> Self

Try to handle the message as 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 by a previous handle_if, this call has no effect.

Returns self to allow chaining multiple handle_if calls.

Source

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

Handle messages that didn’t match any previous handle_if call.

This is the fallback handler that receives the original untyped message if none of the typed handlers matched. You must call this method to complete the pattern matching chain and get the final result.

Trait Implementations§

Source§

impl<R: Debug + Role> Debug for TypeNotification<R>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<R> Freeze for TypeNotification<R>
where R: Freeze,

§

impl<R> !RefUnwindSafe for TypeNotification<R>

§

impl<R> Send for TypeNotification<R>

§

impl<R> Sync for TypeNotification<R>

§

impl<R> Unpin for TypeNotification<R>
where R: Unpin,

§

impl<R> UnsafeUnpin for TypeNotification<R>
where R: UnsafeUnpin,

§

impl<R> !UnwindSafe for TypeNotification<R>

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