Skip to main content

rustrade_data/exchange/massive/
channel.rs

1//! The Massive WebSocket channel each subscription kind reads.
2
3use super::{
4    Massive,
5    market::{MassiveServer, MassiveTradeServer},
6};
7use crate::{
8    Identifier,
9    subscription::{
10        Subscription,
11        book::OrderBooksL1,
12        candle::{CandleInterval, Candles},
13        quote::Quotes,
14        trade::PublicTrades,
15    },
16};
17
18/// A Massive WebSocket channel, spelled as the event its messages carry: `XT`, `Q`, `CAS`.
19///
20/// A subscription is the channel and a market joined by a dot, `XT.BTC-USD`, and every message it
21/// delivers carries the channel as its `ev`.
22#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
23pub enum MassiveChannel {
24    /// A channel the cluster publishes.
25    Event(&'static str),
26    /// Candles at an interval Massive does not aggregate at. It has aggregates per second and per
27    /// minute only, so any other interval is refused before anything is sent.
28    UnsupportedInterval(CandleInterval),
29}
30
31impl AsRef<str> for MassiveChannel {
32    /// The event, or nothing for an unsupported interval, which is refused before it is spelled.
33    fn as_ref(&self) -> &str {
34        match self {
35            Self::Event(event) => event,
36            Self::UnsupportedInterval(_) => "",
37        }
38    }
39}
40
41impl<Server, Instrument> Identifier<MassiveChannel>
42    for Subscription<Massive<Server>, Instrument, PublicTrades>
43where
44    Server: MassiveTradeServer,
45{
46    fn id(&self) -> MassiveChannel {
47        MassiveChannel::Event(Server::TRADES)
48    }
49}
50
51impl<Server, Instrument> Identifier<MassiveChannel>
52    for Subscription<Massive<Server>, Instrument, Quotes>
53where
54    Server: MassiveServer,
55{
56    fn id(&self) -> MassiveChannel {
57        MassiveChannel::Event(Server::QUOTES)
58    }
59}
60
61/// The quote channel, read as a top of book.
62impl<Server, Instrument> Identifier<MassiveChannel>
63    for Subscription<Massive<Server>, Instrument, OrderBooksL1>
64where
65    Server: MassiveServer,
66{
67    fn id(&self) -> MassiveChannel {
68        MassiveChannel::Event(Server::QUOTES)
69    }
70}
71
72impl<Server, Instrument> Identifier<MassiveChannel>
73    for Subscription<Massive<Server>, Instrument, Candles>
74where
75    Server: MassiveServer,
76{
77    fn id(&self) -> MassiveChannel {
78        match self.kind.interval {
79            CandleInterval::Sec1 => MassiveChannel::Event(Server::SECOND_AGGREGATES),
80            CandleInterval::Min1 => MassiveChannel::Event(Server::MINUTE_AGGREGATES),
81            other => MassiveChannel::UnsupportedInterval(other),
82        }
83    }
84}