Skip to main content

rustrade_data/transformer/
mod.rs

1use crate::{
2    error::DataError,
3    event::MarketEvent,
4    subscription::{Map, SubscriptionKind},
5};
6use rustrade_integration::{Transformer, protocol::websocket::WsMessage};
7use std::future::Future;
8use tokio::sync::mpsc;
9
10/// Generic stateless [`ExchangeTransformer`] often used for transforming
11/// [`PublicTrades`](crate::subscription::trade::PublicTrades) streams.
12pub mod stateless;
13
14/// Defines how to construct a [`Transformer`] used by [`MarketStream`](super::MarketStream)s to
15/// translate exchange specific types to normalised Barter types.
16pub trait ExchangeTransformer<Exchange, InstrumentKey, Kind>
17where
18    Self: Transformer<Output = MarketEvent<InstrumentKey, Kind::Event>, Error = DataError> + Sized,
19    Kind: SubscriptionKind,
20{
21    /// Initialise a new [`Self`], also fetching any market data snapshots required for the
22    /// associated Exchange and SubscriptionKind market stream to function.
23    ///
24    /// The [`mpsc::UnboundedSender`] can be used by [`Self`] to send messages back to the exchange.
25    fn init(
26        instrument_map: Map<InstrumentKey>,
27        initial_snapshots: &[MarketEvent<InstrumentKey, Kind::Event>],
28        ws_sink_tx: mpsc::UnboundedSender<WsMessage>,
29    ) -> impl Future<Output = Result<Self, DataError>> + Send
30    where
31        InstrumentKey: Sync,
32        Kind::Event: Sync;
33}