use std::os::raw::c_char;
use crate::{
pretty::px_to_f64,
record::{c_chars_to_str, str_to_c_chars},
rtype, ErrorCode, ErrorRec, InstrumentClass, InstrumentDefRec, RecordHeader, Result, Side,
StatRec, SymbolMappingRec, SystemCode, SystemRec, UserDefinedInstrument,
};
use super::*;
impl ErrorMsg {
pub fn new(ts_event: u64, msg: &str) -> Self {
let mut error = Self {
hd: RecordHeader::new::<Self>(rtype::ERROR, 0, 0, ts_event),
..Default::default()
};
for (i, byte) in msg.as_bytes().iter().take(error.err.len() - 1).enumerate() {
error.err[i] = *byte as c_char;
}
error
}
}
impl ErrorRec for ErrorMsg {
fn subscription_id(&self) -> u32 {
u32::MAX
}
fn raw_schema(&self) -> u16 {
u16::MAX
}
fn raw_code(&self) -> u8 {
ErrorCode::default() as u8
}
fn is_last(&self) -> u8 {
u8::MAX
}
fn err(&self) -> Result<&str> {
c_chars_to_str(&self.err)
}
}
impl InstrumentDefMsg {
pub fn trading_reference_price_f64(&self) -> f64 {
px_to_f64(self.trading_reference_price)
}
pub fn currency(&self) -> crate::Result<&str> {
c_chars_to_str(&self.currency)
}
pub fn settl_currency(&self) -> crate::Result<&str> {
c_chars_to_str(&self.settl_currency)
}
pub fn secsubtype(&self) -> crate::Result<&str> {
c_chars_to_str(&self.secsubtype)
}
pub fn group(&self) -> crate::Result<&str> {
c_chars_to_str(&self.group)
}
pub fn exchange(&self) -> crate::Result<&str> {
c_chars_to_str(&self.exchange)
}
pub fn asset(&self) -> crate::Result<&str> {
c_chars_to_str(&self.asset)
}
pub fn cfi(&self) -> crate::Result<&str> {
c_chars_to_str(&self.cfi)
}
pub fn security_type(&self) -> crate::Result<&str> {
c_chars_to_str(&self.security_type)
}
pub fn unit_of_measure(&self) -> crate::Result<&str> {
c_chars_to_str(&self.unit_of_measure)
}
pub fn underlying(&self) -> crate::Result<&str> {
c_chars_to_str(&self.underlying)
}
pub fn strike_price_currency(&self) -> crate::Result<&str> {
c_chars_to_str(&self.strike_price_currency)
}
}
impl InstrumentDefRec for InstrumentDefMsg {
fn raw_ts_recv(&self) -> u64 {
self.ts_recv
}
fn min_price_increment(&self) -> i64 {
self.min_price_increment
}
fn display_factor(&self) -> i64 {
self.display_factor
}
fn raw_instrument_id(&self) -> u64 {
u64::from(self.raw_instrument_id)
}
fn min_lot_size_round_lot(&self) -> i32 {
self.min_lot_size_round_lot
}
fn raw_security_update_action(&self) -> c_char {
self.security_update_action as c_char
}
fn raw_instrument_class(&self) -> c_char {
self.instrument_class
}
fn raw_match_algorithm(&self) -> c_char {
self.match_algorithm
}
fn channel_id(&self) -> u8 {
u8::try_from(self.channel_id).unwrap_or(u8::MAX)
}
fn raw_symbol(&self) -> Result<&str> {
c_chars_to_str(&self.raw_symbol)
}
fn raw_expiration(&self) -> Option<u64> {
Some(self.expiration)
}
fn raw_activation(&self) -> Option<u64> {
Some(self.activation)
}
fn high_limit_price(&self) -> Option<i64> {
Some(self.high_limit_price)
}
fn low_limit_price(&self) -> Option<i64> {
Some(self.low_limit_price)
}
fn max_price_variation(&self) -> Option<i64> {
Some(self.max_price_variation)
}
fn unit_of_measure_qty(&self) -> Option<i64> {
Some(self.unit_of_measure_qty)
}
fn min_price_increment_amount(&self) -> Option<i64> {
Some(self.min_price_increment_amount)
}
fn price_ratio(&self) -> Option<i64> {
Some(self.price_ratio)
}
fn strike_price(&self) -> Option<i64> {
Some(self.strike_price)
}
fn leg_price(&self) -> Option<i64> {
None
}
fn leg_delta(&self) -> Option<i64> {
None
}
fn user_defined_instrument(&self) -> Result<Option<UserDefinedInstrument>> {
Ok(Some(self.user_defined_instrument))
}
fn leg_instrument_class(&self) -> Result<Option<InstrumentClass>> {
Ok(None)
}
fn leg_side(&self) -> Result<Option<Side>> {
Ok(None)
}
}
impl StatRec for StatMsg {
fn raw_ts_recv(&self) -> u64 {
self.ts_recv
}
fn raw_ts_ref(&self) -> u64 {
self.ts_ref
}
fn price(&self) -> i64 {
self.price
}
fn quantity(&self) -> i64 {
if self.quantity == i32::MAX {
i64::MAX
} else {
i64::from(self.quantity)
}
}
fn sequence(&self) -> u32 {
self.sequence
}
fn raw_ts_in_delta(&self) -> i32 {
self.ts_in_delta
}
fn raw_stat_type(&self) -> u16 {
self.stat_type
}
fn channel_id(&self) -> u8 {
u8::try_from(self.channel_id).unwrap_or(u8::MAX)
}
fn raw_update_action(&self) -> u8 {
self.update_action
}
fn stat_flags(&self) -> u8 {
self.stat_flags
}
}
impl SymbolMappingMsg {
pub fn new(
instrument_id: u32,
ts_event: u64,
stype_in_symbol: &str,
stype_out_symbol: &str,
start_ts: u64,
end_ts: u64,
) -> crate::Result<Self> {
Ok(Self {
hd: RecordHeader::new::<Self>(rtype::SYMBOL_MAPPING, 0, instrument_id, ts_event),
stype_in_symbol: str_to_c_chars(stype_in_symbol)?,
stype_out_symbol: str_to_c_chars(stype_out_symbol)?,
_dummy: Default::default(),
start_ts,
end_ts,
})
}
}
impl SymbolMappingRec for SymbolMappingMsg {
fn raw_start_ts(&self) -> u64 {
self.start_ts
}
fn raw_end_ts(&self) -> u64 {
self.end_ts
}
fn raw_stype_in(&self) -> u8 {
u8::MAX
}
fn raw_stype_out(&self) -> u8 {
u8::MAX
}
fn stype_in_symbol(&self) -> Result<&str> {
c_chars_to_str(&self.stype_in_symbol)
}
fn stype_out_symbol(&self) -> Result<&str> {
c_chars_to_str(&self.stype_out_symbol)
}
}
impl SystemMsg {
pub fn new(ts_event: u64, msg: &str) -> Result<Self> {
Ok(Self {
hd: RecordHeader::new::<Self>(rtype::SYSTEM, 0, 0, ts_event),
msg: str_to_c_chars(msg)?,
})
}
pub fn heartbeat(ts_event: u64) -> Self {
Self {
hd: RecordHeader::new::<Self>(rtype::SYSTEM, 0, 0, ts_event),
msg: str_to_c_chars(crate::SystemMsg::HEARTBEAT).unwrap(),
}
}
}
impl SystemRec for SystemMsg {
fn subscription_id(&self) -> u32 {
u32::MAX
}
fn raw_schema(&self) -> u16 {
u16::MAX
}
fn raw_code(&self) -> u8 {
SystemCode::default() as u8
}
fn msg(&self) -> Result<&str> {
c_chars_to_str(&self.msg)
}
}