use rust_decimal::Decimal;
use rust_decimal::serde::float_option as decimal_opt;
use serde::Deserialize;
use strum::{Display, EnumString, FromRepr};
use crate::error::{Error, Result};
use crate::streamer::{Service, subscription::SubscriptionField};
impl SubscriptionField for Field {
const SERVICE: Service = Service::LevelOneFuturesOptions;
}
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
Hash,
serde_repr::Serialize_repr,
Display,
EnumString,
FromRepr,
)]
#[repr(u8)]
#[strum(serialize_all = "snake_case")]
#[non_exhaustive]
pub enum Field {
Symbol,
BidPrice,
AskPrice,
LastPrice,
BidSize,
AskSize,
BidId,
AskId,
TotalVolume,
LastSize,
QuoteTime,
TradeTime,
HighPrice,
LowPrice,
ClosePrice,
LastId,
Description,
OpenPrice,
OpenInterest,
Mark,
Tick,
TickAmount,
FutureMultiplier,
FutureSettlementPrice,
UnderlyingSymbol,
StrikePrice,
FutureExpirationDate,
ExpirationStyle,
ContractType,
SecurityStatus,
Exchange,
ExchangeName,
}
impl From<Field> for u8 {
fn from(field: Field) -> Self {
field as u8
}
}
impl TryFrom<u8> for Field {
type Error = String;
fn try_from(value: u8) -> std::result::Result<Self, Self::Error> {
Field::from_repr(value).ok_or_else(|| format!("Invalid field: {}", value))
}
}
#[derive(Debug, Clone, Default, Deserialize, PartialEq, Eq, Hash)]
#[serde(default)]
#[non_exhaustive]
pub struct Content {
pub key: String,
pub delayed: bool,
#[serde(rename = "assetMainType")]
pub asset_main_type: Option<String>,
#[serde(rename = "assetSubType")]
pub asset_sub_type: Option<String>,
pub cusip: Option<String>,
pub symbol: Option<String>,
#[serde(with = "decimal_opt")]
pub bid_price: Option<Decimal>,
#[serde(with = "decimal_opt")]
pub ask_price: Option<Decimal>,
#[serde(with = "decimal_opt")]
pub last_price: Option<Decimal>,
pub bid_size: Option<u64>,
pub ask_size: Option<u64>,
pub bid_id: Option<String>,
pub ask_id: Option<String>,
pub total_volume: Option<u64>,
pub last_size: Option<u64>,
pub quote_time: Option<u64>,
pub trade_time: Option<u64>,
#[serde(with = "decimal_opt")]
pub high_price: Option<Decimal>,
#[serde(with = "decimal_opt")]
pub low_price: Option<Decimal>,
#[serde(with = "decimal_opt")]
pub close_price: Option<Decimal>,
pub last_id: Option<String>,
pub description: Option<String>,
#[serde(with = "decimal_opt")]
pub open_price: Option<Decimal>,
#[serde(with = "decimal_opt")]
pub open_interest: Option<Decimal>,
#[serde(with = "decimal_opt")]
pub mark: Option<Decimal>,
#[serde(with = "decimal_opt")]
pub tick: Option<Decimal>,
#[serde(with = "decimal_opt")]
pub tick_amount: Option<Decimal>,
#[serde(with = "decimal_opt")]
pub future_multiplier: Option<Decimal>,
#[serde(with = "decimal_opt")]
pub future_settlement_price: Option<Decimal>,
pub underlying_symbol: Option<String>,
#[serde(with = "decimal_opt")]
pub strike_price: Option<Decimal>,
pub future_expiration_date: Option<i64>,
pub expiration_style: Option<String>,
pub contract_type: Option<String>,
pub security_status: Option<String>,
pub exchange: Option<String>,
pub exchange_name: Option<String>,
}
impl Content {
pub(crate) fn decode_batch(remapped: serde_json::Value) -> Result<Vec<Self>> {
serde_json::from_value(remapped).map_err(|e| Error::Codec {
context: "LEVELONE_FUTURES_OPTIONS content".to_string(),
reason: e.to_string(),
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::streamer::StreamerRequest;
use crate::streamer::StreamerResponse;
use crate::streamer::response::{DataContent, parse};
use crate::streamer::subscription::{Command, Subscription, subscribe_parameters};
use rust_decimal_macros::dec;
#[test]
fn parses_level_one_futures_options_data_into_typed_content() {
let frame = r#"{
"data": [{
"service": "LEVELONE_FUTURES_OPTIONS",
"timestamp": 1714949592301,
"command": "SUBS",
"content": [{
"key": "./OZCZ23C565",
"delayed": false,
"1": 12.25, "2": 12.50, "3": 12.375,
"4": 5, "5": 7, "8": 234,
"18": 1500.5,
"19": 12.375, "20": 0.25, "21": 12.50,
"22": 50.0,
"24": "/ZCZ23", "25": 565.0,
"28": "C"
}]
}]
}"#;
let StreamerResponse::Data(data) = parse(frame).unwrap() else {
panic!("expected Data");
};
let payload = &data[0];
assert_eq!(payload.service, Service::LevelOneFuturesOptions);
let DataContent::LevelOneFuturesOptions(items) = &payload.content else {
panic!("expected LevelOneFuturesOptions");
};
let item = &items[0];
assert_eq!(item.key, "./OZCZ23C565");
assert_eq!(item.bid_price, Some(dec!(12.25)));
assert_eq!(item.ask_price, Some(dec!(12.50)));
assert_eq!(item.total_volume, Some(234));
assert_eq!(item.open_interest, Some(dec!(1500.5))); assert_eq!(item.mark, Some(dec!(12.375)));
assert_eq!(item.future_multiplier, Some(dec!(50.0)));
assert_eq!(item.underlying_symbol.as_deref(), Some("/ZCZ23"));
assert_eq!(item.strike_price, Some(dec!(565.0)));
assert_eq!(item.contract_type.as_deref(), Some("C"));
}
#[test]
fn fields_serialize_as_numeric_index() {
let value = subscribe_parameters(
vec!["./OZCZ23C565".to_string()],
vec![
Field::Symbol,
Field::BidPrice,
Field::StrikePrice,
Field::ContractType,
],
);
assert_eq!(value["keys"], "./OZCZ23C565");
assert_eq!(value["fields"], "0,1,25,28");
}
#[test]
fn from_subscription_never_panics() {
let sub = Subscription {
command: Command::Subscribe,
keys: vec!["./OZCZ23C565".to_string()],
fields: vec![Field::Symbol, Field::BidPrice],
};
let _request: StreamerRequest = sub.into();
let sub = Subscription::<Field> {
command: Command::Unsubscribe,
keys: vec![],
fields: vec![],
};
let _request: StreamerRequest = sub.into();
}
#[test]
fn snake_case_field_names_round_trip() {
assert_eq!(Field::UnderlyingSymbol.to_string(), "underlying_symbol");
assert_eq!(
Field::FutureExpirationDate.to_string(),
"future_expiration_date"
);
assert_eq!(Field::ContractType.to_string(), "contract_type");
}
}