Skip to main content

MessageBuilder

Struct MessageBuilder 

Source
pub struct MessageBuilder { /* private fields */ }
Expand description

Aggregate JSON-RPC message builder/parser.

Holds the three sub-components needed to drive a JSON-RPC session:

  • RequestBuilder — produces outbound requests and assigns monotonically-increasing ids; held mutably because it mutates that id counter on every call.
  • ResponseHandler — parses inbound replies keyed by id; fully stateless, so a shared reference is enough.
  • NotificationHandler — parses server-initiated pushes (subscription events, heartbeats); also stateless.

One instance per WebSocket connection is the intended lifetime: the id counter must not be shared across connections, and the handlers hold no connection-specific state.

Implementations§

Source§

impl MessageBuilder

Source

pub fn new() -> Self

Create a new aggregate message builder with a fresh id counter and stateless response/notification parsers.

Source

pub fn request_builder(&mut self) -> &mut RequestBuilder

Borrow the request builder mutably.

Mutability is required because every outbound request advances the builder’s id counter. Prefer calling this from a single task: the MessageBuilder itself is not Sync-protected.

Source

pub fn response_handler(&self) -> &ResponseHandler

Borrow the response handler.

Returned as an immutable reference because the handler is a pure JSON-to-DTO parser with no internal state.

Source

pub fn notification_handler(&self) -> &NotificationHandler

Borrow the notification handler.

Returned as an immutable reference because the handler is a pure JSON-to-DTO parser with no internal state.

Source

pub fn parse_message(&self, data: &str) -> Result<MessageType, Error>

Classify and parse a raw JSON-RPC frame.

Attempts the response path first (JSON-RPC 2.0 responses always carry an id), then the notification path (notifications never have id). Outbound-only JsonRpcRequests — the third MessageType variant — are produced by RequestBuilder, not by this parser, so receiving a “request-shaped” frame from the server is treated as invalid data.

§Errors

Returns a serde_json::Error of kind std::io::ErrorKind::InvalidData when data parses as neither a response nor a notification — typically because it is malformed JSON, lacks both an id and a method, or looks like an outbound request rather than an inbound frame.

Trait Implementations§

Source§

impl Debug for MessageBuilder

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for MessageBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. 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> Same for T

Source§

type Output = T

Should always be Self
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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