barter_data/transformer/
mod.rs

1use crate::{
2    error::DataError,
3    event::MarketEvent,
4    subscription::{Map, SubscriptionKind},
5};
6use async_trait::async_trait;
7use barter_integration::{Transformer, protocol::websocket::WsMessage};
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.
16#[async_trait]
17pub trait ExchangeTransformer<Exchange, InstrumentKey, Kind>
18where
19    Self: Transformer<Output = MarketEvent<InstrumentKey, Kind::Event>, Error = DataError> + Sized,
20    Kind: SubscriptionKind,
21{
22    /// Initialise a new [`Self`], also fetching any market data snapshots required for the
23    /// associated Exchange and SubscriptionKind market stream to function.
24    ///
25    /// The [`mpsc::UnboundedSender`] can be used by [`Self`] to send messages back to the exchange.
26    async fn init(
27        instrument_map: Map<InstrumentKey>,
28        initial_snapshots: &[MarketEvent<InstrumentKey, Kind::Event>],
29        ws_sink_tx: mpsc::UnboundedSender<WsMessage>,
30    ) -> Result<Self, DataError>;
31}