barter_data/
instrument.rs1use barter_instrument::{
2 Keyed,
3 instrument::{
4 Instrument,
5 market_data::{MarketDataInstrument, kind::MarketDataInstrumentKind},
6 name::InstrumentNameExchange,
7 },
8};
9use serde::{Deserialize, Serialize};
10use std::fmt::Debug;
11
12pub trait InstrumentData
18where
19 Self: Clone + Debug + Send + Sync,
20{
21 type Key: Debug + Clone + Eq + Send + Sync;
22 fn key(&self) -> &Self::Key;
23 fn kind(&self) -> &MarketDataInstrumentKind;
24}
25
26impl<InstrumentKey> InstrumentData for Keyed<InstrumentKey, MarketDataInstrument>
27where
28 InstrumentKey: Debug + Clone + Eq + Send + Sync,
29{
30 type Key = InstrumentKey;
31
32 fn key(&self) -> &Self::Key {
33 &self.key
34 }
35
36 fn kind(&self) -> &MarketDataInstrumentKind {
37 &self.value.kind
38 }
39}
40
41impl InstrumentData for MarketDataInstrument {
42 type Key = Self;
43
44 fn key(&self) -> &Self::Key {
45 self
46 }
47
48 fn kind(&self) -> &MarketDataInstrumentKind {
49 &self.kind
50 }
51}
52
53#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize)]
54pub struct MarketInstrumentData<InstrumentKey> {
55 pub key: InstrumentKey,
56 pub name_exchange: InstrumentNameExchange,
57 pub kind: MarketDataInstrumentKind,
58}
59
60impl<InstrumentKey> InstrumentData for MarketInstrumentData<InstrumentKey>
61where
62 InstrumentKey: Debug + Clone + Eq + Send + Sync,
63{
64 type Key = InstrumentKey;
65
66 fn key(&self) -> &Self::Key {
67 &self.key
68 }
69
70 fn kind(&self) -> &MarketDataInstrumentKind {
71 &self.kind
72 }
73}
74
75impl<InstrumentKey> std::fmt::Display for MarketInstrumentData<InstrumentKey>
76where
77 InstrumentKey: std::fmt::Display,
78{
79 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
80 write!(
81 f,
82 "{}_{}_{}",
83 self.key,
84 self.name_exchange.as_ref(),
85 self.kind
86 )
87 }
88}
89
90impl<ExchangeKey, AssetKey, InstrumentKey>
91 From<&Keyed<InstrumentKey, Instrument<ExchangeKey, AssetKey>>>
92 for MarketInstrumentData<InstrumentKey>
93where
94 InstrumentKey: Clone,
95{
96 fn from(value: &Keyed<InstrumentKey, Instrument<ExchangeKey, AssetKey>>) -> Self {
97 Self {
98 key: value.key.clone(),
99 name_exchange: value.value.name_exchange.clone(),
100 kind: MarketDataInstrumentKind::from(&value.value.kind),
101 }
102 }
103}