use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum AssetClass {
Stocks,
Options,
Fx,
Crypto,
Indices,
}
impl AssetClass {
pub fn as_str(&self) -> &'static str {
match self {
Self::Stocks => "stocks",
Self::Options => "options",
Self::Fx => "fx",
Self::Crypto => "crypto",
Self::Indices => "indices",
}
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[non_exhaustive]
pub struct MarketSnapshot {
pub symbol: Option<String>,
pub name: Option<String>,
pub asset_class: Option<AssetClass>,
pub market_status: Option<String>,
pub last_price: Option<f64>,
pub bid: Option<f64>,
pub ask: Option<f64>,
pub open: Option<f64>,
pub high: Option<f64>,
pub low: Option<f64>,
pub close: Option<f64>,
pub previous_close: Option<f64>,
pub volume: Option<f64>,
pub change: Option<f64>,
pub change_percent: Option<f64>,
pub error: Option<String>,
pub message: Option<String>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn asset_class_round_trips_through_provider_spelling() {
for (variant, name) in [
(AssetClass::Stocks, "stocks"),
(AssetClass::Options, "options"),
(AssetClass::Fx, "fx"),
(AssetClass::Crypto, "crypto"),
(AssetClass::Indices, "indices"),
] {
assert_eq!(variant.as_str(), name);
let json = serde_json::to_string(&variant).unwrap();
assert_eq!(json, format!("\"{name}\""));
assert_eq!(
serde_json::from_str::<AssetClass>(&json).unwrap(),
variant,
"{name}"
);
}
}
}