use crate::{hcstrid, packed_value, symbology::Symbolic};
use anyhow::{bail, Result};
use netidx::{
pack::Pack,
protocol::value::{FromValue, Value},
utils::pack,
};
use netidx_derive::Pack;
use rust_decimal::Decimal;
use rust_decimal_macros::dec;
use schemars::{gen::SchemaGenerator, schema::Schema, JsonSchema};
use serde::{Deserialize, Serialize};
pub mod alerts;
pub mod b2c2;
pub mod coinbase;
pub mod dvchain;
pub mod limits;
pub mod oms_query;
pub mod orderflow;
pub mod secrets;
pub mod symbology;
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Pack,
Serialize,
Deserialize,
JsonSchema,
)]
#[repr(i8)]
pub enum Dir {
Buy = 1,
Sell = -1,
}
packed_value!(Dir);
impl Dir {
pub fn flip(self) -> Self {
match self {
Self::Buy => Self::Sell,
Self::Sell => Self::Buy,
}
}
pub fn to_str_uppercase(self) -> &'static str {
match self {
Self::Buy => "BUY",
Self::Sell => "SELL",
}
}
pub fn from_str_uppercase(s: &str) -> Result<Self> {
match s {
"BUY" => Ok(Self::Buy),
"SELL" => Ok(Self::Sell),
_ => bail!("invalid format: {}", s),
}
}
pub fn to_str_lowercase(self) -> &'static str {
match self {
Self::Buy => "buy",
Self::Sell => "sell",
}
}
pub fn from_str_lowercase(s: &str) -> Result<Self> {
match s {
"buy" => Ok(Self::Buy),
"sell" => Ok(Self::Sell),
_ => bail!("invalid format: {}", s),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Pack)]
pub struct DirPair<T: 'static> {
pub buy: T,
pub sell: T,
}
impl<T: Into<Value> + Pack + 'static> Into<Value> for DirPair<T> {
fn into(self) -> Value {
Value::Bytes(pack(&self).unwrap().freeze())
}
}
impl<T: FromValue + Pack + 'static> FromValue for DirPair<T> {
fn from_value(v: Value) -> Result<Self> {
match v {
Value::Bytes(mut b) => Ok(Pack::decode(&mut b)?),
_ => bail!("invalid value, expected a bytes {:?}", v),
}
}
}
impl<T: Default + 'static> Default for DirPair<T> {
fn default() -> Self {
Self { buy: T::default(), sell: T::default() }
}
}
impl<T: 'static> DirPair<T> {
pub fn get(&self, dir: Dir) -> &T {
match dir {
Dir::Buy => &self.buy,
Dir::Sell => &self.sell,
}
}
pub fn get_mut(&mut self, dir: Dir) -> &mut T {
match dir {
Dir::Buy => &mut self.buy,
Dir::Sell => &mut self.sell,
}
}
}
impl DirPair<Decimal> {
pub fn is_empty(&self) -> bool {
self.buy == dec!(0) && self.sell == dec!(0)
}
pub fn net(&self) -> Decimal {
self.buy - self.sell
}
}
hcstrid!(Desk);
packed_value!(Desk);
hcstrid!(Trader);
packed_value!(Trader);
hcstrid!(Account);
packed_value!(Account);