rustkernel_orderbook/messages.rs
1//! Message types for order matching engine kernel.
2//!
3//! Input/output message types for the `BatchKernel` trait implementations
4//! and Ring kernel messages for K2K communication.
5//!
6//! Note: Full RingMessage support requires FR-1 (KernelMessage ↔ RingMessage bridge)
7//! from the RustCompute feature request. See docs/RUSTCOMPUTE_FEATURE_REQUEST.md
8
9use rustkernel_derive::KernelMessage;
10use serde::{Deserialize, Serialize};
11
12use crate::types::{L2Snapshot, MatchResult, Order, Price, Quantity};
13
14// ============================================================================
15// Submit Order Messages
16// ============================================================================
17
18/// Input for submitting a single order.
19///
20/// Ring message type_id: 1000 (OrderMatching domain)
21#[derive(Debug, Clone, Serialize, Deserialize, KernelMessage)]
22#[message(type_id = 1000, domain = "OrderMatching")]
23pub struct SubmitOrderInput {
24 /// Order to submit.
25 pub order: Order,
26}
27
28impl SubmitOrderInput {
29 /// Create a new submit order input.
30 pub fn new(order: Order) -> Self {
31 Self { order }
32 }
33}
34
35/// Output from submitting a single order.
36///
37/// Ring message type_id: 1001 (OrderMatching domain)
38#[derive(Debug, Clone, Serialize, Deserialize, KernelMessage)]
39#[message(type_id = 1001, domain = "OrderMatching")]
40pub struct SubmitOrderOutput {
41 /// Match result from the order submission.
42 pub result: MatchResult,
43 /// Computation time in microseconds.
44 pub compute_time_us: u64,
45}
46
47// ============================================================================
48// Batch Order Messages
49// ============================================================================
50
51/// Input for processing a batch of orders.
52///
53/// Ring message type_id: 1010 (OrderMatching domain)
54#[derive(Debug, Clone, Serialize, Deserialize, KernelMessage)]
55#[message(type_id = 1010, domain = "OrderMatching")]
56pub struct BatchOrderInput {
57 /// Orders to process.
58 pub orders: Vec<Order>,
59}
60
61impl BatchOrderInput {
62 /// Create a new batch order input.
63 pub fn new(orders: Vec<Order>) -> Self {
64 Self { orders }
65 }
66
67 /// Create from a single order.
68 pub fn single(order: Order) -> Self {
69 Self {
70 orders: vec![order],
71 }
72 }
73}
74
75/// Output from processing a batch of orders.
76///
77/// Ring message type_id: 1011 (OrderMatching domain)
78#[derive(Debug, Clone, Serialize, Deserialize, KernelMessage)]
79#[message(type_id = 1011, domain = "OrderMatching")]
80pub struct BatchOrderOutput {
81 /// Match results for each order.
82 pub results: Vec<MatchResult>,
83 /// Total trades generated.
84 pub total_trades: usize,
85 /// Computation time in microseconds.
86 pub compute_time_us: u64,
87}
88
89// ============================================================================
90// Cancel Order Messages
91// ============================================================================
92
93/// Input for canceling an order.
94///
95/// Ring message type_id: 1020 (OrderMatching domain)
96#[derive(Debug, Clone, Serialize, Deserialize, KernelMessage)]
97#[message(type_id = 1020, domain = "OrderMatching")]
98pub struct CancelOrderInput {
99 /// Order ID to cancel.
100 pub order_id: u64,
101}
102
103impl CancelOrderInput {
104 /// Create a new cancel order input.
105 pub fn new(order_id: u64) -> Self {
106 Self { order_id }
107 }
108}
109
110/// Output from canceling an order.
111///
112/// Ring message type_id: 1021 (OrderMatching domain)
113#[derive(Debug, Clone, Serialize, Deserialize, KernelMessage)]
114#[message(type_id = 1021, domain = "OrderMatching")]
115pub struct CancelOrderOutput {
116 /// Canceled order (if found and active).
117 pub canceled_order: Option<Order>,
118 /// Whether the cancellation succeeded.
119 pub success: bool,
120 /// Computation time in microseconds.
121 pub compute_time_us: u64,
122}
123
124// ============================================================================
125// Modify Order Messages
126// ============================================================================
127
128/// Input for modifying an order.
129///
130/// Ring message type_id: 1030 (OrderMatching domain)
131#[derive(Debug, Clone, Serialize, Deserialize, KernelMessage)]
132#[message(type_id = 1030, domain = "OrderMatching")]
133pub struct ModifyOrderInput {
134 /// Order ID to modify.
135 pub order_id: u64,
136 /// New price (optional).
137 pub new_price: Option<Price>,
138 /// New quantity (optional).
139 pub new_quantity: Option<Quantity>,
140}
141
142impl ModifyOrderInput {
143 /// Create a new modify order input.
144 pub fn new(order_id: u64, new_price: Option<Price>, new_quantity: Option<Quantity>) -> Self {
145 Self {
146 order_id,
147 new_price,
148 new_quantity,
149 }
150 }
151
152 /// Create to modify price only.
153 pub fn modify_price(order_id: u64, new_price: Price) -> Self {
154 Self {
155 order_id,
156 new_price: Some(new_price),
157 new_quantity: None,
158 }
159 }
160
161 /// Create to modify quantity only.
162 pub fn modify_quantity(order_id: u64, new_quantity: Quantity) -> Self {
163 Self {
164 order_id,
165 new_price: None,
166 new_quantity: Some(new_quantity),
167 }
168 }
169}
170
171/// Output from modifying an order.
172///
173/// Ring message type_id: 1031 (OrderMatching domain)
174#[derive(Debug, Clone, Serialize, Deserialize, KernelMessage)]
175#[message(type_id = 1031, domain = "OrderMatching")]
176pub struct ModifyOrderOutput {
177 /// Match result if modification succeeded (may match against book).
178 pub result: Option<MatchResult>,
179 /// Whether the modification succeeded.
180 pub success: bool,
181 /// Computation time in microseconds.
182 pub compute_time_us: u64,
183}
184
185// ============================================================================
186// Order Book Snapshot Messages
187// ============================================================================
188
189/// Input for getting an order book snapshot.
190///
191/// Ring message type_id: 1040 (OrderMatching domain)
192#[derive(Debug, Clone, Serialize, Deserialize, KernelMessage)]
193#[message(type_id = 1040, domain = "OrderMatching")]
194pub struct GetSnapshotInput {
195 /// Symbol ID.
196 pub symbol_id: u32,
197 /// Depth (number of price levels).
198 pub depth: usize,
199}
200
201impl GetSnapshotInput {
202 /// Create a new snapshot input.
203 pub fn new(symbol_id: u32, depth: usize) -> Self {
204 Self { symbol_id, depth }
205 }
206
207 /// Create with default depth (10 levels).
208 pub fn with_default_depth(symbol_id: u32) -> Self {
209 Self {
210 symbol_id,
211 depth: 10,
212 }
213 }
214}
215
216/// Output from getting an order book snapshot.
217///
218/// Ring message type_id: 1041 (OrderMatching domain)
219#[derive(Debug, Clone, Serialize, Deserialize, KernelMessage)]
220#[message(type_id = 1041, domain = "OrderMatching")]
221pub struct GetSnapshotOutput {
222 /// L2 order book snapshot (if symbol exists).
223 pub snapshot: Option<L2Snapshot>,
224 /// Computation time in microseconds.
225 pub compute_time_us: u64,
226}