pub mod control;
pub(crate) mod fields;
pub mod read;
pub use control::{Refusal, codes};
pub use read::{BadFrame, decode};
use std::fmt;
use crate::client::{Sent, SentBatch};
use crate::earn::pools::PoolInfo;
use crate::live::account::{
AccountAll, AccountAllAssets, AccountAllOrders, AccountAllPositions, AccountAllTrades, AccountMarket,
AccountOrders, AccountTx, AvgEntryPrices, LivePoints, Notifications, PoolData, Rfq, UserStats,
};
use crate::live::market::{
BoMarketStats, BookUpdate, Candles, Height, MarkPriceCandles, MarketStats, SpotMarketStats, Ticker, Trades,
UpDownCandles,
};
use crate::utils::{AccountIndex, MarketId};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Frame {
Connected { session_id: String },
Pong,
Ping,
Unsubscribed { channel: String },
Error(Refusal),
Sent { id: String, sent: Sent },
SentBatch { id: String, sent: SentBatch },
Snapshot(Data),
Update(Data),
Gap(Gap),
Other { kind: String, text: String },
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Data {
OrderBook(BookUpdate),
Ticker(Ticker),
MarketStats(Vec<MarketStats>),
SpotMarketStats(Vec<SpotMarketStats>),
BoMarketStats(Vec<BoMarketStats>),
Trades(Trades),
Candles(Candles),
MarkPriceCandles(MarkPriceCandles),
UpDownCandles(UpDownCandles),
Height(Height),
AccountAll(Box<AccountAll>),
AccountMarket(Box<AccountMarket>),
UserStats(Box<UserStats>),
AccountTx(AccountTx),
AccountAllOrders(AccountAllOrders),
AccountOrders(AccountOrders),
AccountAllTrades(AccountAllTrades),
AccountAllPositions(AccountAllPositions),
AccountAllAssets(AccountAllAssets),
AvgEntryPrices(AvgEntryPrices),
PoolData(Box<PoolData>),
PoolInfo {
account: AccountIndex,
info: Box<PoolInfo>,
},
Notifications(Notifications),
Rfqs(Vec<Rfq>),
LivePoints(LivePoints),
Other {
channel: String,
text: String,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Gap {
pub market: MarketId,
pub expected: u64,
pub got: u64,
}
impl fmt::Display for Gap {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"market {}: an order book update began at nonce {} where the book was at {}; a frame was lost",
self.market, self.got, self.expected
)
}
}
impl std::error::Error for Gap {}
impl Frame {
#[must_use]
pub const fn data(&self) -> Option<&Data> {
match self {
Frame::Snapshot(data) | Frame::Update(data) => Some(data),
_ => None,
}
}
#[must_use]
pub const fn is_snapshot(&self) -> bool {
matches!(self, Frame::Snapshot(_))
}
#[must_use]
pub fn answers(&self) -> Option<&str> {
match self {
Frame::Sent { id, .. } | Frame::SentBatch { id, .. } => Some(id),
Frame::Error(refusal) => refusal.id.as_deref(),
_ => None,
}
}
}
#[cfg(test)]
#[path = "tests/frame.rs"]
mod tests;