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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#![warn(
missing_debug_implementations,
missing_copy_implementations,
rust_2018_idioms,
)]
use std::fmt::{Debug, Display, Formatter};
use std::ops::{Deref, DerefMut};
use serde::{Deserialize, Deserializer, Serialize};
pub mod socket;
pub mod util;
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Deserialize, Serialize)]
pub struct Instrument {
pub base: Symbol,
pub quote: Symbol,
pub kind: InstrumentKind,
}
impl Display for Instrument {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", format!("({}_{}, {}", self.base, self.quote, self.kind))
}
}
impl<S> From<(S, S, InstrumentKind)> for Instrument
where
S: Into<Symbol>,
{
fn from((base, quote, kind): (S, S, InstrumentKind)) -> Self {
Self {
base: base.into(),
quote: quote.into(),
kind
}
}
}
impl Instrument {
pub fn new<S>(base: S, quote: S, kind: InstrumentKind) -> Self
where
S: Into<Symbol>
{
Self {
base: base.into(),
quote: quote.into(),
kind,
}
}
pub fn to_id(&self, exchange: &str) -> InstrumentId {
InstrumentId::new(self, exchange)
}
}
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum InstrumentKind {
Spot,
FuturePerpetual,
FutureQuarterly,
}
impl Default for InstrumentKind {
fn default() -> Self {
Self::Spot
}
}
impl Display for InstrumentKind {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", match self {
InstrumentKind::Spot => "spot",
InstrumentKind::FuturePerpetual => "future_perpetual",
InstrumentKind::FutureQuarterly => "future_quarterly",
})
}
}
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize)]
pub struct Symbol(String);
impl Debug for Symbol {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl Display for Symbol {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl AsRef<str> for Symbol {
fn as_ref(&self) -> &str {
&self.0
}
}
impl<'de> Deserialize<'de> for Symbol {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de> {
String::deserialize(deserializer).map(Symbol::new)
}
}
impl<S> From<S> for Symbol where S: Into<String> {
fn from(input: S) -> Self {
Symbol::new(input)
}
}
impl Symbol {
pub fn new<S>(input: S) -> Self where S: Into<String> {
Self(input.into().to_lowercase())
}
}
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize)]
pub struct InstrumentId(String);
impl Debug for InstrumentId {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl Display for InstrumentId {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl AsRef<str> for InstrumentId {
fn as_ref(&self) -> &str {
&self.0
}
}
impl<'de> Deserialize<'de> for InstrumentId {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
String::deserialize(deserializer).map(InstrumentId)
}
}
impl InstrumentId {
pub fn new(instrument: &Instrument, exchange: &str) -> Self {
Self(format!("{}_{}_{}_{}", exchange, instrument.base, instrument.quote, instrument.kind).to_lowercase())
}
}
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize)]
pub struct Sequence(pub u64);
impl Display for Sequence {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl Debug for Sequence {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl AsRef<u64> for Sequence {
fn as_ref(&self) -> &u64 {
&self.0
}
}
impl Deref for Sequence {
type Target = u64;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for Sequence {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<'de> Deserialize<'de> for Sequence {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de> {
u64::deserialize(deserializer).map(Sequence)
}
}