1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//! Domain errors. Every variant carries the concrete values that explain
//! the failure (ARCHITECTURE.md section 8.1): an opaque variant name is
//! never the whole diagnostic.
use core::fmt;
use crate::types::OrderId;
/// Errors returned by book construction and by every book command.
///
/// Commands that return an error have mutated nothing: no order, level
/// aggregate, bitmap bit, lookup entry, or sequence number changes when
/// any of these is returned (INV-ORD-4).
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum BookError {
/// `BookConfig` failed validation at construction.
InvalidConfig {
/// The field that failed validation.
field: &'static str,
/// The rule the field violated.
rule: &'static str,
},
/// An order id of zero was supplied; zero is the lookup sentinel.
InvalidOrderId {
/// The offending order id (always zero).
order_id: OrderId,
},
/// An active order already uses this id.
DuplicateOrderId {
/// The conflicting order id.
order_id: OrderId,
},
/// A quantity of zero was supplied on add or amend.
InvalidQuantity {
/// The offending quantity (always zero).
qty: u64,
},
/// A price failed the tick grid rules of ARCHITECTURE.md section 3.2.
InvalidPriceTick {
/// The raw price supplied, in quote units.
price: u64,
/// Configured quote units per tick.
tick_size: u64,
/// Configured tick value of grid index 0.
grid_start_tick: u64,
/// Configured number of ticks on the grid.
grid_len: u32,
},
/// Cancel or amend named an order that is not live.
OrderNotFound {
/// The order id that was not found.
order_id: OrderId,
},
/// The arena has no free slot for an order that would rest.
CapacityFull {
/// Configured arena capacity.
max_orders: u32,
},
/// The lookup table reached its 50 percent load cap. Defensive: the
/// arena cap is enforced first, so this guards the table invariant
/// directly rather than relying on that ordering.
LookupCapacityFull {
/// Table capacity in buckets.
capacity: usize,
/// Entries present when the insert was refused.
len: usize,
},
/// A Post-Only order price would cross the opposite best price.
PostOnlyWouldCross {
/// The raw price of the incoming order.
price: u64,
/// The raw opposite best price it would have crossed.
opposite_best: u64,
},
/// A Fill-or-Kill order could not be filled in full.
FokInsufficientLiquidity {
/// Quantity the order requested.
requested: u64,
/// STP-adjusted executable quantity found in preflight.
available: u64,
},
/// An amend would leave the book crossed.
AmendWouldCross {
/// The order being amended.
order_id: OrderId,
/// The raw new price requested.
new_price: u64,
/// The raw opposite best price it would cross.
opposite_best: u64,
},
/// The caller event buffer is smaller than the preflight worst-case
/// event count for this command. Returned before any mutation, so
/// the identical command can be retried with a larger buffer.
EventBufferFull {
/// Worst-case events the command could emit.
needed: usize,
/// Free slots the buffer actually had.
available: usize,
},
/// A checked aggregate operation overflowed. Defensive guard:
/// aggregates are sized so this is unreachable in a valid book.
ArithmeticOverflow {
/// What was being computed when the overflow was detected.
what: &'static str,
},
}
impl fmt::Display for BookError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidConfig { field, rule } => {
write!(
f,
"invalid book configuration: field {field} violates rule: {rule}"
)
}
Self::InvalidOrderId { order_id } => {
write!(f, "invalid order id {}: zero is reserved", order_id.0)
}
Self::DuplicateOrderId { order_id } => {
write!(
f,
"duplicate order id {}: an active order already uses it",
order_id.0
)
}
Self::InvalidQuantity { qty } => {
write!(f, "invalid quantity {qty}: quantities must be positive")
}
Self::InvalidPriceTick {
price,
tick_size,
grid_start_tick,
grid_len,
} => write!(
f,
"invalid price tick: price {price} with tick size {tick_size} does not land on the grid starting at tick {grid_start_tick} with {grid_len} ticks"
),
Self::OrderNotFound { order_id } => {
write!(f, "order not found: no live order with id {}", order_id.0)
}
Self::CapacityFull { max_orders } => {
write!(
f,
"book capacity full: arena holds at most {max_orders} live orders"
)
}
Self::LookupCapacityFull { capacity, len } => write!(
f,
"lookup table at load cap: {len} entries in {capacity} buckets (cap is 50 percent)"
),
Self::PostOnlyWouldCross {
price,
opposite_best,
} => write!(
f,
"post-only order at price {price} would cross the opposite best price {opposite_best}"
),
Self::FokInsufficientLiquidity {
requested,
available,
} => write!(
f,
"fill-or-kill order requested {requested} but only {available} is executable"
),
Self::AmendWouldCross {
order_id,
new_price,
opposite_best,
} => write!(
f,
"amend of order {} to price {new_price} would cross the opposite best price {opposite_best}",
order_id.0
),
Self::EventBufferFull { needed, available } => write!(
f,
"event buffer full: command needs up to {needed} event slots, buffer has {available}"
),
Self::ArithmeticOverflow { what } => {
write!(f, "arithmetic overflow while computing {what}")
}
}
}
}
impl core::error::Error for BookError {}