Archived
Add Side to PriceLevelUpdate.
This commit is contained in:
1 parent
cd02a89303
commit
97360aefac
9 files changed
+82
-1
No files matched your search
@@ -1 +1,4 @@
|
||||
pub mod util;
|
||||
|
||||
mod side;
|
||||
pub use side::{InvalidSide, Side};
|
||||
@@ -1,4 +1,5 @@
|
||||
use chrono::DateTime;
|
||||
use engineering_challenge::Side;
|
||||
use engineering_challenge::util::dtos::challenge_orderbook::{
|
||||
AnyMessage, PriceLevelUpdateDomain, SnapshotAsksEntryDomain, SnapshotBidsEntryDomain,
|
||||
SnapshotDomain,
|
||||
@@ -35,6 +36,7 @@ fn main() -> anyhow::Result<()> {
|
||||
exchange_timestamp: timestamp,
|
||||
price_level: 400_000,
|
||||
quantity: 1_250_000,
|
||||
side: Side::Buy,
|
||||
symbol: symbol.clone(),
|
||||
};
|
||||
|
||||
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// The side of an order or price level, shared by DTOs and application code.
|
||||
/// Buy means bid; Sell means ask. The discriminants are SBE wire values and
|
||||
/// must not be renumbered.
|
||||
#[repr(u8)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
pub enum Side {
|
||||
Buy = 0,
|
||||
Sell = 1,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Side {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.write_str(match self {
|
||||
Self::Buy => "Buy",
|
||||
Self::Sell => "Sell",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<u8> for Side {
|
||||
type Error = InvalidSide;
|
||||
|
||||
fn try_from(value: u8) -> Result<Self, Self::Error> {
|
||||
match value {
|
||||
0 => Ok(Self::Buy),
|
||||
1 => Ok(Self::Sell),
|
||||
_ => Err(InvalidSide(value)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct InvalidSide(pub u8);
|
||||
|
||||
impl std::fmt::Display for InvalidSide {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "invalid side {}; expected 0 (Buy) or 1 (Sell)", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for InvalidSide {}
|
||||
@@ -26,6 +26,10 @@ fn main() -> Result<()> {
|
||||
.with_domain_type(
|
||||
ConversionSelector::named_type("UTCTimestampMicrosEncoding"),
|
||||
"chrono::DateTime<chrono::Utc>",
|
||||
)
|
||||
.with_domain_type(
|
||||
ConversionSelector::named_type("SideEncoding"),
|
||||
"crate::Side",
|
||||
);
|
||||
generate_str_to_out_dir(&xml, config)?;
|
||||
Ok(())
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
use chrono::{DateTime, Utc};
|
||||
|
||||
use crate::{InvalidSide, Side};
|
||||
|
||||
macro_rules! schemas {
|
||||
($($schema:ident),* $(,)?) => {$(
|
||||
pub mod $schema {
|
||||
@@ -76,6 +78,22 @@ macro_rules! sbe_encode {
|
||||
|
||||
schemas!(challenge_orderbook);
|
||||
|
||||
impl challenge_orderbook::TryFromSbe<challenge_orderbook::SideEncoding> for Side {
|
||||
type Error = InvalidSide;
|
||||
|
||||
fn try_from_sbe(wire: challenge_orderbook::SideEncoding) -> Result<Self, Self::Error> {
|
||||
Self::try_from(wire.value())
|
||||
}
|
||||
}
|
||||
|
||||
impl challenge_orderbook::TryToSbe<challenge_orderbook::SideEncoding> for Side {
|
||||
type Error = std::convert::Infallible;
|
||||
|
||||
fn try_to_sbe(&self) -> Result<challenge_orderbook::SideEncoding, Self::Error> {
|
||||
Ok(challenge_orderbook::SideEncoding::new(*self as u8))
|
||||
}
|
||||
}
|
||||
|
||||
sbe_encode!(
|
||||
challenge_orderbook::SnapshotDomain,
|
||||
challenge_orderbook::PriceLevelUpdateDomain,
|
||||
|
||||
Reference in new issue
Block a user