use crate::{InstrumentClass, RType, Schema, TriState, VersionUpgradePolicy};
impl InstrumentClass {
pub fn is_option(&self) -> bool {
matches!(self, Self::Call | Self::Put | Self::OptionSpread)
}
pub fn is_future(&self) -> bool {
matches!(self, Self::Future | Self::FutureSpread)
}
pub fn is_spread(&self) -> bool {
matches!(
self,
Self::FutureSpread | Self::OptionSpread | Self::MixedSpread
)
}
}
impl From<Schema> for RType {
fn from(schema: Schema) -> Self {
match schema {
Schema::Mbo => RType::Mbo,
Schema::Mbp1 | Schema::Tbbo => RType::Mbp1,
Schema::Mbp10 => RType::Mbp10,
Schema::Trades => RType::Mbp0,
Schema::Ohlcv1S => RType::Ohlcv1S,
Schema::Ohlcv1M => RType::Ohlcv1M,
Schema::Ohlcv1H => RType::Ohlcv1H,
Schema::Ohlcv1D => RType::Ohlcv1D,
Schema::OhlcvEod => RType::OhlcvEod,
Schema::Definition => RType::InstrumentDef,
Schema::Statistics => RType::Statistics,
Schema::Status => RType::Status,
Schema::Imbalance => RType::Imbalance,
Schema::Cmbp1 => RType::Cmbp1,
Schema::Cbbo1S => RType::Cbbo1S,
Schema::Cbbo1M => RType::Cbbo1M,
Schema::Tcbbo => RType::Tcbbo,
Schema::Bbo1S => RType::Bbo1S,
Schema::Bbo1M => RType::Bbo1M,
}
}
}
impl RType {
pub fn try_into_schema(rtype: u16) -> Option<Schema> {
use crate::enums::rtype::*;
let rtype = u8::try_from(rtype).ok()?;
match rtype {
MBP_0 => Some(Schema::Trades),
MBP_1 => Some(Schema::Mbp1),
TBBO => Some(Schema::Tbbo),
MBP_10 => Some(Schema::Mbp10),
OHLCV_1S => Some(Schema::Ohlcv1S),
OHLCV_1M => Some(Schema::Ohlcv1M),
OHLCV_1H => Some(Schema::Ohlcv1H),
OHLCV_1D => Some(Schema::Ohlcv1D),
OHLCV_EOD => Some(Schema::OhlcvEod),
STATUS => Some(Schema::Status),
INSTRUMENT_DEF => Some(Schema::Definition),
IMBALANCE => Some(Schema::Imbalance),
STATISTICS => Some(Schema::Statistics),
MBO => Some(Schema::Mbo),
CMBP_1 => Some(Schema::Cmbp1),
CBBO_1S => Some(Schema::Cbbo1S),
CBBO_1M => Some(Schema::Cbbo1M),
TCBBO => Some(Schema::Tcbbo),
BBO_1S => Some(Schema::Bbo1S),
BBO_1M => Some(Schema::Bbo1M),
_ => None,
}
}
pub const fn interval(self) -> Option<time::Duration> {
match self {
RType::Ohlcv1S | RType::Cbbo1S | RType::Bbo1S => Some(time::Duration::SECOND),
RType::Ohlcv1M | RType::Cbbo1M | RType::Bbo1M => Some(time::Duration::MINUTE),
RType::Ohlcv1H => Some(time::Duration::HOUR),
RType::Ohlcv1D | RType::OhlcvEod => Some(time::Duration::DAY),
_ => None,
}
}
}
impl Schema {
pub fn interval(self) -> Option<time::Duration> {
RType::from(self).interval()
}
}
impl From<TriState> for Option<bool> {
fn from(value: TriState) -> Self {
match value {
TriState::NotAvailable => None,
TriState::No => Some(false),
TriState::Yes => Some(true),
}
}
}
impl From<Option<bool>> for TriState {
fn from(value: Option<bool>) -> Self {
match value {
Some(true) => Self::Yes,
Some(false) => Self::No,
None => Self::NotAvailable,
}
}
}
impl VersionUpgradePolicy {
pub fn validate_compatibility(self, version: u8) -> crate::Result<()> {
match self.target_version() {
Some(target) if self == Self::ExpectV4 && version < target => {
Err(crate::Error::decode(format!(
"`VersionUpgradePolicy::ExpectV4` requires DBN version {target} input, got version {version}. Choose `UpgradeToV4` to convert it"
)))
}
Some(target) if version > target => Err(crate::Error::decode(format!(
"Invalid combination of `VersionUpgradePolicy::{self:?}` and input version {version}. Choose either `AsIs` or a newer upgrade policy"
))),
_ => Ok(()),
}
}
pub(crate) fn target_version(self) -> Option<u8> {
match self {
Self::AsIs => None,
Self::UpgradeToV2 => Some(2),
Self::UpgradeToV3 => Some(3),
Self::UpgradeToV4 | Self::ExpectV4 => Some(crate::v4::DBN_VERSION),
}
}
pub(crate) fn is_upgrade_situation(self, version: u8) -> bool {
self != Self::ExpectV4 && self.target_version().is_some_and(|target| version < target)
}
pub fn output_version(self, input_version: u8) -> u8 {
self.target_version().unwrap_or(input_version)
}
}
#[cfg(test)]
mod tests {
use rstest::*;
use crate::VersionUpgradePolicy;
#[rstest]
fn upgrade_to_v4_targets_v4(#[values(1, 2, 3)] input_version: u8) {
let policy = VersionUpgradePolicy::UpgradeToV4;
assert_eq!(policy.output_version(input_version), crate::v4::DBN_VERSION);
assert!(policy.is_upgrade_situation(input_version));
assert!(policy.validate_compatibility(input_version).is_ok());
}
#[rstest]
fn expect_v4_accepts_only_v4(#[values(1, 2, 3, 4)] input_version: u8) {
let policy = VersionUpgradePolicy::ExpectV4;
assert_eq!(
policy.validate_compatibility(input_version).is_ok(),
input_version == crate::v4::DBN_VERSION
);
assert!(!policy.is_upgrade_situation(input_version));
assert_eq!(policy.output_version(input_version), crate::v4::DBN_VERSION);
}
#[test]
fn upgrade_to_v3_rejects_v4() {
assert!(VersionUpgradePolicy::UpgradeToV3
.validate_compatibility(crate::v4::DBN_VERSION)
.is_err());
}
}