From 6d6fcf89c8b4cbd3574e5daddccd76d8637eff28 Mon Sep 17 00:00:00 2001 From: 96RadhikaJadhav Date: Sun, 21 Feb 2021 12:31:33 +0530 Subject: [PATCH] Renamed matchbook-types-to-matchbook-messages --- packages/matchbook-messages/.gitignore | 1 + packages/matchbook-messages/Cargo.lock | 260 +++++++++++++++++++++++++ packages/matchbook-messages/Cargo.toml | 14 ++ packages/matchbook-messages/README.md | 4 + packages/matchbook-messages/src/lib.rs | 115 +++++++++++ 5 files changed, 394 insertions(+) create mode 100644 packages/matchbook-messages/.gitignore create mode 100644 packages/matchbook-messages/Cargo.lock create mode 100644 packages/matchbook-messages/Cargo.toml create mode 100644 packages/matchbook-messages/README.md create mode 100644 packages/matchbook-messages/src/lib.rs diff --git a/packages/matchbook-messages/.gitignore b/packages/matchbook-messages/.gitignore new file mode 100644 index 0000000..9f97022 --- /dev/null +++ b/packages/matchbook-messages/.gitignore @@ -0,0 +1 @@ +target/ \ No newline at end of file diff --git a/packages/matchbook-messages/Cargo.lock b/packages/matchbook-messages/Cargo.lock new file mode 100644 index 0000000..c60f0d7 --- /dev/null +++ b/packages/matchbook-messages/Cargo.lock @@ -0,0 +1,260 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "0.7.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7404febffaa47dac81aa44dba71523c9d069b1bdc50a77db41195149e17f68e5" +dependencies = [ + "memchr", +] + +[[package]] +name = "ansi_term" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" +dependencies = [ + "winapi", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "ctor" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8f45d9ad417bcef4817d614a501ab55cdd96a6fdb24f49aab89a54acfd66b19" +dependencies = [ + "quote", + "syn", +] + +[[package]] +name = "difference" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "524cbf6897b527295dff137cec09ecf3a05f4fddffd7dfcd1585403449e74198" + +[[package]] +name = "env_logger" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17392a012ea30ef05a610aa97dfb49496e71c9f676b27879922ea5bdf60d9d3f" +dependencies = [ + "log", + "regex", +] + +[[package]] +name = "getrandom" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "libc" +version = "0.2.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c" + +[[package]] +name = "log" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "matchbook-messages" +version = "0.1.0" +dependencies = [ + "pretty_assertions", + "quickcheck", + "serde", +] + +[[package]] +name = "memchr" +version = "2.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" + +[[package]] +name = "once_cell" +version = "1.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13bd41f508810a131401606d54ac32a467c97172d74ba7662562ebba5ad07fa0" + +[[package]] +name = "output_vt100" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53cdc5b785b7a58c5aad8216b3dfa114df64b0b06ae6e1501cef91df2fbdf8f9" +dependencies = [ + "winapi", +] + +[[package]] +name = "pretty_assertions" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f81e1644e1b54f5a68959a29aa86cde704219254669da328ecfdf6a1f09d427" +dependencies = [ + "ansi_term", + "ctor", + "difference", + "output_vt100", +] + +[[package]] +name = "proc-macro2" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quickcheck" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "588f6378e4dd99458b60ec275b4477add41ce4fa9f64dcba6f15adccb19b50d6" +dependencies = [ + "env_logger", + "log", + "rand", +] + +[[package]] +name = "quote" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" +dependencies = [ + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7" +dependencies = [ + "getrandom", +] + +[[package]] +name = "regex" +version = "1.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9251239e129e16308e70d853559389de218ac275b515068abc96829d05b948a" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", + "thread_local", +] + +[[package]] +name = "regex-syntax" +version = "0.6.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5eb417147ba9860a96cfe72a0b93bf88fee1744b5636ec99ab20c1aa9376581" + +[[package]] +name = "serde" +version = "1.0.123" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92d5161132722baa40d802cc70b15262b98258453e85e5d1d365c757c73869ae" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.123" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9391c295d64fc0abb2c556bad848f33cb8296276b1ad2677d1ae1ace4f258f31" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "syn" +version = "1.0.60" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c700597eca8a5a762beb35753ef6b94df201c81cca676604f547495a0d7f0081" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "thread_local" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8018d24e04c95ac8790716a5987d0fec4f8b27249ffa0f7d33f1369bdfb88cbd" +dependencies = [ + "once_cell", +] + +[[package]] +name = "unicode-xid" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" + +[[package]] +name = "wasi" +version = "0.10.2+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/packages/matchbook-messages/Cargo.toml b/packages/matchbook-messages/Cargo.toml new file mode 100644 index 0000000..fcbbc1c --- /dev/null +++ b/packages/matchbook-messages/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "matchbook-messages" +version = "0.1.0" +authors = ["Will Johnston "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dev-dependencies] +pretty_assertions = "0.6.1" +quickcheck = "1.0.3" + +[dependencies] +serde = {version="1.0", features=["derive"]} diff --git a/packages/matchbook-messages/README.md b/packages/matchbook-messages/README.md new file mode 100644 index 0000000..4717384 --- /dev/null +++ b/packages/matchbook-messages/README.md @@ -0,0 +1,4 @@ +# Matchbook-messages + +This crate contains types meant to be shared between matchbook services. + diff --git a/packages/matchbook-messages/src/lib.rs b/packages/matchbook-messages/src/lib.rs new file mode 100644 index 0000000..225b5c0 --- /dev/null +++ b/packages/matchbook-messages/src/lib.rs @@ -0,0 +1,115 @@ +use serde::{Deserialize, Serialize}; + +pub type ParticipantId = u64; +pub type Price = usize; +pub type Quantity = usize; +pub type SymbolOwned = String; +pub type SymbolRef<'a> = &'a str; + +#[derive(Debug, Clone, Copy, Serialize, Deserialize)] +pub enum Side { + Bid, + Ask, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Message { + pub service_id: ServiceId, + pub participant_id: ParticipantId, + pub kind: MessageKind, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub enum MessageKind { + LimitOrderSubmitRequest { + side: Side, + price: Price, + quantity: Quantity, + symbol: SymbolOwned, + }, + LimitOrderSubmitRequestAcknowledge { + side: Side, + price: Price, + quantity: Quantity, + symbol: SymbolOwned, + }, +} + +#[derive(Debug, Clone, Copy, Serialize, Deserialize)] +pub struct ServiceId { + kind: ServiceKind, + number: u16, +} + +impl std::str::FromStr for ServiceId { + type Err = Box; + fn from_str(s: &str) -> std::result::Result::Err> { + let mut split = s.split(":"); + let kind = if let Some(kind) = split.next() { + ServiceKind::from_str(kind)? + } else { + return Err(format!("incorrectly formatted ServiceId '{}'", s).into()); + }; + + let number = if let Some(num) = split.next() { + num.parse()? + } else { + return Err(format!("incorrectly formatted ServiceId '{}'", s).into()); + }; + + Ok(ServiceId { kind, number }) + } +} + +#[derive(Debug, Clone, Copy, Serialize, Deserialize)] +pub enum ServiceKind { + Port, + MatchingEngine, +} + +impl ServiceKind { + pub fn as_str<'a>(self) -> &'a str { + match self { + ServiceKind::Port => "port", + ServiceKind::MatchingEngine => "matching-engine", + } + } +} + +impl std::str::FromStr for ServiceKind { + type Err = Box; + + fn from_str(s: &str) -> std::result::Result::Err> { + match s { + "port" => Ok(ServiceKind::Port), + "matching-engine" => Ok(ServiceKind::MatchingEngine), + unknown => Err(format!("service kind '{}' is unknown", unknown).into()), + } + } +} + +#[cfg(test)] +mod test { + use super::*; + use quickcheck::quickcheck; + use std::str::FromStr; + + impl quickcheck::Arbitrary for ServiceKind { + fn arbitrary(g: &mut quickcheck::Gen) -> Self { + let choices = [ServiceKind::Port]; + g.choose(&choices).unwrap().clone() + } + } + + quickcheck! { + fn can_parse_service_identifier_from_str(kind: ServiceKind, n: u16) -> bool { + let s = format!("{}:{}", kind.as_str(), n); + ServiceId::from_str(&s).is_ok() + } + + fn cant_parse_unknown_service_identifier_from_str(n: u16) -> bool { + let s = format!("unknown:{}", n); + ServiceId::from_str(&s).is_err() + } + } +}