use std::fmt;
use crate::utils::MarketKind;
use crate::utils::sign::tx::BadTx;
pub const PERPS_SLOTS: std::ops::RangeInclusive<u16> = 0..=254;
pub const BINARY_OPTIONS_SLOTS: std::ops::RangeInclusive<u16> = 1000..=2000;
pub const SPOT_SLOTS: std::ops::RangeInclusive<u16> = 2048..=4094;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct MarketSlot(u16);
impl MarketSlot {
pub fn new(slot: u16) -> Result<MarketSlot, BadTx> {
match kind_of(slot) {
Some(_) => Ok(MarketSlot(slot)),
None => Err(BadTx::new("MarketIndex", format!("{slot} is no market slot: 0-254, 1000-2000, 2048-4094"))),
}
}
#[must_use]
pub const fn of<const SLOT: u16>() -> MarketSlot {
const { assert!(kind_of(SLOT).is_some(), "not a market slot") };
MarketSlot(SLOT)
}
#[must_use]
pub const fn get(self) -> u16 {
self.0
}
#[must_use]
pub const fn kind(self) -> MarketKind {
match kind_of(self.0) {
Some(kind) => kind,
None => MarketKind::Perps,
}
}
}
const fn kind_of(slot: u16) -> Option<MarketKind> {
match slot {
0..=254 => Some(MarketKind::Perps),
1000..=2000 => Some(MarketKind::BinaryOptions),
2048..=4094 => Some(MarketKind::Spot),
_ => None,
}
}
impl fmt::Display for MarketSlot {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
pub(crate) fn binary_options_slot(slot: MarketSlot) -> Result<(), BadTx> {
match slot.kind() {
MarketKind::BinaryOptions => Ok(()),
_ => Err(BadTx::new("MarketIndex", format!("slot {slot} holds no binary options: 1000-2000"))),
}
}
#[cfg(test)]
#[path = "tests/slot.rs"]
mod tests;