1use cxmr_currency::CurrencyPair;
4use cxmr_exchanges::{Exchange, Market};
5
6use super::{Event, EventData};
7
8#[derive(Serialize, Deserialize, Clone)]
10pub struct Events {
11 pub market: Market,
13
14 pub events: Vec<Event>,
16
17 pub timestamp: Option<u64>,
19}
20
21impl Events {
22 pub fn exchange(&self) -> Exchange {
24 self.market.exchange()
25 }
26
27 pub fn currency_pair(&self) -> CurrencyPair {
29 self.market.currency_pair()
30 }
31}
32
33impl std::fmt::Debug for Events {
34 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
35 write!(f, "{}", ::serde_json::to_string(self).unwrap())
36 }
37}
38
39impl Into<Vec<EventData>> for Events {
40 fn into(self: Events) -> Vec<EventData> {
41 let ts = self.timestamp.unwrap();
42 self.events
43 .into_iter()
44 .map(|e| <Vec<EventData>>::from(&e))
45 .flatten()
46 .map(|mut event| {
47 if event.ts == 0 {
48 event.ts = ts;
49 }
50 event
51 })
52 .collect()
53 }
54}
55
56impl From<(Market, u64, Vec<EventData>)> for Events {
57 fn from(v: (Market, u64, Vec<EventData>)) -> Events {
58 let (market, ts, rows) = v;
59 Events {
60 market: market,
61 events: rows.into_iter().map(|r| r.into()).collect(),
62 timestamp: Some(ts * 1000),
63 }
64 }
65}