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:
- Create a
TypeNotificationfrom an untyped message - Chain
.handle_if()calls for each type you want to try - 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(())
})
.awaitSince notifications don’t expect responses, handlers only receive the parsed notification (not a request context).
Implementations§
Source§impl<R: Role> TypeNotification<R>
impl<R: Role> TypeNotification<R>
Sourcepub fn new(request: UntypedMessage, cx: &ConnectionTo<R>) -> Self
pub fn new(request: UntypedMessage, cx: &ConnectionTo<R>) -> Self
Create a new pattern matcher for the given untyped notification message.
Sourcepub async fn handle_if<N: JsonRpcNotification>(
self,
op: impl AsyncFnOnce(N) -> Result<(), Error>,
) -> Self
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.
Sourcepub async fn otherwise(
self,
op: impl AsyncFnOnce(UntypedMessage) -> Result<(), Error>,
) -> Result<(), Error>
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.