orderbook_rs/orderbook/
error.rs

1//! Order book error types
2
3use pricelevel::{PriceLevelError, Side};
4use std::fmt;
5
6/// Errors that can occur within the OrderBook
7#[derive(Debug)]
8pub enum OrderBookError {
9    /// Error from underlying price level operations
10    PriceLevelError(PriceLevelError),
11
12    /// Order not found in the book
13    OrderNotFound(String),
14
15    /// Invalid price level
16    InvalidPriceLevel(u64),
17
18    /// Price crossing (bid >= ask)
19    PriceCrossing {
20        /// Price that would cause crossing
21        price: u64,
22        /// Side of the order
23        side: Side,
24        /// Best opposite price
25        opposite_price: u64,
26    },
27
28    /// Insufficient liquidity for market order
29    InsufficientLiquidity {
30        /// The side of the market order
31        side: Side,
32        /// Quantity requested
33        requested: u64,
34        /// Quantity available
35        available: u64,
36    },
37
38    /// Operation not permitted for specified order type
39    InvalidOperation {
40        /// Description of the error
41        message: String,
42    },
43}
44
45impl fmt::Display for OrderBookError {
46    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47        match self {
48            OrderBookError::PriceLevelError(err) => write!(f, "Price level error: {}", err),
49            OrderBookError::OrderNotFound(id) => write!(f, "Order not found: {}", id),
50            OrderBookError::InvalidPriceLevel(price) => write!(f, "Invalid price level: {}", price),
51            OrderBookError::PriceCrossing {
52                price,
53                side,
54                opposite_price,
55            } => {
56                write!(
57                    f,
58                    "Price crossing: {} {} would cross opposite at {}",
59                    side, price, opposite_price
60                )
61            }
62            OrderBookError::InsufficientLiquidity {
63                side,
64                requested,
65                available,
66            } => {
67                write!(
68                    f,
69                    "Insufficient liquidity for {} order: requested {}, available {}",
70                    side, requested, available
71                )
72            }
73            OrderBookError::InvalidOperation { message } => {
74                write!(f, "Invalid operation: {}", message)
75            }
76        }
77    }
78}
79
80impl std::error::Error for OrderBookError {}
81
82impl From<PriceLevelError> for OrderBookError {
83    fn from(err: PriceLevelError) -> Self {
84        OrderBookError::PriceLevelError(err)
85    }
86}