1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use crate::{
exchange::{subscription::ExchangeSub, Connector},
subscription::{Map, SubKind, Subscription, SubscriptionMeta},
Identifier,
};
use barter_integration::model::SubscriptionId;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
pub trait SubscriptionMapper {
fn map<Exchange, Kind>(subscriptions: &[Subscription<Exchange, Kind>]) -> SubscriptionMeta
where
Exchange: Connector,
Kind: SubKind,
Subscription<Exchange, 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, Kind>(subscriptions: &[Subscription<Exchange, Kind>]) -> SubscriptionMeta
where
Exchange: Connector,
Kind: SubKind,
Subscription<Exchange, Kind>: Identifier<Exchange::Channel> + Identifier<Exchange::Market>,
ExchangeSub<Exchange::Channel, Exchange::Market>: Identifier<SubscriptionId>,
{
let mut instrument_map = Map(HashMap::with_capacity(subscriptions.len()));
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.clone());
exchange_sub
})
.collect::<Vec<ExchangeSub<Exchange::Channel, Exchange::Market>>>();
let subscriptions = Exchange::requests(exchange_subs);
SubscriptionMeta {
instrument_map,
subscriptions,
}
}
}