barter_data/subscriber/
mapper.rs1use crate::{
2 Identifier,
3 exchange::{Connector, subscription::ExchangeSub},
4 instrument::InstrumentData,
5 subscription::{Map, Subscription, SubscriptionKind, SubscriptionMeta},
6};
7use barter_integration::subscription::SubscriptionId;
8use fnv::FnvHashMap;
9use serde::{Deserialize, Serialize};
10
11pub trait SubscriptionMapper {
14 fn map<Exchange, Instrument, Kind>(
15 subscriptions: &[Subscription<Exchange, Instrument, Kind>],
16 ) -> SubscriptionMeta<Instrument::Key>
17 where
18 Exchange: Connector,
19 Instrument: InstrumentData,
20 Kind: SubscriptionKind,
21 Subscription<Exchange, Instrument, Kind>:
22 Identifier<Exchange::Channel> + Identifier<Exchange::Market>;
23}
24
25#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Deserialize, Serialize)]
28pub struct WebSocketSubMapper;
29
30impl SubscriptionMapper for WebSocketSubMapper {
31 fn map<Exchange, Instrument, Kind>(
32 subscriptions: &[Subscription<Exchange, Instrument, Kind>],
33 ) -> SubscriptionMeta<Instrument::Key>
34 where
35 Exchange: Connector,
36 Instrument: InstrumentData,
37 Kind: SubscriptionKind,
38 Subscription<Exchange, Instrument, Kind>:
39 Identifier<Exchange::Channel> + Identifier<Exchange::Market>,
40 ExchangeSub<Exchange::Channel, Exchange::Market>: Identifier<SubscriptionId>,
41 {
42 let mut instrument_map = Map(FnvHashMap::with_capacity_and_hasher(
44 subscriptions.len(),
45 Default::default(),
46 ));
47
48 let exchange_subs = subscriptions
50 .iter()
51 .map(|subscription| {
52 let exchange_sub = ExchangeSub::new(subscription);
54
55 let subscription_id = exchange_sub.id();
57
58 instrument_map
60 .0
61 .insert(subscription_id, subscription.instrument.key().clone());
62
63 exchange_sub
64 })
65 .collect::<Vec<ExchangeSub<Exchange::Channel, Exchange::Market>>>();
66
67 let ws_subscriptions = Exchange::requests(exchange_subs);
69
70 SubscriptionMeta {
71 instrument_map,
72 ws_subscriptions,
73 }
74 }
75}