barter_data/subscriber/
mapper.rsuse crate::{
exchange::{subscription::ExchangeSub, Connector},
instrument::InstrumentData,
subscription::{Map, Subscription, SubscriptionKind, SubscriptionMeta},
Identifier,
};
use barter_integration::model::SubscriptionId;
use fnv::FnvHashMap;
use serde::{Deserialize, Serialize};
pub trait SubscriptionMapper {
fn map<Exchange, Instrument, Kind>(
subscriptions: &[Subscription<Exchange, Instrument, Kind>],
) -> SubscriptionMeta<Instrument::Key>
where
Exchange: Connector,
Instrument: InstrumentData,
Kind: SubscriptionKind,
Subscription<Exchange, Instrument, Kind>:
Identifier<Exchange::Channel> + Identifier<Exchange::Market>;
}
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Deserialize, Serialize)]
pub struct WebSocketSubMapper;
impl SubscriptionMapper for WebSocketSubMapper {
fn map<Exchange, Instrument, Kind>(
subscriptions: &[Subscription<Exchange, Instrument, Kind>],
) -> SubscriptionMeta<Instrument::Key>
where
Exchange: Connector,
Instrument: InstrumentData,
Kind: SubscriptionKind,
Subscription<Exchange, Instrument, Kind>:
Identifier<Exchange::Channel> + Identifier<Exchange::Market>,
ExchangeSub<Exchange::Channel, Exchange::Market>: Identifier<SubscriptionId>,
{
let mut instrument_map = Map(FnvHashMap::with_capacity_and_hasher(
subscriptions.len(),
Default::default(),
));
let exchange_subs = subscriptions
.iter()
.map(|subscription| {
let exchange_sub = ExchangeSub::new(subscription);
let subscription_id = exchange_sub.id();
instrument_map
.0
.insert(subscription_id, subscription.instrument.key().clone());
exchange_sub
})
.collect::<Vec<ExchangeSub<Exchange::Channel, Exchange::Market>>>();
let ws_subscriptions = Exchange::requests(exchange_subs);
SubscriptionMeta {
instrument_map,
ws_subscriptions,
}
}
}