Skip to main content

brokapi_bcs_protocol/models/
messages.rs

1//! The result of parsing one WebSocket frame.
2
3use crate::ws::models::{
4    LimitsData, MarginalIndicatorsData, MarketDataMessage, OrderMessage, WsError, WsPortfolioData,
5};
6
7/// One parsed inbound WebSocket frame.
8///
9/// A session sends these into the channel you gave it. The variant is chosen by the content of the
10/// frame first (an error frame, a market data frame with `responseType`) and then by the channel the
11/// session is on. A frame that fits nothing arrives as [`IncomingMessage::Unknown`] with its raw
12/// text, so no data is lost silently.
13///
14/// # Examples
15///
16/// ```
17/// use brokapi_bcs_protocol::models::messages::IncomingMessage;
18/// use brokapi_bcs_protocol::ws::client::BcsWsChannel;
19/// use brokapi_bcs_protocol::ws::parse_incoming;
20///
21/// let frame = r#"{"responseType":"OrderBook","ticker":"SBER","classCode":"TQBR","bids":[],"asks":[]}"#;
22/// match parse_incoming(frame, BcsWsChannel::MarketData) {
23///     IncomingMessage::MarketData(_) => {}
24///     other => panic!("unexpected: {other:?}"),
25/// }
26/// ```
27// Boxing the large variant would change the public shape of the enum; the frames are short-lived.
28#[allow(clippy::large_enum_variant)]
29#[derive(Debug, Clone)]
30pub enum IncomingMessage {
31    /// An error frame of the broker (`type = "error"`).
32    Error(WsError),
33    /// A market data frame: an answer to a subscription, or data.
34    MarketData(MarketDataMessage),
35    /// A snapshot of limits (limits channel).
36    Limits(LimitsData),
37    /// A snapshot of the portfolio (portfolio channel).
38    Portfolio(WsPortfolioData),
39    /// An order message from the execution or events channel.
40    OrderExecution(OrderMessage),
41    /// An order message from the transaction channel (deprecated by the broker).
42    OrderTransaction(OrderMessage),
43    /// Margin indicators (margin channel).
44    MarginalIndicators(MarginalIndicatorsData),
45    /// A frame that could not be parsed into any of the above. It holds the raw text.
46    Unknown(String),
47    /// Not a frame of the broker: the session has just (re)connected, sent before it restores the
48    /// subscriptions. Only when the session was opened with `WsOptions::announce_connects`. The first
49    /// connect sends it too. A reader counts them: from the second on, the connection was lost and
50    /// everything that came before is not continued by what follows.
51    Reconnected,
52}