databento_defs/
enums.rs

1//! Enums used in Databento APIs.
2use std::fmt::{self, Display, Formatter};
3use std::os::raw::c_char;
4
5use num_enum::{IntoPrimitive, TryFromPrimitive};
6
7use crate::Error;
8/// A side of the market. The side of the market for resting orders, or the side
9/// of the aggressor for trades.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
11pub enum Side {
12    /// A sell order.
13    Ask,
14    /// A buy order.
15    Bid,
16    /// None or unknown.
17    None,
18}
19
20impl From<Side> for char {
21    fn from(side: Side) -> Self {
22        match side {
23            Side::Ask => 'A',
24            Side::Bid => 'B',
25            Side::None => 'N',
26        }
27    }
28}
29
30impl From<Side> for c_char {
31    fn from(side: Side) -> Self {
32        char::from(side) as c_char
33    }
34}
35
36#[cfg(feature = "serde")]
37impl serde::Serialize for Side {
38    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
39        serializer.serialize_char(char::from(*self))
40    }
41}
42
43/// A tick action.
44#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
45pub enum Action {
46    /// An existing order was modified.
47    Modify,
48    /// A trade executed.
49    Trade,
50    /// An order was cancelled.
51    Cancel,
52    /// A new order was added.
53    Add,
54    /// Reset the book; clear all orders for an instrument.
55    Clear,
56}
57
58impl From<Action> for char {
59    fn from(action: Action) -> Self {
60        match action {
61            Action::Modify => 'M',
62            Action::Trade => 'T',
63            Action::Cancel => 'C',
64            Action::Add => 'A',
65            Action::Clear => 'R',
66        }
67    }
68}
69
70impl From<Action> for c_char {
71    fn from(action: Action) -> Self {
72        char::from(action) as c_char
73    }
74}
75
76#[cfg(feature = "serde")]
77impl serde::Serialize for Action {
78    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
79        serializer.serialize_char(char::from(*self))
80    }
81}
82
83/// A symbology type. Refer to the [symbology documentation](https://docs.databento.com/reference-historical/basics/symbology)
84/// for more information.
85#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, TryFromPrimitive)]
86#[cfg_attr(
87    feature = "serde",
88    derive(serde::Serialize),
89    serde(rename_all = "snake_case")
90)]
91#[repr(u8)]
92pub enum SType {
93    /// Symbology using a unique numeric ID.
94    ProductId = 0,
95    /// Symbology using the original symbols provided by the publisher.
96    Native = 1,
97    /// A set of Databento-specific symbologies for referring to groups of symbols.
98    Smart = 2,
99}
100
101impl std::str::FromStr for SType {
102    type Err = Error;
103
104    fn from_str(s: &str) -> Result<Self, Self::Err> {
105        match s {
106            "product_id" => Ok(SType::ProductId),
107            "native" => Ok(SType::Native),
108            "smart" => Ok(SType::Smart),
109            _ => Err(Error::TypeConversion(
110                "Value doesn't match a valid symbol type",
111            )),
112        }
113    }
114}
115
116impl SType {
117    /// Convert the symbology type to its `str` representation.
118    pub fn as_str(&self) -> &'static str {
119        match self {
120            SType::Native => "native",
121            SType::Smart => "smart",
122            SType::ProductId => "product_id",
123        }
124    }
125}
126
127impl Display for SType {
128    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
129        f.write_str(self.as_str())
130    }
131}
132
133/// A data record schema.
134#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, TryFromPrimitive)]
135#[repr(u16)]
136pub enum Schema {
137    /// Market by order.
138    Mbo = 0,
139    /// Market by price with a book depth of 1.
140    Mbp1 = 1,
141    /// Market by price with a book depth of 10.
142    Mbp10 = 2,
143    /// Combination of [Self::Trades] and [Self::Mbp1].
144    Tbbo = 3,
145    /// All trade events.
146    Trades = 4,
147    /// Open, high, low, close, and volume at a 1-second cadence.
148    Ohlcv1S = 5,
149    /// Open, high, low, close, and volume at a 1-minute cadence.
150    Ohlcv1M = 6,
151    /// Open, high, low, close, and volume at an hourly cadence.
152    Ohlcv1H = 7,
153    /// Open, high, low, close, and volume at a daily cadence.
154    Ohlcv1D = 8,
155    /// Symbol definitions.
156    Definition = 9,
157    ///
158    Statistics = 10,
159    /// Exchange status.
160    Status = 11,
161}
162
163impl std::str::FromStr for Schema {
164    type Err = Error;
165
166    fn from_str(s: &str) -> Result<Self, Self::Err> {
167        match s {
168            "mbo" => Ok(Schema::Mbo),
169            "mbp-1" => Ok(Schema::Mbp1),
170            "mbp-10" => Ok(Schema::Mbp10),
171            "tbbo" => Ok(Schema::Tbbo),
172            "trades" => Ok(Schema::Trades),
173            "ohlcv-1s" => Ok(Schema::Ohlcv1S),
174            "ohlcv-1m" => Ok(Schema::Ohlcv1M),
175            "ohlcv-1h" => Ok(Schema::Ohlcv1H),
176            "ohlcv-1d" => Ok(Schema::Ohlcv1D),
177            "definition" => Ok(Schema::Definition),
178            "statistics" => Ok(Schema::Statistics),
179            "status" => Ok(Schema::Status),
180            _ => Err(Error::TypeConversion("Value doesn't match a valid schema")),
181        }
182    }
183}
184
185impl Schema {
186    /// Converts the given schema to a `&'static str`.
187    pub fn as_str(&self) -> &'static str {
188        match self {
189            Schema::Mbo => "mbo",
190            Schema::Mbp1 => "mbp-1",
191            Schema::Mbp10 => "mbp-10",
192            Schema::Tbbo => "tbbo",
193            Schema::Trades => "trades",
194            Schema::Ohlcv1S => "ohlcv-1s",
195            Schema::Ohlcv1M => "ohlcv-1m",
196            Schema::Ohlcv1H => "ohlcv-1h",
197            Schema::Ohlcv1D => "ohlcv-1d",
198            Schema::Definition => "definition",
199            Schema::Statistics => "statistics",
200            Schema::Status => "status",
201        }
202    }
203}
204
205#[cfg(feature = "serde")]
206impl serde::Serialize for Schema {
207    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
208        serializer.serialize_str(self.as_str())
209    }
210}
211
212impl Display for Schema {
213    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
214        f.write_str(self.as_str())
215    }
216}
217
218/// A data encoding format.
219#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, TryFromPrimitive)]
220#[cfg_attr(
221    feature = "serde",
222    derive(serde::Serialize),
223    serde(rename_all = "lowercase")
224)]
225#[repr(u8)]
226pub enum Encoding {
227    /// Databento Binary Encoding + Zstandard compression.
228    Dbz = 0,
229    /// Comma-separated values.
230    Csv = 1,
231    /// JavaScript object notation.
232    Json = 2,
233}
234
235impl std::str::FromStr for Encoding {
236    type Err = Error;
237
238    fn from_str(s: &str) -> Result<Self, Self::Err> {
239        match s {
240            "dbz" => Ok(Encoding::Dbz),
241            "csv" => Ok(Encoding::Csv),
242            "json" => Ok(Encoding::Json),
243            _ => Err(Error::TypeConversion(
244                "Value doesn't match a valid encoding",
245            )),
246        }
247    }
248}
249
250impl Encoding {
251    /// Converts the given encoding to a `&'static str`.
252    pub fn as_str(&self) -> &'static str {
253        match self {
254            Encoding::Dbz => "dbz",
255            Encoding::Csv => "csv",
256            Encoding::Json => "json",
257        }
258    }
259}
260
261impl Display for Encoding {
262    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
263        f.write_str(self.as_str())
264    }
265}
266
267/// A compression format or none if uncompressed.
268#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, TryFromPrimitive)]
269#[cfg_attr(
270    feature = "serde",
271    derive(serde::Serialize),
272    serde(rename_all = "lowercase")
273)]
274#[repr(u8)]
275pub enum Compression {
276    /// Uncompressed.
277    None = 0,
278    /// Zstandard compressed.
279    ZStd = 1,
280}
281impl std::str::FromStr for Compression {
282    type Err = Error;
283
284    fn from_str(s: &str) -> Result<Self, Self::Err> {
285        match s {
286            "none" => Ok(Compression::None),
287            "zstd" => Ok(Compression::ZStd),
288            _ => Err(Error::TypeConversion(
289                "Value doesn't match a valid compression",
290            )),
291        }
292    }
293}
294
295impl Compression {
296    /// Converts the given compression to a `&'static str`.
297    pub fn as_str(&self) -> &'static str {
298        match self {
299            Compression::None => "none",
300            Compression::ZStd => "zstd",
301        }
302    }
303}
304
305impl Display for Compression {
306    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
307        f.write_str(self.as_str())
308    }
309}
310
311#[repr(u8)]
312#[derive(Clone, Copy, Debug, PartialEq, Eq, IntoPrimitive)]
313#[cfg_attr(feature = "serde", derive(serde::Serialize))]
314#[doc(hidden)]
315pub enum SecurityUpdateAction {
316    Add = b'A',
317    Modify = b'M',
318    Delete = b'D',
319    // Deprecated, but still present in legacy files
320    Invalid = b'~',
321}