Skip to main content

qamd_rs/
types.rs

1use std::fmt;
2use serde::{Serialize, Deserialize};
3
4/// Represents an optional numeric value in market data
5/// This is needed because market data may contain special
6/// string values like "-" to represent missing data
7#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
8#[serde(untagged)]
9pub enum OptionalNumeric<T> {
10    /// A valid numeric value
11    Value(T),
12    /// A string value (usually "-" for missing data)
13    String(String),
14    /// A null value (explicitly missing)
15    #[serde(rename = "null")]
16    Null,
17}
18
19impl<T: Default> Default for OptionalNumeric<T> {
20    fn default() -> Self {
21        OptionalNumeric::Null
22    }
23}
24
25impl<T: fmt::Display> fmt::Display for OptionalNumeric<T> {
26    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27        match self {
28            OptionalNumeric::Value(v) => write!(f, "{}", v),
29            OptionalNumeric::String(s) => write!(f, "{}", s),
30            OptionalNumeric::Null => write!(f, "null"),
31        }
32    }
33}
34
35/// Type alias for optional market data fields (typically price-related)
36pub type OptionalF64 = OptionalNumeric<f64>;
37
38/// Type alias for optional volume fields
39pub type OptionalI64 = OptionalNumeric<i64>;