pricelevel 0.7.0

A high-performance, lock-free price level implementation for limit order books in Rust. This library provides the building blocks for creating efficient trading systems with support for multiple order types and concurrent access patterns.
Documentation
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#[cfg(test)]
mod tests {
    use crate::UuidGenerator;
    use crate::execution::list::TransactionList;
    use crate::execution::transaction::Transaction;
    use crate::orders::{OrderId, Side};
    use std::str::FromStr;
    use uuid::Uuid;

    fn create_test_transactions() -> Vec<Transaction> {
        let namespace = Uuid::parse_str("6ba7b810-9dad-11d1-80b4-00c04fd430c8").unwrap();
        let transaction_id_generator = UuidGenerator::new(namespace);
        vec![
            Transaction {
                transaction_id: transaction_id_generator.next(),
                taker_order_id: OrderId::from_u64(1),
                maker_order_id: OrderId::from_u64(2),
                price: 10000,
                quantity: 5,
                taker_side: Side::Buy,
                timestamp: 1616823000000,
            },
            Transaction {
                transaction_id: transaction_id_generator.next(),
                taker_order_id: OrderId::from_u64(3),
                maker_order_id: OrderId::from_u64(4),
                price: 10001,
                quantity: 10,
                taker_side: Side::Sell,
                timestamp: 1616823000001,
            },
        ]
    }

    #[test]
    fn test_transaction_list_new() {
        let list = TransactionList::new();
        assert_eq!(list.transactions.len(), 0);
    }

    #[test]
    fn test_transaction_list_from_vec() {
        let transactions = create_test_transactions();
        let list = TransactionList::from_vec(transactions.clone());
        assert_eq!(list.transactions, transactions);
    }

    #[test]
    fn test_transaction_list_add() {
        let mut list = TransactionList::new();
        let transaction = create_test_transactions()[0];
        list.add(transaction);

        assert_eq!(list.transactions.len(), 1);

        let uuid = Uuid::parse_str("6af613b6-569c-5c22-9c37-2ed93f31d3af").unwrap();
        assert_eq!(list.transactions[0].transaction_id, uuid);
    }

    #[test]
    fn test_transaction_list_as_vec() {
        let transactions = create_test_transactions();
        let list = TransactionList::from_vec(transactions.clone());
        let vec_ref = list.as_vec();

        assert_eq!(vec_ref, &transactions);
    }

    #[test]
    fn test_transaction_list_into_vec() {
        let transactions = create_test_transactions();
        let list = TransactionList::from_vec(transactions.clone());
        let vec = list.into_vec();

        assert_eq!(vec, transactions);
    }

    #[test]
    fn test_transaction_list_display() {
        let transactions = create_test_transactions();
        let list = TransactionList::from_vec(transactions);
        let display_str = list.to_string();

        assert!(display_str.starts_with("Transactions:["));
        assert!(display_str.ends_with("]"));
        assert!(display_str.contains("transaction_id=6af613b6-569c-5c22-9c37-2ed93f31d3af"));
        assert!(display_str.contains("transaction_id=b04965e6-a9bb-591f-8f8a-1adcb2c8dc39"));
    }

    #[test]
    fn test_transaction_list_from_str_valid() {
        let input = "Transactions:[Transaction:transaction_id=6af613b6-569c-5c22-9c37-2ed93f31d3af;taker_order_id=00000000-0000-0001-0000-000000000000;maker_order_id=00000000-0000-0002-0000-000000000000;price=10000;quantity=5;taker_side=BUY;timestamp=1616823000000,Transaction:transaction_id=6af613b6-569c-5c22-9c37-2ed93f31d3b0;taker_order_id=00000000-0000-0003-0000-000000000000;maker_order_id=00000000-0000-0004-0000-000000000000;price=10001;quantity=10;taker_side=SELL;timestamp=1616823000001]";
        let list = TransactionList::from_str(input).unwrap();

        assert_eq!(list.transactions.len(), 2);
        let uuid = Uuid::parse_str("6af613b6-569c-5c22-9c37-2ed93f31d3af").unwrap();

        assert_eq!(list.transactions[0].transaction_id, uuid);
        assert_eq!(list.transactions[0].taker_order_id, OrderId::from_u64(1));
        assert_eq!(list.transactions[0].maker_order_id, OrderId::from_u64(2));
        assert_eq!(list.transactions[0].price, 10000);
        assert_eq!(list.transactions[0].quantity, 5);
        assert_eq!(list.transactions[0].taker_side, Side::Buy);
        assert_eq!(list.transactions[0].timestamp, 1616823000000);

        let uuid = Uuid::parse_str("6af613b6-569c-5c22-9c37-2ed93f31d3b0").unwrap();

        assert_eq!(list.transactions[1].transaction_id, uuid);
        assert_eq!(list.transactions[1].taker_order_id, OrderId::from_u64(3));
        assert_eq!(list.transactions[1].maker_order_id, OrderId::from_u64(4));
        assert_eq!(list.transactions[1].price, 10001);
        assert_eq!(list.transactions[1].quantity, 10);
        assert_eq!(list.transactions[1].taker_side, Side::Sell);
        assert_eq!(list.transactions[1].timestamp, 1616823000001);
    }

    #[test]
    fn test_transaction_list_from_str_empty() {
        let input = "Transactions:[]";
        let list = TransactionList::from_str(input).unwrap();

        assert_eq!(list.transactions.len(), 0);
    }

    #[test]
    fn test_transaction_list_from_str_invalid_format() {
        let input = "Transacciones:[]";
        let result = TransactionList::from_str(input);
        assert!(result.is_err());

        let input = "Transactions:";
        let result = TransactionList::from_str(input);
        assert!(result.is_err());

        let input = "[Transaction:transaction_id=6af613b6-569c-5c22-9c37-2ed93f31d3af;taker_order_id=1;maker_order_id=2;price=10000;quantity=5;taker_side=BUY;timestamp=1616823000000]";
        let result = TransactionList::from_str(input);
        assert!(result.is_err());
    }

    #[test]
    fn test_transaction_list_from_str_invalid_transaction() {
        let input = "Transactions:[Transaction:transaction_id=6af613b6-569c-5c22-9c37-2ed93f31d3af;taker_order_id=1;maker_order_id=2;price=10000;quantity=5;taker_side=BUY;timestamp=1616823000000,Transaction:transaction_id=invalid;taker_order_id=3;maker_order_id=4;price=10001;quantity=10;taker_side=SELL;timestamp=1616823000001]";
        let result = TransactionList::from_str(input);
        assert!(result.is_err());
    }

    #[test]
    fn test_transaction_list_round_trip() {
        let original_transactions = create_test_transactions();
        let original = TransactionList::from_vec(original_transactions);

        let string_representation = original.to_string();
        let parsed = TransactionList::from_str(&string_representation).unwrap();

        assert_eq!(parsed.transactions.len(), original.transactions.len());

        for i in 0..parsed.transactions.len() {
            assert_eq!(
                parsed.transactions[i].transaction_id,
                original.transactions[i].transaction_id
            );
            assert_eq!(
                parsed.transactions[i].taker_order_id,
                original.transactions[i].taker_order_id
            );
            assert_eq!(
                parsed.transactions[i].maker_order_id,
                original.transactions[i].maker_order_id
            );
            assert_eq!(parsed.transactions[i].price, original.transactions[i].price);
            assert_eq!(
                parsed.transactions[i].quantity,
                original.transactions[i].quantity
            );
            assert_eq!(
                parsed.transactions[i].taker_side,
                original.transactions[i].taker_side
            );
            assert_eq!(
                parsed.transactions[i].timestamp,
                original.transactions[i].timestamp
            );
        }
    }

    #[test]
    fn test_from_into_conversions() {
        // Vec<Transaction> -> TransactionList
        let transactions = create_test_transactions();
        let list: TransactionList = transactions.clone().into();
        assert_eq!(list.transactions, transactions);

        // TransactionList -> Vec<Transaction>
        let list = TransactionList::from_vec(transactions.clone());
        let vec: Vec<Transaction> = list.into();
        assert_eq!(vec, transactions);
    }

    // In execution/list.rs test module or in a separate test file

    #[test]
    fn test_transaction_list_parsing_edge_cases() {
        // Test empty transactions list
        let input = "Transactions:[]";
        let list = TransactionList::from_str(input).unwrap();
        assert_eq!(list.len(), 0);

        // Test single transaction with complex fields
        let input = "Transactions:[Transaction:transaction_id=6ba7b810-9dad-11d1-80b4-00c04fd430c8;taker_order_id=00000000-0000-0001-0000-000000000000;maker_order_id=00000000-0000-0002-0000-000000000000;price=10000;quantity=5;taker_side=BUY;timestamp=1616823000000]";
        let list = TransactionList::from_str(input).unwrap();
        assert_eq!(list.len(), 1);

        // Test with nested brackets in transaction fields
        let input = "Transactions:[Transaction:transaction_id=6ba7b810-9dad-11d1-80b4-00c04fd430c8;taker_order_id=[00000000-0000-0001-0000-000000000000];maker_order_id=00000000-0000-0002-0000-000000000000;price=10000;quantity=5;taker_side=BUY;timestamp=1616823000000]";

        // This might fail depending on the implementation details
        let result = TransactionList::from_str(input);
        // If it fails, assert that it's due to the expected reason
        if let Err(err) = result {
            let err_string = format!("{err:?}");
            assert!(err_string.contains("Invalid") || err_string.contains("taker_order_id"));
        }
    }

    #[test]
    fn test_transaction_list_from_vec_empty() {
        let empty_vec: Vec<Transaction> = Vec::new();
        let list = TransactionList::from_vec(empty_vec);

        assert_eq!(list.len(), 0);
        assert!(list.is_empty());

        // Test display output for empty list
        assert_eq!(list.to_string(), "Transactions:[]");

        // Test round-trip via string parsing
        let parsed = TransactionList::from_str(&list.to_string()).unwrap();
        assert_eq!(parsed.len(), 0);
    }

    #[test]
    fn test_transaction_list_parsing_errors() {
        // Test invalid prefix
        let input = "InvalidPrefix:[]";
        let result = TransactionList::from_str(input);
        assert!(result.is_err());

        // Test missing closing bracket
        let input = "Transactions:[";
        let result = TransactionList::from_str(input);
        assert!(result.is_err());

        // Test unbalanced brackets
        let input =
            "Transactions:[Transaction:transaction_id=6ba7b810-9dad-11d1-80b4-00c04fd430c8;]";
        let result = TransactionList::from_str(input);
        assert!(result.is_err() || result.unwrap().len() == 1);
    }
}

#[cfg(test)]
mod transaction_list_serialization_tests {

    use crate::UuidGenerator;
    use crate::execution::list::TransactionList;
    use crate::execution::transaction::Transaction;
    use crate::orders::{OrderId, Side};
    use std::str::FromStr;
    use uuid::Uuid;

    fn create_test_transactions() -> Vec<Transaction> {
        let namespace = Uuid::parse_str("6ba7b810-9dad-11d1-80b4-00c04fd430c8").unwrap();
        let transaction_id_generator = UuidGenerator::new(namespace);
        vec![
            Transaction {
                transaction_id: transaction_id_generator.next(),
                taker_order_id: OrderId::from_u64(1),
                maker_order_id: OrderId::from_u64(2),
                price: 10000,
                quantity: 5,
                taker_side: Side::Buy,
                timestamp: 1616823000000,
            },
            Transaction {
                transaction_id: transaction_id_generator.next(),
                taker_order_id: OrderId::from_u64(3),
                maker_order_id: OrderId::from_u64(4),
                price: 10001,
                quantity: 10,
                taker_side: Side::Sell,
                timestamp: 1616823000001,
            },
        ]
    }

    fn create_test_transaction_list() -> TransactionList {
        TransactionList::from_vec(create_test_transactions())
    }

    #[test]
    fn test_custom_display_format() {
        let list = create_test_transaction_list();
        let display_str = list.to_string();

        assert!(display_str.starts_with("Transactions:["));
        assert!(display_str.ends_with("]"));

        assert!(display_str.contains("transaction_id=6af613b6-569c-5c22-9c37-2ed93f31d3af"));
        assert!(display_str.contains("taker_order_id=00000000-0000-0001-0000-000000000000"));
        assert!(display_str.contains("maker_order_id=00000000-0000-0002-0000-000000000000"));
        assert!(display_str.contains("transaction_id=b04965e6-a9bb-591f-8f8a-1adcb2c8dc39"));
        assert!(display_str.contains("taker_order_id=00000000-0000-0003-0000-000000000000"));
        assert!(display_str.contains("maker_order_id=00000000-0000-0004-0000-000000000000"));
    }

    #[test]
    fn test_empty_list_display() {
        let list = TransactionList::new();
        let display_str = list.to_string();

        assert_eq!(display_str, "Transactions:[]");
    }

    #[test]
    fn test_from_str_valid() {
        let input = "Transactions:[Transaction:transaction_id=6af613b6-569c-5c22-9c37-2ed93f31d3af;taker_order_id=00000000-0000-0001-0000-000000000000;maker_order_id=00000000-0000-0002-0000-000000000000;price=10000;quantity=5;taker_side=BUY;timestamp=1616823000000,Transaction:transaction_id=b04965e6-a9bb-591f-8f8a-1adcb2c8dc39;taker_order_id=00000000-0000-0003-0000-000000000000;maker_order_id=00000000-0000-0004-0000-000000000000;price=10001;quantity=10;taker_side=SELL;timestamp=1616823000001]";
        let list = TransactionList::from_str(input).unwrap();

        assert_eq!(list.len(), 2);

        let tx1 = &list.transactions[0];
        let uuid = Uuid::parse_str("6af613b6-569c-5c22-9c37-2ed93f31d3af").unwrap();
        assert_eq!(tx1.transaction_id, uuid);
        assert_eq!(tx1.taker_order_id, OrderId::from_u64(1));
        assert_eq!(tx1.maker_order_id, OrderId::from_u64(2));
        assert_eq!(tx1.price, 10000);
        assert_eq!(tx1.quantity, 5);
        assert_eq!(tx1.taker_side, Side::Buy);
        assert_eq!(tx1.timestamp, 1616823000000);

        let tx2 = &list.transactions[1];
        let uuid = Uuid::parse_str("b04965e6-a9bb-591f-8f8a-1adcb2c8dc39").unwrap();
        assert_eq!(tx2.transaction_id, uuid);
        assert_eq!(tx2.taker_order_id, OrderId::from_u64(3));
        assert_eq!(tx2.maker_order_id, OrderId::from_u64(4));
        assert_eq!(tx2.price, 10001);
        assert_eq!(tx2.quantity, 10);
        assert_eq!(tx2.taker_side, Side::Sell);
        assert_eq!(tx2.timestamp, 1616823000001);
    }

    #[test]
    fn test_from_str_empty() {
        let input = "Transactions:[]";
        let list = TransactionList::from_str(input).unwrap();

        assert_eq!(list.len(), 0);
        assert!(list.is_empty());
    }

    #[test]
    fn test_from_str_invalid_format() {
        let input = "InvalidFormat";
        let result = TransactionList::from_str(input);
        assert!(result.is_err());

        let input = "TransactionsList:[Transaction:transaction_id=6af613b6-569c-5c22-9c37-2ed93f31d3af;taker_order_id=1;maker_order_id=2;price=10000;quantity=5;taker_side=BUY;timestamp=1616823000000]";
        let result = TransactionList::from_str(input);
        assert!(result.is_err());

        let input = "Transactions:";
        let result = TransactionList::from_str(input);
        assert!(result.is_err());

        let input = "Transactions:[Transaction:transaction_id=6af613b6-569c-5c22-9c37-2ed93f31d3af;taker_order_id=1;maker_order_id=2;price=10000;quantity=5;taker_side=BUY;timestamp=1616823000000";
        let result = TransactionList::from_str(input);
        assert!(result.is_err());
    }

    #[test]
    fn test_from_str_invalid_transaction() {
        let input = "Transactions:[Transaction:transaction_id=abc;taker_order_id=1;maker_order_id=2;price=10000;quantity=5;taker_side=BUY;timestamp=1616823000000]";
        let result = TransactionList::from_str(input);
        assert!(result.is_err());

        let input = "Transactions:[InvalidTransaction]";
        let result = TransactionList::from_str(input);
        assert!(result.is_err());
    }

    #[test]
    fn test_custom_serialization_round_trip() {
        let original = create_test_transaction_list();
        let string_representation = original.to_string();
        let parsed = TransactionList::from_str(&string_representation).unwrap();

        assert_eq!(parsed.len(), original.len());

        for i in 0..parsed.len() {
            assert_eq!(
                parsed.transactions[i].transaction_id,
                original.transactions[i].transaction_id
            );
            assert_eq!(
                parsed.transactions[i].taker_order_id,
                original.transactions[i].taker_order_id
            );
            assert_eq!(
                parsed.transactions[i].maker_order_id,
                original.transactions[i].maker_order_id
            );
            assert_eq!(parsed.transactions[i].price, original.transactions[i].price);
            assert_eq!(
                parsed.transactions[i].quantity,
                original.transactions[i].quantity
            );
            assert_eq!(
                parsed.transactions[i].taker_side,
                original.transactions[i].taker_side
            );
            assert_eq!(
                parsed.transactions[i].timestamp,
                original.transactions[i].timestamp
            );
        }
    }

    #[test]
    fn test_from_vec_and_into_vec() {
        let transactions = create_test_transactions();
        let list = TransactionList::from_vec(transactions.clone());
        assert_eq!(list.len(), transactions.len());

        let result_vec = list.into_vec();
        assert_eq!(result_vec.len(), transactions.len());

        for i in 0..result_vec.len() {
            assert_eq!(result_vec[i].transaction_id, transactions[i].transaction_id);
        }
    }

    #[test]
    fn test_from_into_trait_implementations() {
        let transactions = create_test_transactions();

        let list: TransactionList = transactions.clone().into();
        assert_eq!(list.len(), transactions.len());

        let list = TransactionList::from_vec(transactions.clone());
        let vec: Vec<Transaction> = list.into();
        assert_eq!(vec.len(), transactions.len());

        for i in 0..vec.len() {
            assert_eq!(vec[i].transaction_id, transactions[i].transaction_id);
        }
    }

    #[test]
    fn test_add_transaction() {
        let mut list = TransactionList::new();
        assert_eq!(list.len(), 0);

        let tx1 = create_test_transactions()[0];
        list.add(tx1);
        assert_eq!(list.len(), 1);
        let uuid = Uuid::parse_str("6af613b6-569c-5c22-9c37-2ed93f31d3af").unwrap();
        assert_eq!(list.transactions[0].transaction_id, uuid);

        let tx2 = create_test_transactions()[1];
        list.add(tx2);
        assert_eq!(list.len(), 2);
        let uuid = Uuid::parse_str("b04965e6-a9bb-591f-8f8a-1adcb2c8dc39").unwrap();
        assert_eq!(list.transactions[1].transaction_id, uuid);
    }

    #[test]
    fn test_as_vec() {
        let list = create_test_transaction_list();
        let vec_ref = list.as_vec();

        assert_eq!(vec_ref.len(), 2);
        let uuid = Uuid::parse_str("6af613b6-569c-5c22-9c37-2ed93f31d3af").unwrap();
        assert_eq!(vec_ref[0].transaction_id, uuid);
        let uuid = Uuid::parse_str("b04965e6-a9bb-591f-8f8a-1adcb2c8dc39").unwrap();
        assert_eq!(vec_ref[1].transaction_id, uuid);
    }

    #[test]
    fn test_default_implementation() {
        let list = TransactionList::default();
        assert_eq!(list.len(), 0);
        assert!(list.is_empty());
    }

    #[test]
    fn test_is_empty() {
        let empty_list = TransactionList::new();
        assert!(empty_list.is_empty());

        let non_empty_list = create_test_transaction_list();
        assert!(!non_empty_list.is_empty());
    }

    #[test]
    fn test_complex_transaction_list_parsing() {
        let input = "Transactions:[Transaction:transaction_id=6af613b6-569c-5c22-9c37-2ed93f31d3af;taker_order_id=00000000-0000-0001-0000-000000000000;maker_order_id=00000000-0000-0002-0000-000000000000;price=10000;quantity=5;taker_side=BUY;timestamp=1616823000000,Transaction:transaction_id=b04965e6-a9bb-591f-8f8a-1adcb2c8dc39;taker_order_id=00000000-0000-0003-0000-000000000000;maker_order_id=00000000-0000-0004-0000-000000000000;price=10001;quantity=10;taker_side=SELL;timestamp=1616823000001,Transaction:transaction_id=b04965e6-a9bb-591f-8f8a-1adcb2c8dc40;taker_order_id=00000000-0000-0005-0000-000000000000;maker_order_id=00000000-0000-0006-0000-000000000000;price=10002;quantity=15;taker_side=BUY;timestamp=1616823000002]";

        let list = TransactionList::from_str(input).unwrap();

        assert_eq!(list.len(), 3);
        let uuid = Uuid::parse_str("6af613b6-569c-5c22-9c37-2ed93f31d3af").unwrap();
        assert_eq!(list.transactions[0].transaction_id, uuid);
        let uuid = Uuid::parse_str("b04965e6-a9bb-591f-8f8a-1adcb2c8dc39").unwrap();
        assert_eq!(list.transactions[1].transaction_id, uuid);
        let uuid = Uuid::parse_str("b04965e6-a9bb-591f-8f8a-1adcb2c8dc40").unwrap();
        assert_eq!(list.transactions[2].transaction_id, uuid);
    }
}