deribit-fix 0.3.1

This crate provides a client for the Deribit Markets API using the FIX protocol.
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
/******************************************************************************
   Author: Joaquín Béjar García
   Email: jb@taunais.com
   Date: 10/8/25
******************************************************************************/

//! Mass Quote Acknowledgement FIX Message Implementation

use crate::error::Result as DeribitFixResult;
use crate::message::builder::MessageBuilder;
use crate::message::orders::OrderSide;
use crate::model::types::MsgType;
use chrono::Utc;
use serde::{Deserialize, Serialize};

/// Quote acknowledgement status enumeration
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum QuoteAckStatus {
    /// Received - not yet processed
    Received,
    /// Accepted
    Accepted,
    /// Rejected
    Rejected,
}

impl From<QuoteAckStatus> for i32 {
    fn from(status: QuoteAckStatus) -> Self {
        match status {
            QuoteAckStatus::Received => 0,
            QuoteAckStatus::Accepted => 1,
            QuoteAckStatus::Rejected => 5,
        }
    }
}

impl TryFrom<i32> for QuoteAckStatus {
    type Error = String;

    fn try_from(value: i32) -> Result<Self, Self::Error> {
        match value {
            0 => Ok(QuoteAckStatus::Received),
            1 => Ok(QuoteAckStatus::Accepted),
            5 => Ok(QuoteAckStatus::Rejected),
            _ => Err(format!("Invalid QuoteAckStatus: {}", value)),
        }
    }
}

/// Quote reject reason enumeration
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum QuoteRejectReason {
    /// Unknown symbol
    UnknownSymbol,
    /// Exchange closed
    ExchangeClosed,
    /// Quote exceeds limit
    QuoteExceedsLimit,
    /// Too late to enter
    TooLateToEnter,
    /// Unknown quote
    UnknownQuote,
    /// Duplicate quote
    DuplicateQuote,
    /// Invalid bid/ask spread
    InvalidBidAskSpread,
    /// Invalid price
    InvalidPrice,
    /// Not authorized to quote security
    NotAuthorizedToQuoteSecurity,
    /// Price exceeds current price band
    PriceExceedsPriceBand,
    /// Quote locked - Try again
    QuoteLockedTryAgain,
    /// Invalid or unknown security issuer
    InvalidOrUnknownSecurityIssuer,
    /// Invalid or unknown issuer of underlying security
    InvalidOrUnknownIssuerOfUnderlyingSecurity,
    /// Other
    Other,
}

impl From<QuoteRejectReason> for i32 {
    fn from(reason: QuoteRejectReason) -> Self {
        match reason {
            QuoteRejectReason::UnknownSymbol => 1,
            QuoteRejectReason::ExchangeClosed => 2,
            QuoteRejectReason::QuoteExceedsLimit => 3,
            QuoteRejectReason::TooLateToEnter => 4,
            QuoteRejectReason::UnknownQuote => 5,
            QuoteRejectReason::DuplicateQuote => 6,
            QuoteRejectReason::InvalidBidAskSpread => 7,
            QuoteRejectReason::InvalidPrice => 8,
            QuoteRejectReason::NotAuthorizedToQuoteSecurity => 9,
            QuoteRejectReason::PriceExceedsPriceBand => 10,
            QuoteRejectReason::QuoteLockedTryAgain => 11,
            QuoteRejectReason::InvalidOrUnknownSecurityIssuer => 12,
            QuoteRejectReason::InvalidOrUnknownIssuerOfUnderlyingSecurity => 13,
            QuoteRejectReason::Other => 99,
        }
    }
}

impl TryFrom<i32> for QuoteRejectReason {
    type Error = String;

    fn try_from(value: i32) -> Result<Self, Self::Error> {
        match value {
            1 => Ok(QuoteRejectReason::UnknownSymbol),
            2 => Ok(QuoteRejectReason::ExchangeClosed),
            3 => Ok(QuoteRejectReason::QuoteExceedsLimit),
            4 => Ok(QuoteRejectReason::TooLateToEnter),
            5 => Ok(QuoteRejectReason::UnknownQuote),
            6 => Ok(QuoteRejectReason::DuplicateQuote),
            7 => Ok(QuoteRejectReason::InvalidBidAskSpread),
            8 => Ok(QuoteRejectReason::InvalidPrice),
            9 => Ok(QuoteRejectReason::NotAuthorizedToQuoteSecurity),
            10 => Ok(QuoteRejectReason::PriceExceedsPriceBand),
            11 => Ok(QuoteRejectReason::QuoteLockedTryAgain),
            12 => Ok(QuoteRejectReason::InvalidOrUnknownSecurityIssuer),
            13 => Ok(QuoteRejectReason::InvalidOrUnknownIssuerOfUnderlyingSecurity),
            99 => Ok(QuoteRejectReason::Other),
            _ => Err(format!("Invalid QuoteRejectReason: {}", value)),
        }
    }
}

/// Quote entry acknowledgement for mass quote ack
#[derive(Clone, PartialEq, Serialize, Deserialize)]
pub struct QuoteEntryAck {
    /// Quote entry ID
    pub quote_entry_id: String,
    /// Instrument symbol
    pub symbol: String,
    /// Quote acknowledgement status
    pub quote_ack_status: QuoteAckStatus,
    /// Quote reject reason
    pub quote_reject_reason: Option<QuoteRejectReason>,
    /// Side
    pub side: Option<OrderSide>,
    /// Bid price
    pub bid_px: Option<f64>,
    /// Offer price
    pub offer_px: Option<f64>,
    /// Bid size
    pub bid_size: Option<f64>,
    /// Offer size
    pub offer_size: Option<f64>,
    /// Text
    pub text: Option<String>,
}

impl QuoteEntryAck {
    /// Create a new quote entry acknowledgement
    pub fn new(quote_entry_id: String, symbol: String, quote_ack_status: QuoteAckStatus) -> Self {
        Self {
            quote_entry_id,
            symbol,
            quote_ack_status,
            quote_reject_reason: None,
            side: None,
            bid_px: None,
            offer_px: None,
            bid_size: None,
            offer_size: None,
            text: None,
        }
    }

    /// Create an accepted quote entry acknowledgement
    pub fn accepted(
        quote_entry_id: String,
        symbol: String,
        bid_px: Option<f64>,
        offer_px: Option<f64>,
        bid_size: Option<f64>,
        offer_size: Option<f64>,
    ) -> Self {
        Self {
            quote_entry_id,
            symbol,
            quote_ack_status: QuoteAckStatus::Accepted,
            quote_reject_reason: None,
            side: None,
            bid_px,
            offer_px,
            bid_size,
            offer_size,
            text: None,
        }
    }

    /// Create a rejected quote entry acknowledgement
    pub fn rejected(
        quote_entry_id: String,
        symbol: String,
        reject_reason: QuoteRejectReason,
        text: Option<String>,
    ) -> Self {
        Self {
            quote_entry_id,
            symbol,
            quote_ack_status: QuoteAckStatus::Rejected,
            quote_reject_reason: Some(reject_reason),
            side: None,
            bid_px: None,
            offer_px: None,
            bid_size: None,
            offer_size: None,
            text,
        }
    }
}

/// Mass Quote Acknowledgement message (MsgType = 'b')
#[derive(Clone, PartialEq, Serialize, Deserialize)]
pub struct MassQuoteAcknowledgement {
    /// Quote request ID
    pub quote_req_id: Option<String>,
    /// Quote ID
    pub quote_id: String,
    /// Quote acknowledgement status
    pub quote_ack_status: QuoteAckStatus,
    /// Quote reject reason
    pub quote_reject_reason: Option<QuoteRejectReason>,
    /// Quote response level
    pub quote_resp_level: Option<i32>,
    /// Quote set ID
    pub quote_set_id: Option<String>,
    /// Total quote entries
    pub tot_quote_entries: Option<i32>,
    /// Quote entry acknowledgements
    pub quote_entry_acks: Vec<QuoteEntryAck>,
    /// Account
    pub account: Option<String>,
    /// Clearing account
    pub clearing_account: Option<String>,
    /// Text
    pub text: Option<String>,
    /// Custom label
    pub deribit_label: Option<String>,
    /// Use standard FIX repeating groups instead of simplified custom tags
    pub use_standard_repeating_groups: bool,
}

impl MassQuoteAcknowledgement {
    /// Create a new mass quote acknowledgement
    pub fn new(quote_id: String, quote_ack_status: QuoteAckStatus) -> Self {
        Self {
            quote_req_id: None,
            quote_id,
            quote_ack_status,
            quote_reject_reason: None,
            quote_resp_level: None,
            quote_set_id: None,
            tot_quote_entries: None,
            quote_entry_acks: Vec::new(),
            account: None,
            clearing_account: None,
            text: None,
            deribit_label: None,
            use_standard_repeating_groups: false, // Default to simplified custom tags for backward compatibility
        }
    }

    /// Create an accepted mass quote acknowledgement
    pub fn accepted(
        quote_id: String,
        quote_set_id: String,
        quote_entry_acks: Vec<QuoteEntryAck>,
    ) -> Self {
        let tot_quote_entries = quote_entry_acks.len() as i32;

        Self {
            quote_req_id: None,
            quote_id,
            quote_ack_status: QuoteAckStatus::Accepted,
            quote_reject_reason: None,
            quote_resp_level: None,
            quote_set_id: Some(quote_set_id),
            tot_quote_entries: Some(tot_quote_entries),
            quote_entry_acks,
            account: None,
            clearing_account: None,
            text: None,
            deribit_label: None,
            use_standard_repeating_groups: false, // Default to simplified custom tags for backward compatibility
        }
    }

    /// Create a rejected mass quote acknowledgement
    pub fn rejected(
        quote_id: String,
        reject_reason: QuoteRejectReason,
        text: Option<String>,
    ) -> Self {
        Self {
            quote_req_id: None,
            quote_id,
            quote_ack_status: QuoteAckStatus::Rejected,
            quote_reject_reason: Some(reject_reason),
            quote_resp_level: None,
            quote_set_id: None,
            tot_quote_entries: None,
            quote_entry_acks: Vec::new(),
            account: None,
            clearing_account: None,
            text,
            deribit_label: None,
            use_standard_repeating_groups: false, // Default to simplified custom tags for backward compatibility
        }
    }

    /// Add a quote entry acknowledgement
    pub fn add_quote_entry_ack(mut self, entry_ack: QuoteEntryAck) -> Self {
        self.quote_entry_acks.push(entry_ack);
        self.tot_quote_entries = Some(self.quote_entry_acks.len() as i32);
        self
    }

    /// Set quote request ID
    pub fn with_quote_req_id(mut self, quote_req_id: String) -> Self {
        self.quote_req_id = Some(quote_req_id);
        self
    }

    /// Set quote response level
    pub fn with_quote_resp_level(mut self, quote_resp_level: i32) -> Self {
        self.quote_resp_level = Some(quote_resp_level);
        self
    }

    /// Set account
    pub fn with_account(mut self, account: String) -> Self {
        self.account = Some(account);
        self
    }

    /// Set custom label
    pub fn with_label(mut self, label: String) -> Self {
        self.deribit_label = Some(label);
        self
    }

    /// Enable standard FIX repeating groups (instead of simplified custom tags)
    pub fn enable_standard_repeating_groups(mut self) -> Self {
        self.use_standard_repeating_groups = true;
        self
    }

    /// Disable standard FIX repeating groups (use simplified custom tags for backward compatibility)
    pub fn disable_standard_repeating_groups(mut self) -> Self {
        self.use_standard_repeating_groups = false;
        self
    }

    /// Convert to FIX message
    pub fn to_fix_message(
        &self,
        sender_comp_id: &str,
        target_comp_id: &str,
        msg_seq_num: u32,
    ) -> DeribitFixResult<String> {
        let mut builder = MessageBuilder::new()
            .msg_type(MsgType::MassQuoteAcknowledgement)
            .sender_comp_id(sender_comp_id.to_string())
            .target_comp_id(target_comp_id.to_string())
            .msg_seq_num(msg_seq_num)
            .sending_time(Utc::now());

        // Required fields
        builder = builder
            .field(117, self.quote_id.clone()) // QuoteID
            .field(297, i32::from(self.quote_ack_status).to_string()); // QuoteAckStatus

        // Optional fields
        if let Some(quote_req_id) = &self.quote_req_id {
            builder = builder.field(131, quote_req_id.clone());
        }

        if let Some(quote_reject_reason) = &self.quote_reject_reason {
            builder = builder.field(300, i32::from(*quote_reject_reason).to_string());
        }

        if let Some(quote_resp_level) = &self.quote_resp_level {
            builder = builder.field(301, quote_resp_level.to_string());
        }

        if let Some(quote_set_id) = &self.quote_set_id {
            builder = builder.field(302, quote_set_id.clone());
        }

        if let Some(tot_quote_entries) = &self.tot_quote_entries {
            builder = builder.field(295, tot_quote_entries.to_string());
        }

        if let Some(account) = &self.account {
            builder = builder.field(1, account.clone());
        }

        if let Some(text) = &self.text {
            builder = builder.field(58, text.clone());
        }

        if let Some(deribit_label) = &self.deribit_label {
            builder = builder.field(100010, deribit_label.clone());
        }

        // Add quote entry acknowledgements - support both standard FIX repeating groups and simplified custom tags
        if self.use_standard_repeating_groups {
            // Standard FIX repeating groups implementation
            builder = builder.field(295, self.quote_entry_acks.len().to_string()); // NoQuoteEntries

            for entry_ack in &self.quote_entry_acks {
                builder = builder
                    .field(299, entry_ack.quote_entry_id.clone()) // QuoteEntryID
                    .field(9020, i32::from(entry_ack.quote_ack_status).to_string()); // QuoteEntryType (0 = order, 1 = trade, 2 = error)

                if let Some(quote_set_id) = &self.quote_set_id {
                    builder = builder.field(302, quote_set_id.clone()); // QuoteSetID
                }

                builder = builder.field(1167, i32::from(entry_ack.quote_ack_status).to_string()); // QuoteEntryStatus

                builder = builder.field(55, entry_ack.symbol.clone()); // Symbol

                if let Some(side) = &entry_ack.side {
                    builder = builder.field(54, char::from(*side).to_string()); // Side
                }

                if let Some(bid_px) = &entry_ack.bid_px {
                    builder = builder.field(132, bid_px.to_string()); // BidPx
                }

                if let Some(offer_px) = &entry_ack.offer_px {
                    builder = builder.field(133, offer_px.to_string()); // OfferPx
                }

                if let Some(bid_size) = &entry_ack.bid_size {
                    builder = builder.field(134, bid_size.to_string()); // BidSize
                }

                if let Some(offer_size) = &entry_ack.offer_size {
                    builder = builder.field(135, offer_size.to_string()); // OfferSize
                }

                if let Some(quote_reject_reason) = &entry_ack.quote_reject_reason {
                    builder = builder.field(368, i32::from(*quote_reject_reason).to_string()); // QuoteEntryRejectReason
                }

                if let Some(text) = &entry_ack.text {
                    builder = builder.field(58, text.clone()); // Text
                }
            }
        } else {
            // Simplified custom tags implementation (backward compatibility)
            for (i, entry_ack) in self.quote_entry_acks.iter().enumerate() {
                let base_tag = 3000 + (i * 100); // Custom tag range for quote entry acks

                builder = builder
                    .field(base_tag as u32, entry_ack.quote_entry_id.clone()) // QuoteEntryID
                    .field((base_tag + 1) as u32, entry_ack.symbol.clone()) // Symbol
                    .field(
                        (base_tag + 2) as u32,
                        i32::from(entry_ack.quote_ack_status).to_string(),
                    ); // QuoteAckStatus

                if let Some(quote_reject_reason) = &entry_ack.quote_reject_reason {
                    builder = builder.field(
                        (base_tag + 3) as u32,
                        i32::from(*quote_reject_reason).to_string(),
                    );
                }

                if let Some(side) = &entry_ack.side {
                    builder = builder.field((base_tag + 4) as u32, char::from(*side).to_string());
                }

                if let Some(bid_px) = &entry_ack.bid_px {
                    builder = builder.field((base_tag + 10) as u32, bid_px.to_string());
                }

                if let Some(offer_px) = &entry_ack.offer_px {
                    builder = builder.field((base_tag + 11) as u32, offer_px.to_string());
                }

                if let Some(bid_size) = &entry_ack.bid_size {
                    builder = builder.field((base_tag + 12) as u32, bid_size.to_string());
                }

                if let Some(offer_size) = &entry_ack.offer_size {
                    builder = builder.field((base_tag + 13) as u32, offer_size.to_string());
                }

                if let Some(text) = &entry_ack.text {
                    builder = builder.field((base_tag + 20) as u32, text.clone());
                }
            }
        }

        Ok(builder.build()?.to_string())
    }
}

impl_json_display!(MassQuoteAcknowledgement, QuoteEntryAck);
impl_json_debug_pretty!(MassQuoteAcknowledgement, QuoteEntryAck);

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_quote_entry_ack_creation() {
        let entry_ack = QuoteEntryAck::new(
            "QEA123".to_string(),
            "BTC-PERPETUAL".to_string(),
            QuoteAckStatus::Accepted,
        );

        assert_eq!(entry_ack.quote_entry_id, "QEA123");
        assert_eq!(entry_ack.symbol, "BTC-PERPETUAL");
        assert_eq!(entry_ack.quote_ack_status, QuoteAckStatus::Accepted);
        assert!(entry_ack.quote_reject_reason.is_none());
    }

    #[test]
    fn test_quote_entry_ack_accepted() {
        let entry_ack = QuoteEntryAck::accepted(
            "QEA456".to_string(),
            "ETH-PERPETUAL".to_string(),
            Some(3200.0),
            Some(3205.0),
            Some(10.0),
            Some(8.0),
        );

        assert_eq!(entry_ack.quote_ack_status, QuoteAckStatus::Accepted);
        assert_eq!(entry_ack.bid_px, Some(3200.0));
        assert_eq!(entry_ack.offer_px, Some(3205.0));
        assert_eq!(entry_ack.bid_size, Some(10.0));
        assert_eq!(entry_ack.offer_size, Some(8.0));
    }

    #[test]
    fn test_quote_entry_ack_rejected() {
        let entry_ack = QuoteEntryAck::rejected(
            "QEA789".to_string(),
            "BTC-PERPETUAL".to_string(),
            QuoteRejectReason::InvalidPrice,
            Some("Price out of range".to_string()),
        );

        assert_eq!(entry_ack.quote_ack_status, QuoteAckStatus::Rejected);
        assert_eq!(
            entry_ack.quote_reject_reason,
            Some(QuoteRejectReason::InvalidPrice)
        );
        assert_eq!(entry_ack.text, Some("Price out of range".to_string()));
    }

    #[test]
    fn test_mass_quote_acknowledgement_creation() {
        let mass_quote_ack =
            MassQuoteAcknowledgement::new("MQA123".to_string(), QuoteAckStatus::Received);

        assert_eq!(mass_quote_ack.quote_id, "MQA123");
        assert_eq!(mass_quote_ack.quote_ack_status, QuoteAckStatus::Received);
        assert!(mass_quote_ack.quote_entry_acks.is_empty());
    }

    #[test]
    fn test_mass_quote_acknowledgement_accepted() {
        let entry_ack1 = QuoteEntryAck::accepted(
            "QEA1".to_string(),
            "BTC-PERPETUAL".to_string(),
            Some(50000.0),
            Some(50010.0),
            Some(5.0),
            Some(3.0),
        );
        let entry_ack2 = QuoteEntryAck::accepted(
            "QEA2".to_string(),
            "ETH-PERPETUAL".to_string(),
            Some(3200.0),
            Some(3205.0),
            Some(10.0),
            Some(8.0),
        );

        let mass_quote_ack = MassQuoteAcknowledgement::accepted(
            "MQA456".to_string(),
            "QS789".to_string(),
            vec![entry_ack1, entry_ack2],
        );

        assert_eq!(mass_quote_ack.quote_ack_status, QuoteAckStatus::Accepted);
        assert_eq!(mass_quote_ack.quote_set_id, Some("QS789".to_string()));
        assert_eq!(mass_quote_ack.tot_quote_entries, Some(2));
        assert_eq!(mass_quote_ack.quote_entry_acks.len(), 2);
    }

    #[test]
    fn test_mass_quote_acknowledgement_rejected() {
        let mass_quote_ack = MassQuoteAcknowledgement::rejected(
            "MQA999".to_string(),
            QuoteRejectReason::ExchangeClosed,
            Some("Exchange is closed".to_string()),
        );

        assert_eq!(mass_quote_ack.quote_ack_status, QuoteAckStatus::Rejected);
        assert_eq!(
            mass_quote_ack.quote_reject_reason,
            Some(QuoteRejectReason::ExchangeClosed)
        );
        assert_eq!(mass_quote_ack.text, Some("Exchange is closed".to_string()));
        assert!(mass_quote_ack.quote_entry_acks.is_empty());
    }

    #[test]
    fn test_mass_quote_acknowledgement_add_entry() {
        let entry_ack = QuoteEntryAck::new(
            "QEA1".to_string(),
            "BTC-PERPETUAL".to_string(),
            QuoteAckStatus::Accepted,
        );

        let mass_quote_ack =
            MassQuoteAcknowledgement::new("MQA123".to_string(), QuoteAckStatus::Accepted)
                .add_quote_entry_ack(entry_ack);

        assert_eq!(mass_quote_ack.tot_quote_entries, Some(1));
        assert_eq!(mass_quote_ack.quote_entry_acks.len(), 1);
    }

    #[test]
    fn test_mass_quote_acknowledgement_to_fix_message() {
        let entry_ack = QuoteEntryAck::accepted(
            "QEA1".to_string(),
            "BTC-PERPETUAL".to_string(),
            Some(50000.0),
            Some(50010.0),
            Some(5.0),
            Some(3.0),
        );

        let mass_quote_ack = MassQuoteAcknowledgement::accepted(
            "MQA123".to_string(),
            "QS456".to_string(),
            vec![entry_ack],
        )
        .with_label("test-label".to_string());

        let fix_message = mass_quote_ack
            .to_fix_message("SENDER", "TARGET", 1)
            .unwrap();

        // Check that the message contains required fields
        assert!(fix_message.contains("35=b")); // MsgType
        assert!(fix_message.contains("117=MQA123")); // QuoteID
        assert!(fix_message.contains("297=1")); // QuoteAckStatus=Accepted
        assert!(fix_message.contains("302=QS456")); // QuoteSetID
        assert!(fix_message.contains("295=1")); // TotQuoteEntries
        assert!(fix_message.contains("100010=test-label")); // Custom label

        // Check quote entry ack fields (simplified check)
        assert!(fix_message.contains("3000=QEA1")); // QuoteEntryID
        assert!(fix_message.contains("3001=BTC-PERPETUAL")); // Symbol
        assert!(fix_message.contains("3002=1")); // QuoteAckStatus=Accepted
    }

    #[test]
    fn test_quote_ack_status_conversions() {
        assert_eq!(i32::from(QuoteAckStatus::Received), 0);
        assert_eq!(i32::from(QuoteAckStatus::Accepted), 1);
        assert_eq!(i32::from(QuoteAckStatus::Rejected), 5);

        assert_eq!(
            QuoteAckStatus::try_from(0).unwrap(),
            QuoteAckStatus::Received
        );
        assert_eq!(
            QuoteAckStatus::try_from(1).unwrap(),
            QuoteAckStatus::Accepted
        );
        assert_eq!(
            QuoteAckStatus::try_from(5).unwrap(),
            QuoteAckStatus::Rejected
        );

        assert!(QuoteAckStatus::try_from(99).is_err());
    }

    #[test]
    fn test_quote_reject_reason_conversions() {
        assert_eq!(i32::from(QuoteRejectReason::UnknownSymbol), 1);
        assert_eq!(i32::from(QuoteRejectReason::ExchangeClosed), 2);
        assert_eq!(i32::from(QuoteRejectReason::Other), 99);

        assert_eq!(
            QuoteRejectReason::try_from(1).unwrap(),
            QuoteRejectReason::UnknownSymbol
        );
        assert_eq!(
            QuoteRejectReason::try_from(2).unwrap(),
            QuoteRejectReason::ExchangeClosed
        );
        assert_eq!(
            QuoteRejectReason::try_from(99).unwrap(),
            QuoteRejectReason::Other
        );

        assert!(QuoteRejectReason::try_from(50).is_err());
    }
}