use crate::streams::consumer::MarketStreamResult;
use crate::subscription::book::OrderBookEvent;
use crate::{
error::DataError,
subscription::{
book::OrderBookL1, candle::Candle, liquidation::Liquidation, trade::PublicTrade,
},
};
use barter_integration::model::{instrument::Instrument, Exchange};
use chrono::{DateTime, Utc};
use derive_more::From;
use serde::{Deserialize, Serialize};
#[derive(Debug)]
pub struct MarketIter<InstrumentKey, T>(pub Vec<Result<MarketEvent<InstrumentKey, T>, DataError>>);
impl<InstrumentKey, T> FromIterator<Result<MarketEvent<InstrumentKey, T>, DataError>>
for MarketIter<InstrumentKey, T>
{
fn from_iter<Iter>(iter: Iter) -> Self
where
Iter: IntoIterator<Item = Result<MarketEvent<InstrumentKey, T>, DataError>>,
{
Self(iter.into_iter().collect())
}
}
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Deserialize, Serialize)]
pub struct MarketEvent<InstrumentKey = Instrument, T = DataKind> {
pub time_exchange: DateTime<Utc>,
pub time_received: DateTime<Utc>,
pub exchange: Exchange,
pub instrument: InstrumentKey,
pub kind: T,
}
impl<InstrumentKey, T> MarketEvent<InstrumentKey, T> {
pub fn map_kind<F, O>(self, op: F) -> MarketEvent<InstrumentKey, O>
where
F: FnOnce(T) -> O,
{
MarketEvent {
time_exchange: self.time_exchange,
time_received: self.time_received,
exchange: self.exchange,
instrument: self.instrument,
kind: op(self.kind),
}
}
}
#[derive(Clone, PartialEq, Debug, Deserialize, Serialize, From)]
pub enum DataKind {
Trade(PublicTrade),
OrderBookL1(OrderBookL1),
OrderBook(OrderBookEvent),
Candle(Candle),
Liquidation(Liquidation),
}
impl<InstrumentKey> From<MarketStreamResult<InstrumentKey, PublicTrade>>
for MarketStreamResult<InstrumentKey, DataKind>
{
fn from(value: MarketStreamResult<InstrumentKey, PublicTrade>) -> Self {
value.map_ok(MarketEvent::from)
}
}
impl<InstrumentKey> From<MarketEvent<InstrumentKey, PublicTrade>>
for MarketEvent<InstrumentKey, DataKind>
{
fn from(value: MarketEvent<InstrumentKey, PublicTrade>) -> Self {
value.map_kind(PublicTrade::into)
}
}
impl<InstrumentKey> From<MarketStreamResult<InstrumentKey, OrderBookL1>>
for MarketStreamResult<InstrumentKey, DataKind>
{
fn from(value: MarketStreamResult<InstrumentKey, OrderBookL1>) -> Self {
value.map_ok(MarketEvent::from)
}
}
impl<InstrumentKey> From<MarketEvent<InstrumentKey, OrderBookL1>>
for MarketEvent<InstrumentKey, DataKind>
{
fn from(value: MarketEvent<InstrumentKey, OrderBookL1>) -> Self {
value.map_kind(OrderBookL1::into)
}
}
impl<InstrumentKey> From<MarketStreamResult<InstrumentKey, OrderBookEvent>>
for MarketStreamResult<InstrumentKey, DataKind>
{
fn from(value: MarketStreamResult<InstrumentKey, OrderBookEvent>) -> Self {
value.map_ok(MarketEvent::from)
}
}
impl<InstrumentKey> From<MarketEvent<InstrumentKey, OrderBookEvent>>
for MarketEvent<InstrumentKey, DataKind>
{
fn from(value: MarketEvent<InstrumentKey, OrderBookEvent>) -> Self {
value.map_kind(OrderBookEvent::into)
}
}
impl<InstrumentKey> From<MarketStreamResult<InstrumentKey, Candle>>
for MarketStreamResult<InstrumentKey, DataKind>
{
fn from(value: MarketStreamResult<InstrumentKey, Candle>) -> Self {
value.map_ok(MarketEvent::from)
}
}
impl<InstrumentKey> From<MarketEvent<InstrumentKey, Candle>>
for MarketEvent<InstrumentKey, DataKind>
{
fn from(value: MarketEvent<InstrumentKey, Candle>) -> Self {
value.map_kind(Candle::into)
}
}
impl<InstrumentKey> From<MarketStreamResult<InstrumentKey, Liquidation>>
for MarketStreamResult<InstrumentKey, DataKind>
{
fn from(value: MarketStreamResult<InstrumentKey, Liquidation>) -> Self {
value.map_ok(MarketEvent::from)
}
}
impl<InstrumentKey> From<MarketEvent<InstrumentKey, Liquidation>>
for MarketEvent<InstrumentKey, DataKind>
{
fn from(value: MarketEvent<InstrumentKey, Liquidation>) -> Self {
value.map_kind(Liquidation::into)
}
}