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
impl MessageBuilder
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new aggregate message builder with a fresh id counter and stateless response/notification parsers.
Sourcepub fn request_builder(&mut self) -> &mut RequestBuilder
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.
Sourcepub fn response_handler(&self) -> &ResponseHandler
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.
Sourcepub fn notification_handler(&self) -> &NotificationHandler
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.
Sourcepub fn parse_message(&self, data: &str) -> Result<MessageType, Error>
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.