From 97360aefacaa455289f68cbec51b9965f8b7e949 Mon Sep 17 00:00:00 2001 From: Calder White Date: Thu, 22 Oct 2026 18:44:20 -0400 Subject: [PATCH] Add Side to PriceLevelUpdate. --- Cargo.toml | 1 + README.md | 1 + .../challenge_orderbook.yaml | 6 ++- sbe_schemas/includes/common-types.xml | 5 +++ src/lib.rs | 3 ++ src/main.rs | 2 + src/side.rs | 43 +++++++++++++++++++ src/util/build.rs | 4 ++ src/util/dtos.rs | 18 ++++++++ 9 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 src/side.rs diff --git a/Cargo.toml b/Cargo.toml index 05b273d..7a4b316 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ build = "src/util/build.rs" anyhow = "1" chrono = "0.4" crossbeam = "0.8.4" +serde = { version = "1", features = ["derive"] } tempfile = "3" [build-dependencies] diff --git a/README.md b/README.md index 8403e93..4dab97f 100644 --- a/README.md +++ b/README.md @@ -18,3 +18,4 @@ If you're curious to see the YAML schema, take a look at [challenge_orderbook.ya We also use a custom timestamp field, so that the Rust struct can use `chrono::DateTime` in the generated struct, and in SBE schema, it is a uint64 representing the timestamp as the number of microseconds since unix epoch. + diff --git a/sbe_schemas/engineering_challenge/challenge_orderbook.yaml b/sbe_schemas/engineering_challenge/challenge_orderbook.yaml index 54d54ea..002d896 100644 --- a/sbe_schemas/engineering_challenge/challenge_orderbook.yaml +++ b/sbe_schemas/engineering_challenge/challenge_orderbook.yaml @@ -1,5 +1,5 @@ id: 1 -version: 1 +version: 2 description: Voluntas engineering challenge orderbook update schema. messages: @@ -49,4 +49,8 @@ messages: id: 5 type: int64 description: The absolute available quantity, in fixed point; 1_000_000 = 1 share/contract + side: + id: 6 + type: SideEncoding + description: The side being updated; Buy = bid, Sell = ask symbol: { id: 3, type: string } diff --git a/sbe_schemas/includes/common-types.xml b/sbe_schemas/includes/common-types.xml index 46cb4f9..e325b5b 100644 --- a/sbe_schemas/includes/common-types.xml +++ b/sbe_schemas/includes/common-types.xml @@ -47,6 +47,11 @@ + + + + + 0 diff --git a/src/lib.rs b/src/lib.rs index 812d1ed..3065c9a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1 +1,4 @@ pub mod util; + +mod side; +pub use side::{InvalidSide, Side}; diff --git a/src/main.rs b/src/main.rs index dffd52a..a6461fc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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(), }; diff --git a/src/side.rs b/src/side.rs new file mode 100644 index 0000000..9bf2a5b --- /dev/null +++ b/src/side.rs @@ -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 for Side { + type Error = InvalidSide; + + fn try_from(value: u8) -> Result { + 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 {} diff --git a/src/util/build.rs b/src/util/build.rs index 491d7a9..2ac7bdb 100644 --- a/src/util/build.rs +++ b/src/util/build.rs @@ -26,6 +26,10 @@ fn main() -> Result<()> { .with_domain_type( ConversionSelector::named_type("UTCTimestampMicrosEncoding"), "chrono::DateTime", + ) + .with_domain_type( + ConversionSelector::named_type("SideEncoding"), + "crate::Side", ); generate_str_to_out_dir(&xml, config)?; Ok(()) diff --git a/src/util/dtos.rs b/src/util/dtos.rs index 5ffdc51..6498a1f 100644 --- a/src/util/dtos.rs +++ b/src/util/dtos.rs @@ -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 for Side { + type Error = InvalidSide; + + fn try_from_sbe(wire: challenge_orderbook::SideEncoding) -> Result { + Self::try_from(wire.value()) + } +} + +impl challenge_orderbook::TryToSbe for Side { + type Error = std::convert::Infallible; + + fn try_to_sbe(&self) -> Result { + Ok(challenge_orderbook::SideEncoding::new(*self as u8)) + } +} + sbe_encode!( challenge_orderbook::SnapshotDomain, challenge_orderbook::PriceLevelUpdateDomain,