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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//! Enums used in Databento APIs.
use std::fmt::{self, Display, Formatter};
use std::os::raw::c_char;

use num_enum::TryFromPrimitive;

use crate::Error;
/// A side of the market. The side of the market for resting orders, or the side
/// of the aggressor for trades.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Side {
    /// A sell order.
    Ask,
    /// A buy order.
    Bid,
    /// None or unknown.
    None,
}

impl From<Side> for char {
    fn from(side: Side) -> Self {
        match side {
            Side::Ask => 'A',
            Side::Bid => 'B',
            Side::None => 'N',
        }
    }
}

impl From<Side> for c_char {
    fn from(side: Side) -> Self {
        char::from(side) as c_char
    }
}

#[cfg(feature = "serde")]
impl serde::Serialize for Side {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_char(char::from(*self))
    }
}

/// A tick action.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Action {
    /// An existing order was modified.
    Modify,
    /// A trade executed.
    Trade,
    /// An order was cancelled.
    Cancel,
    /// A new order was added.
    Add,
    /// Reset the book; clear all orders for an instrument.
    Clear,
}

impl From<Action> for char {
    fn from(action: Action) -> Self {
        match action {
            Action::Modify => 'M',
            Action::Trade => 'T',
            Action::Cancel => 'C',
            Action::Add => 'A',
            Action::Clear => 'R',
        }
    }
}

impl From<Action> for c_char {
    fn from(action: Action) -> Self {
        char::from(action) as c_char
    }
}

#[cfg(feature = "serde")]
impl serde::Serialize for Action {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_char(char::from(*self))
    }
}

/// A symbology type. Refer to the [symbology documentation](https://docs.databento.com/reference-historical/basics/symbology)
/// for more information.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, TryFromPrimitive)]
#[cfg_attr(
    feature = "serde",
    derive(serde::Serialize),
    serde(rename_all = "snake_case")
)]
#[repr(u8)]
pub enum SType {
    /// Symbology using a unique numeric ID.
    ProductId = 0,
    /// Symbology using the original symbols provided by the publisher.
    Native = 1,
    /// A set of Databento-specific symbologies for referring to groups of symbols.
    Smart = 2,
}

impl std::str::FromStr for SType {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "product_id" => Ok(SType::ProductId),
            "native" => Ok(SType::Native),
            "smart" => Ok(SType::Smart),
            _ => Err(Error::TypeConversion(
                "Value doesn't match a valid symbol type",
            )),
        }
    }
}

impl SType {
    /// Convert the symbology type to its `str` representation.
    pub fn as_str(&self) -> &'static str {
        match self {
            SType::Native => "native",
            SType::Smart => "smart",
            SType::ProductId => "product_id",
        }
    }
}

impl Display for SType {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// A data record schema.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, TryFromPrimitive)]
#[repr(u16)]
pub enum Schema {
    /// Market by order.
    Mbo = 0,
    /// Market by price with a book depth of 1.
    Mbp1 = 1,
    /// Market by price with a book depth of 10.
    Mbp10 = 2,
    /// Combination of [Self::Trades] and [Self::Mbp1].
    Tbbo = 3,
    /// All trade events.
    Trades = 4,
    /// Open, high, low, close, and volume at a 1-second cadence.
    Ohlcv1S = 5,
    /// Open, high, low, close, and volume at a 1-minute cadence.
    Ohlcv1M = 6,
    /// Open, high, low, close, and volume at an hourly cadence.
    Ohlcv1H = 7,
    /// Open, high, low, close, and volume at a daily cadence.
    Ohlcv1D = 8,
    /// Symbol definitions.
    Definition = 9,
    ///
    Statistics = 10,
    /// Exchange status.
    Status = 11,
}

impl std::str::FromStr for Schema {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "mbo" => Ok(Schema::Mbo),
            "mbp-1" => Ok(Schema::Mbp1),
            "mbp-10" => Ok(Schema::Mbp10),
            "tbbo" => Ok(Schema::Tbbo),
            "trades" => Ok(Schema::Trades),
            "ohlcv-1s" => Ok(Schema::Ohlcv1S),
            "ohlcv-1m" => Ok(Schema::Ohlcv1M),
            "ohlcv-1h" => Ok(Schema::Ohlcv1H),
            "ohlcv-1d" => Ok(Schema::Ohlcv1D),
            "definition" => Ok(Schema::Definition),
            "statistics" => Ok(Schema::Statistics),
            "status" => Ok(Schema::Status),
            _ => Err(Error::TypeConversion("Value doesn't match a valid schema")),
        }
    }
}

impl Schema {
    /// Converts the given schema to a `&'static str`.
    pub fn as_str(&self) -> &'static str {
        match self {
            Schema::Mbo => "mbo",
            Schema::Mbp1 => "mbp-1",
            Schema::Mbp10 => "mbp-10",
            Schema::Tbbo => "tbbo",
            Schema::Trades => "trades",
            Schema::Ohlcv1S => "ohlcv-1s",
            Schema::Ohlcv1M => "ohlcv-1m",
            Schema::Ohlcv1H => "ohlcv-1h",
            Schema::Ohlcv1D => "ohlcv-1d",
            Schema::Definition => "definition",
            Schema::Statistics => "statistics",
            Schema::Status => "status",
        }
    }
}

#[cfg(feature = "serde")]
impl serde::Serialize for Schema {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_str(self.as_str())
    }
}

impl Display for Schema {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// A data encoding format.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, TryFromPrimitive)]
#[cfg_attr(
    feature = "serde",
    derive(serde::Serialize),
    serde(rename_all = "lowercase")
)]
#[repr(u8)]
pub enum Encoding {
    /// Databento Binary Encoding + Zstandard compression.
    Dbz = 0,
    /// Comma-separated values.
    Csv = 1,
    /// JavaScript object notation.
    Json = 2,
}

impl std::str::FromStr for Encoding {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "dbz" => Ok(Encoding::Dbz),
            "csv" => Ok(Encoding::Csv),
            "json" => Ok(Encoding::Json),
            _ => Err(Error::TypeConversion(
                "Value doesn't match a valid encoding",
            )),
        }
    }
}

impl Encoding {
    /// Converts the given encoding to a `&'static str`.
    pub fn as_str(&self) -> &'static str {
        match self {
            Encoding::Dbz => "dbz",
            Encoding::Csv => "csv",
            Encoding::Json => "json",
        }
    }
}

impl Display for Encoding {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// A compression format or none if uncompressed.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, TryFromPrimitive)]
#[cfg_attr(
    feature = "serde",
    derive(serde::Serialize),
    serde(rename_all = "lowercase")
)]
#[repr(u8)]
pub enum Compression {
    /// Uncompressed.
    None = 0,
    /// Zstandard compressed.
    ZStd = 1,
}
impl std::str::FromStr for Compression {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "none" => Ok(Compression::None),
            "zstd" => Ok(Compression::ZStd),
            _ => Err(Error::TypeConversion(
                "Value doesn't match a valid compression",
            )),
        }
    }
}

impl Compression {
    /// Converts the given compression to a `&'static str`.
    pub fn as_str(&self) -> &'static str {
        match self {
            Compression::None => "none",
            Compression::ZStd => "zstd",
        }
    }
}

impl Display for Compression {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}