Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Renamed matchbook-types-to-matchbook-messages #39

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/matchbook-messages/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target/
260 changes: 260 additions & 0 deletions packages/matchbook-messages/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions packages/matchbook-messages/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "matchbook-messages"
version = "0.1.0"
authors = ["Will Johnston <[email protected]>"]
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"]}
4 changes: 4 additions & 0 deletions packages/matchbook-messages/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Matchbook-messages

This crate contains types meant to be shared between matchbook services.

115 changes: 115 additions & 0 deletions packages/matchbook-messages/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<dyn std::error::Error>;
fn from_str(s: &str) -> std::result::Result<Self, <Self as std::str::FromStr>::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<dyn std::error::Error>;

fn from_str(s: &str) -> std::result::Result<Self, <Self as std::str::FromStr>::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()
}
}
}