rust_order_book/
enums.rs

1//! Common enumerations used throughout the order book engine.
2//!
3//! This module defines order types, sides, statuses, and time-in-force policies
4//! used to describe and control order behavior.
5
6use serde::{Deserialize, Serialize};
7
8use crate::{
9    order::{OrderId, Price, Quantity},
10    LimitOrderOptions, MarketOrderOptions,
11};
12
13/// Represents the type of order being placed.
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
15#[serde(rename_all = "snake_case")]
16pub enum OrderType {
17    /// A market order that matches immediately against available liquidity.
18    Market,
19    /// A limit order that rests on the book until matched or canceled.
20    Limit,
21    // StopMarket,
22    // StopLimit,
23    // OCO
24}
25
26/// Represents the side of an order: buy or sell.
27#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
28#[serde(rename_all = "lowercase")]
29pub enum Side {
30    /// Buy side (bids)
31    Buy,
32    /// Sell side (asks)
33    Sell,
34}
35
36/// Specifies how long an order remains active before it is executed or expires.
37#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
38#[serde(rename_all = "UPPERCASE")]
39pub enum TimeInForce {
40    /// Good-til-cancelled: the order remains until manually canceled.
41    GTC,
42    /// Immediate-or-cancel: the order executes partially or fully, then cancels.
43    IOC,
44    /// Fill-or-kill: the order must fill entirely or be canceled.
45    FOK,
46}
47
48/// Represents the current status of an order.
49#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
50#[serde(rename_all = "snake_case")]
51pub enum OrderStatus {
52    /// The order has been accepted but not yet matched.
53    New,
54    /// The order was partially matched, some quantity remains.
55    PartiallyFilled,
56    /// The order was completely matched.
57    Filled,
58    /// The order was canceled before being fully filled.
59    Canceled,
60    /// The order was rejected due to invalid input or constraints.
61    Rejected,
62}
63
64/// Represents the type of operation recorded in the order book journal.
65///
66/// This enum provides a type-safe and explicit way to indicate which kind of
67/// operation was logged—such as a market order, limit order, or cancellation.
68#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
69#[serde(rename_all = "snake_case")]
70pub enum JournalOp {
71    /// Market order
72    Market,
73    /// Limit order
74    Limit,
75    /// Modify order (cancel and create new order with updated price and quantity)
76    Modify,
77    /// Cancel (delete) order
78    Cancel,
79}
80
81#[derive(Debug, Clone, Copy, PartialEq, Eq)]
82pub enum OrderOptions {
83    Market(MarketOrderOptions),
84    Limit(LimitOrderOptions),
85    Modify { id: OrderId, price: Option<Price>, quantity: Option<Quantity> },
86    Cancel(OrderId),
87}
88
89#[cfg(test)]
90mod tests {
91    use super::*;
92    use serde_json::to_string;
93
94    #[test]
95    fn test_enum_serialization() {
96        assert_eq!(to_string(&Side::Buy).unwrap(), "\"buy\"");
97        assert_eq!(to_string(&Side::Sell).unwrap(), "\"sell\"");
98
99        assert_eq!(to_string(&OrderType::Market).unwrap(), "\"market\"");
100        assert_eq!(to_string(&OrderType::Limit).unwrap(), "\"limit\"");
101
102        assert_eq!(to_string(&OrderStatus::New).unwrap(), "\"new\"");
103        assert_eq!(to_string(&OrderStatus::PartiallyFilled).unwrap(), "\"partially_filled\"");
104        assert_eq!(to_string(&OrderStatus::Filled).unwrap(), "\"filled\"");
105        assert_eq!(to_string(&OrderStatus::Canceled).unwrap(), "\"canceled\"");
106        assert_eq!(to_string(&OrderStatus::Rejected).unwrap(), "\"rejected\"");
107
108        assert_eq!(to_string(&TimeInForce::GTC).unwrap(), "\"GTC\"");
109        assert_eq!(to_string(&TimeInForce::IOC).unwrap(), "\"IOC\"");
110        assert_eq!(to_string(&TimeInForce::FOK).unwrap(), "\"FOK\"");
111
112        assert_eq!(to_string(&JournalOp::Market).unwrap(), "\"market\"");
113        assert_eq!(to_string(&JournalOp::Limit).unwrap(), "\"limit\"");
114        assert_eq!(to_string(&JournalOp::Cancel).unwrap(), "\"cancel\"");
115    }
116}