rusty_model/instruments/
mod.rs1use crate::venues::Venue;
2use rusty_common::SmartString;
3use serde::{Deserialize, Serialize};
4use std::any::Any;
5use std::fmt::{Debug, Display, Formatter};
6
7pub trait Instrument: 'static + Send + Sync + Debug {
9 fn id(&self) -> InstrumentId;
11
12 fn symbol(&self) -> SmartString {
14 self.id().symbol
15 }
16
17 fn venue(&self) -> Venue {
19 self.id().venue
20 }
21
22 fn as_any(&self) -> &dyn Any;
24
25 fn clone_box(&self) -> Box<dyn Instrument>;
27}
28
29#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
31pub struct InstrumentId {
32 pub symbol: SmartString, pub venue: Venue,
36}
37
38impl InstrumentId {
39 #[must_use]
41 pub fn new(symbol: impl AsRef<str>, venue: Venue) -> Self {
42 Self {
43 symbol: symbol.as_ref().into(),
44 venue,
45 }
46 }
47}
48
49impl Display for InstrumentId {
50 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
51 write!(
52 f,
53 "InstrumentId(symbol: {}, venue: {:?})",
54 self.symbol, self.venue
55 )
56 }
57}
58
59#[derive(Debug, Clone)]
61pub struct SpotInstrument {
62 id: InstrumentId,
63 base_currency: SmartString,
64 quote_currency: SmartString,
65}
66
67impl SpotInstrument {
68 #[must_use]
70 pub fn new(
71 symbol: impl AsRef<str>,
72 base_currency: impl AsRef<str>,
73 quote_currency: impl AsRef<str>,
74 venue: Venue,
75 ) -> Self {
76 Self {
77 id: InstrumentId::new(symbol, venue),
78 base_currency: base_currency.as_ref().into(),
79 quote_currency: quote_currency.as_ref().into(),
80 }
81 }
82
83 #[must_use]
85 pub fn base_currency(&self) -> &str {
86 &self.base_currency
87 }
88
89 #[must_use]
91 pub fn quote_currency(&self) -> &str {
92 &self.quote_currency
93 }
94}
95
96impl Instrument for SpotInstrument {
97 fn id(&self) -> InstrumentId {
98 self.id.clone()
99 }
100
101 fn as_any(&self) -> &dyn Any {
102 self
103 }
104
105 fn clone_box(&self) -> Box<dyn Instrument> {
106 Box::new(self.clone())
107 }
108}