pub fn parse_incoming(text: &str, service: BcsWsChannel) -> IncomingMessageExpand description
Parses one text frame of a channel into an IncomingMessage.
The function never fails and never panics. The order of tries is:
- A frame with
"type": "error"becomesIncomingMessage::Error. - A frame with a
responseTypebecomesIncomingMessage::MarketData. - Otherwise the frame is read as the data type of
service: limits, portfolio, order message (execution and events channels), transaction message, margin indicators, or market data. - A frame that fits none of these comes back as
IncomingMessage::Unknownwith the raw text. A broken frame on the transaction channel is also logged as a warning, without its text (it holds account data).
A session calls this for every text frame. Call it yourself to test your handler on a sample frame.
§Examples
use brokapi_bcs_protocol::models::enums::OrderStatus;
use brokapi_bcs_protocol::models::messages::IncomingMessage;
use brokapi_bcs_protocol::ws::client::BcsWsChannel;
use brokapi_bcs_protocol::ws::parse_incoming;
// An order event: the order is filled.
let frame = r#"{
"originalClientOrderId": "550e8400-e29b-41d4-a716-446655440000",
"clientOrderId": "",
"data": {"orderStatus": "2", "executionType": "2", "ticker": "SBER", "classCode": "TQBR",
"side": "1", "orderType": "2", "orderQuantity": 1, "executedQuantity": 1,
"lastQuantity": 1, "remainedQuantity": 0, "price": 300.0}
}"#;
match parse_incoming(frame, BcsWsChannel::OrderEvents) {
IncomingMessage::OrderExecution(m) => {
assert_eq!(m.data.unwrap().order_status, OrderStatus::Filled);
}
other => panic!("unexpected: {other:?}"),
}
// Text that is not a frame of the broker is kept, not dropped.
assert!(matches!(parse_incoming("hello", BcsWsChannel::Limits), IncomingMessage::Unknown(_)));