fixer 0.11.0

A full featured messaging engine for the FIX protocol implemented in Rust
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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
use crate::message::ParseError;
use crate::session::settings::{ConditionallyRequiredSetting, IncorrectFormatForSetting};
use crate::tag::Tag;
use delegate::delegate;
use enum_dispatch::enum_dispatch;
use simple_error::SimpleError;
use std::error::Error;
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::sync::LazyLock;
use thiserror::Error as ThisError;

/// Sentinel error returned from [`Application::to_app`](crate::application::Application::to_app)
/// to prevent a message from being sent.
pub static ERR_DO_NOT_SEND: LazyLock<SimpleError> = LazyLock::new(|| simple_error!("Do Not Send"));

pub const REJECT_REASON_INVALID_TAG_NUMBER: isize = 0;
pub const REJECT_REASON_REQUIRED_TAG_MISSING: isize = 1;
pub const REJECT_REASON_TAG_NOT_DEFINED_FOR_THIS_MESSAGE_TYPE: isize = 2;
pub const REJECT_REASON_UNSUPPORTED_MESSAGE_TYPE: isize = 3;
pub const REJECT_REASON_TAG_SPECIFIED_WITHOUT_A_VALUE: isize = 4;
pub const REJECT_REASON_VALUE_IS_INCORRECT: isize = 5;
pub const REJECT_REASON_CONDITIONALLY_REQUIRED_FIELD_MISSING: isize = 5;
pub const REJECT_REASON_INCORRECT_DATA_FORMAT_FOR_VALUE: isize = 6;
pub const REJECT_REASON_COMP_ID_PROBLEM: isize = 9;
pub const REJECT_REASON_SENDING_TIME_ACCURACY_PROBLEM: isize = 10;
pub const REJECT_REASON_INVALID_MSG_TYPE: isize = 11;
pub const REJECT_REASON_TAG_APPEARS_MORE_THAN_ONCE: isize = 13;
pub const REJECT_REASON_TAG_SPECIFIED_OUT_OF_REQUIRED_ORDER: isize = 14;
pub const REJECT_REASON_REPEATING_GROUP_FIELDS_OUT_OF_ORDER: isize = 15;
pub const REJECT_REASON_INCORRECT_NUM_IN_GROUP_COUNT_FOR_REPEATING_GROUP: isize = 16;
pub const REJECT_REASON_OTHER: isize = 99;

/// Trait for errors that map to a FIX Reject or `BusinessMessageReject`.
///
/// Returned from [`Application::from_app`](crate::application::Application::from_app)
/// and [`Application::from_admin`](crate::application::Application::from_admin)
/// to tell the engine to send a reject to the counterparty.
#[enum_dispatch(MessageRejectErrorEnum)]
pub trait MessageRejectErrorTrait: Error {
    /// Session-level reject reason (tag 373) or business reject reason
    /// (tag 380).
    fn reject_reason(&self) -> isize;
    /// Business-level `RefMsgType` for `BusinessMessageReject`.
    fn business_reject_ref_id(&self) -> &str;
    /// The tag that caused the rejection, if applicable.
    fn ref_tag_id(&self) -> Option<Tag>;
    /// Returns `true` if this is a business-level reject (35=j) rather than
    /// a session-level reject (35=3).
    fn is_business_reject(&self) -> bool;
}

/// Convenience alias for `Result<(), MessageRejectErrorEnum>`, used as the
/// return type of [`Application`](crate::application::Application) callbacks.
pub type MessageRejectErrorResult = Result<(), MessageRejectErrorEnum>;

/// Enum of all message rejection error types.
#[enum_dispatch]
#[derive(Debug, ThisError, Clone, PartialEq, Eq)]
pub enum MessageRejectErrorEnum {
    MessageRejectError(MessageRejectError),
    RejectLogon(RejectLogon),
    IncorrectBeginString(IncorrectBeginString),
    TargetTooLow(TargetTooLow),
    TargetTooHigh(TargetTooHigh),
}

impl Display for MessageRejectErrorEnum {
    delegate! {
        to match self {
            Self::MessageRejectError(err) => err,
            Self::RejectLogon(err) => err,
            Self::IncorrectBeginString(err) => err,
            Self::TargetTooLow(err) => err,
            Self::TargetTooHigh(err) => err,
        } {
            fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult;
        }
    }
}

impl From<MessageRejectErrorEnum> for SimpleError {
    fn from(err: MessageRejectErrorEnum) -> SimpleError {
        SimpleError::from(err)
    }
}

/// Returned from [`Application::from_admin`](crate::application::Application::from_admin)
/// to reject an incoming Logon.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RejectLogon {
    pub text: String,
}

impl Display for RejectLogon {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        write!(f, "{}", self.text)
    }
}

impl Error for RejectLogon {}

impl MessageRejectErrorTrait for RejectLogon {
    // reject_reason implements MessageRejectError
    fn reject_reason(&self) -> isize {
        0
    }

    // business_reject_ref_id implements MessageRejectError
    fn business_reject_ref_id(&self) -> &'static str {
        ""
    }

    // ref_tag_id implements MessageRejectError
    fn ref_tag_id(&self) -> Option<Tag> {
        None
    }

    // is_business_reject implements MessageRejectError
    fn is_business_reject(&self) -> bool {
        false
    }
}

#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct MessageRejectError {
    pub reject_reason: isize,
    pub text: String,
    pub business_reject_ref_id: String,
    pub ref_tag_id: Option<Tag>,
    pub is_business_reject: bool,
}

impl Display for MessageRejectError {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        write!(f, "{}", self.text)
    }
}

impl Error for MessageRejectError {}

impl MessageRejectErrorTrait for MessageRejectError {
    fn reject_reason(&self) -> isize {
        self.reject_reason
    }

    fn business_reject_ref_id(&self) -> &str {
        &self.business_reject_ref_id
    }

    // ref_tag_id implements MessageRejectError
    fn ref_tag_id(&self) -> Option<Tag> {
        self.ref_tag_id
    }

    // is_business_reject implements MessageRejectError
    fn is_business_reject(&self) -> bool {
        self.is_business_reject
    }
}

impl MessageRejectError {
    // MessageRejectError::new() returns a MessageRejectError with the given error message, reject reason, and optional reftagid
    #[allow(clippy::new_ret_no_self)]
    pub fn new(
        err: String,
        reject_reason: isize,
        ref_tag_id: Option<Tag>,
    ) -> MessageRejectErrorEnum {
        MessageRejectError {
            text: err,
            reject_reason,
            ref_tag_id,
            business_reject_ref_id: String::new(),
            is_business_reject: false,
        }
        .into()
    }
}

// new_business_message_reject_error returns a MessageRejectError with the given error mesage, reject reason, and optional ref_tag_id.
// Reject is treated as a business level reject
pub fn new_business_message_reject_error(
    err: String,
    reject_reason: isize,
    ref_tag_id: Option<Tag>,
) -> MessageRejectErrorEnum {
    MessageRejectError {
        text: err,
        reject_reason,
        ref_tag_id,
        business_reject_ref_id: String::new(),
        is_business_reject: true,
    }
    .into()
}

// new_business_message_reject_error_with_ref_id returns a MessageRejectError with the given error mesage, reject reason, ref_id, and optional ref_tag_id.
// Reject is treated as a business level reject
pub fn new_business_message_reject_error_with_ref_id(
    err: String,
    reject_reason: isize,
    business_reject_ref_id: String,
    ref_tag_id: Option<Tag>,
) -> MessageRejectErrorEnum {
    MessageRejectError {
        text: err,
        reject_reason,
        ref_tag_id,
        business_reject_ref_id,
        is_business_reject: true,
    }
    .into()
}

// incorrect_data_format_for_value returns an error indicating a field that cannot be parsed as the type required.
pub fn incorrect_data_format_for_value(tag: Tag) -> MessageRejectErrorEnum {
    MessageRejectError::new(
        String::from("Incorrect data format for value"),
        REJECT_REASON_INCORRECT_DATA_FORMAT_FOR_VALUE,
        Some(tag),
    )
}

// repeating_group_fields_out_of_order returns an error indicating a problem parsing repeating groups fields
pub fn repeating_group_fields_out_of_order(tag: Tag, mut reason: String) -> MessageRejectErrorEnum {
    if reason.is_empty() {
        reason = String::from("Repeating group fields out of order");
    } else {
        reason = format!("Repeating group fields out of order ({reason})");
    }
    MessageRejectError::new(
        reason,
        REJECT_REASON_REPEATING_GROUP_FIELDS_OUT_OF_ORDER,
        Some(tag),
    )
}

// value_is_incorrect returns an error indicating a field with value that is not valid.
pub fn value_is_incorrect(tag: Tag) -> MessageRejectErrorEnum {
    MessageRejectError::new(
        String::from("Value is incorrect (out of range) for this tag"),
        REJECT_REASON_VALUE_IS_INCORRECT,
        Some(tag),
    )
}

// conditionally_required_field_missing indicates that the requested field could not be found in the FIX message.
pub fn conditionally_required_field_missing(tag: Tag) -> MessageRejectErrorEnum {
    new_business_message_reject_error(
        format!("Conditionally Required Field Missing ({tag})"),
        REJECT_REASON_CONDITIONALLY_REQUIRED_FIELD_MISSING,
        Some(tag),
    )
}

// value_is_incorrect_no_tag returns an error indicating a field with value that is not valid.
pub fn value_is_incorrect_no_tag() -> MessageRejectErrorEnum {
    MessageRejectError::new(
        String::from("Value is incorrect (out of range) for this tag"),
        REJECT_REASON_VALUE_IS_INCORRECT,
        None,
    )
}

// invalid_message_type returns an error to indicate an invalid message type
pub fn invalid_message_type() -> MessageRejectErrorEnum {
    MessageRejectError::new(
        String::from("Invalid MsgType"),
        REJECT_REASON_INVALID_MSG_TYPE,
        None,
    )
}

// unsupported_message_type returns an error to indicate an unhandled message.
pub fn unsupported_message_type() -> MessageRejectErrorEnum {
    new_business_message_reject_error(
        String::from("Unsupported Message Type"),
        REJECT_REASON_UNSUPPORTED_MESSAGE_TYPE,
        None,
    )
}

// tag_not_defined_for_this_message_type returns an error for an invalid tag appearing in a message.
pub fn tag_not_defined_for_this_message_type(tag: Tag) -> MessageRejectErrorEnum {
    MessageRejectError::new(
        String::from("Tag not defined for this message type"),
        REJECT_REASON_TAG_NOT_DEFINED_FOR_THIS_MESSAGE_TYPE,
        Some(tag),
    )
}

// tag_appears_more_than_once return an error for multiple tags in a message not detected as a repeating group.
pub fn tag_appears_more_than_once(tag: Tag) -> MessageRejectErrorEnum {
    MessageRejectError::new(
        String::from("Tag appears more than once"),
        REJECT_REASON_TAG_APPEARS_MORE_THAN_ONCE,
        Some(tag),
    )
}

// required_tag_missing returns a validation error when a required field cannot be found in a message.
pub fn required_tag_missing(tag: Tag) -> MessageRejectErrorEnum {
    MessageRejectError::new(
        String::from("Required tag missing"),
        REJECT_REASON_REQUIRED_TAG_MISSING,
        Some(tag),
    )
}

// incorrect_num_in_group_count_for_repeating_group returns a validation error when the num in group value for a group does not match actual size.
pub fn incorrect_num_in_group_count_for_repeating_group(tag: Tag) -> MessageRejectErrorEnum {
    MessageRejectError::new(
        String::from("Incorrect NumInGroup count for repeating group"),
        REJECT_REASON_INCORRECT_NUM_IN_GROUP_COUNT_FOR_REPEATING_GROUP,
        Some(tag),
    )
}

// tag_specified_out_of_required_order returns validation error when the group order does not match the spec.
pub fn tag_specified_out_of_required_order(tag: Tag) -> MessageRejectErrorEnum {
    MessageRejectError::new(
        String::from("Tag specified out of required order"),
        REJECT_REASON_TAG_SPECIFIED_OUT_OF_REQUIRED_ORDER,
        Some(tag),
    )
}

// tag_specified_without_a_value returns a validation error for when a field has no value.
pub fn tag_specified_without_a_value(tag: Tag) -> MessageRejectErrorEnum {
    MessageRejectError::new(
        String::from("Tag specified without a value"),
        REJECT_REASON_TAG_SPECIFIED_WITHOUT_A_VALUE,
        Some(tag),
    )
}

// invalid_tag_number returns a validation error for messages with invalid tags.
pub fn invalid_tag_number(tag: Tag) -> MessageRejectErrorEnum {
    MessageRejectError::new(
        String::from("Invalid tag number"),
        REJECT_REASON_INVALID_TAG_NUMBER,
        Some(tag),
    )
}

// comp_id_problem creates a reject for msg where msg has invalid comp id values.
pub fn comp_id_problem() -> MessageRejectErrorEnum {
    MessageRejectError::new(
        String::from("CompID problem"),
        REJECT_REASON_COMP_ID_PROBLEM,
        None,
    )
}

// sending_time_accuracy_problem creates a reject for a msg with stale or invalid sending time.
pub fn sending_time_accuracy_problem() -> MessageRejectErrorEnum {
    MessageRejectError::new(
        String::from("SendingTime accuracy problem"),
        REJECT_REASON_SENDING_TIME_ACCURACY_PROBLEM,
        None,
    )
}

pub fn other_error() -> MessageRejectErrorEnum {
    MessageRejectError::new(String::from("Other error"), REJECT_REASON_OTHER, None)
}

/// A session-level reject indicating the counterparty sent a message whose
/// `BeginString` (tag 8) does not match the expected FIX version for this
/// session.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct IncorrectBeginString {
    pub message_reject_error: MessageRejectError,
}

impl MessageRejectErrorTrait for IncorrectBeginString {
    delegate! {
        to self.message_reject_error {
            fn reject_reason(&self) -> isize;
            fn business_reject_ref_id(&self) -> &str;
            fn ref_tag_id(&self) -> Option<Tag>;
            fn is_business_reject(&self) -> bool;
        }
    }
}

impl Display for IncorrectBeginString {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        write!(f, "Incorrect BeginString")
    }
}

impl Error for IncorrectBeginString {}

/// A session-level reject indicating the incoming `MsgSeqNum` is higher than
/// expected, which typically triggers a resend request.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct TargetTooHigh {
    pub message_reject_error: MessageRejectError,
    pub received_target: isize,
    pub expected_target: isize,
}

impl MessageRejectErrorTrait for TargetTooHigh {
    delegate! {
        to self.message_reject_error {
            fn reject_reason(&self) -> isize;
            fn business_reject_ref_id(&self) -> &str;
            fn ref_tag_id(&self) -> Option<Tag>;
            fn is_business_reject(&self) -> bool;
        }
    }
}

impl Display for TargetTooHigh {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        write!(
            f,
            "MsgSeqNum too high, expecting {} but received {}",
            self.expected_target, self.received_target
        )
    }
}

impl Error for TargetTooHigh {}

/// A session-level reject indicating the incoming `MsgSeqNum` is lower than
/// expected, suggesting a possible duplicate or sequencing error.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct TargetTooLow {
    pub message_reject_error: MessageRejectError,
    pub received_target: isize,
    pub expected_target: isize,
}

impl MessageRejectErrorTrait for TargetTooLow {
    delegate! {
        to self.message_reject_error {
            fn reject_reason(&self) -> isize;
            fn business_reject_ref_id(&self) -> &str;
            fn ref_tag_id(&self) -> Option<Tag>;
            fn is_business_reject(&self) -> bool;
        }
    }
}

impl Display for TargetTooLow {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        write!(
            f,
            "MsgSeqNum too low, expecting {} but received {}",
            self.expected_target, self.received_target
        )
    }
}

impl Error for TargetTooLow {}

/// Top-level error type for the fixer engine.
///
/// Wraps all error kinds that can occur during session operation:
/// message-level rejects, configuration errors, and parse failures.
#[derive(Debug, ThisError)]
pub enum FixerError {
    Reject(#[from] MessageRejectErrorEnum),
    Simple(#[from] SimpleError),
    ConditionallyRequiredSetting(#[from] ConditionallyRequiredSetting),
    IncorrectFormatForSetting(#[from] IncorrectFormatForSetting),
    Parse(#[from] ParseError),
}

impl Display for FixerError {
    delegate! {
        to match self {
            Self::Reject(err) => err,
            Self::Simple(err) => err,
            Self::ConditionallyRequiredSetting(err) => err,
            Self::IncorrectFormatForSetting(err) => err,
            Self::Parse(err) => err,
        } {
            fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult;
        }
    }
}

impl From<FixerError> for SimpleError {
    fn from(err: FixerError) -> SimpleError {
        SimpleError::from(err)
    }
}

impl FixerError {
    /// Creates a [`ConditionallyRequiredSetting`] error for the given key.
    pub fn new_conditionally_required(setting: &str) -> Self {
        Self::ConditionallyRequiredSetting(ConditionallyRequiredSetting {
            setting: setting.to_string(),
        })
    }

    /// Creates an [`IncorrectFormatForSetting`] error with a generic message.
    pub fn new_incorrect_format_for_setting(setting: &str, string_val: &str) -> Self {
        Self::IncorrectFormatForSetting(IncorrectFormatForSetting {
            setting: setting.to_string(),
            value: string_val.to_string(),
            err: Box::new(simple_error!("invalid setting")),
        })
    }

    /// Creates an [`IncorrectFormatForSetting`] error wrapping a specific
    /// cause.
    pub fn new_incorrect_format_for_setting_with_error(
        setting: &str,
        string_val: &str,
        err: Box<dyn Error + Send + Sync>,
    ) -> Self {
        Self::IncorrectFormatForSetting(IncorrectFormatForSetting {
            setting: setting.to_string(),
            value: string_val.to_string(),
            err,
        })
    }
}

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

    #[test]
    fn test_new_message_reject_error() {
        let expected_error_string = "Custom error";
        let expected_reject_reason = 5;
        let expected_ref_tag_id: Tag = 44;
        let expected_is_business_reject = false;

        let msg_rej = MessageRejectError::new(
            String::from(expected_error_string),
            expected_reject_reason,
            Some(expected_ref_tag_id),
        );

        assert_eq!(
            msg_rej.to_string(),
            expected_error_string,
            "expected: {expected_error_string}, got: {msg_rej}\n",
        );

        assert_eq!(
            msg_rej.reject_reason(),
            expected_reject_reason,
            "expected: {}, got: {}\n",
            expected_reject_reason,
            msg_rej.reject_reason(),
        );

        assert_eq!(
            msg_rej.ref_tag_id(),
            Some(expected_ref_tag_id),
            "expected: {}, got: {}\n",
            expected_ref_tag_id,
            msg_rej.ref_tag_id().unwrap(),
        );

        assert_eq!(
            msg_rej.is_business_reject(),
            expected_is_business_reject,
            "Expected is_business_reject to be false\n",
        );
    }

    #[test]
    fn test_new_business_message_reject_error() {
        let expected_error_string = "Custom error";
        let expected_reject_reason = 5;
        let expected_ref_tag_id: Tag = 44;
        let expected_is_business_reject = true;

        let msg_rej = new_business_message_reject_error(
            String::from(expected_error_string),
            expected_reject_reason,
            Some(expected_ref_tag_id),
        );

        assert_eq!(
            msg_rej.to_string(),
            expected_error_string,
            "expected: {expected_error_string}, got: {msg_rej}\n",
        );
        assert_eq!(
            msg_rej.reject_reason(),
            expected_reject_reason,
            "expected: {}, got: {}\n",
            expected_reject_reason,
            msg_rej.reject_reason(),
        );
        assert_eq!(
            msg_rej.ref_tag_id(),
            Some(expected_ref_tag_id),
            "expected: {}, got: {}\n",
            expected_ref_tag_id,
            msg_rej.ref_tag_id().unwrap(),
        );
        assert_eq!(
            msg_rej.is_business_reject(),
            expected_is_business_reject,
            "Expected is_business_reject to be true\n",
        );
    }

    #[test]
    fn test_new_business_message_reject_error_with_ref_id() {
        let expected_error_string = "Custom error";
        let expected_reject_reason = 5;
        let expected_business_reject_ref_id = "1";
        let expected_ref_tag_id: Tag = 44;
        let expected_is_business_reject = true;

        let msg_rej = new_business_message_reject_error_with_ref_id(
            String::from(expected_error_string),
            expected_reject_reason,
            String::from(expected_business_reject_ref_id),
            Some(expected_ref_tag_id),
        );

        assert_eq!(
            msg_rej.to_string(),
            expected_error_string,
            "expected: {expected_error_string}, got: {msg_rej}\n",
        );
        assert_eq!(
            msg_rej.reject_reason(),
            expected_reject_reason,
            "expected: {}, got: {}\n",
            expected_reject_reason,
            msg_rej.reject_reason(),
        );
        assert_eq!(
            msg_rej.business_reject_ref_id(),
            expected_business_reject_ref_id,
            "expected: {}, got: {}\n",
            expected_business_reject_ref_id,
            msg_rej.business_reject_ref_id(),
        );
        assert_eq!(
            msg_rej.ref_tag_id(),
            Some(expected_ref_tag_id),
            "expected: {}, got: {}\n",
            expected_ref_tag_id,
            msg_rej.ref_tag_id().unwrap(),
        );
        assert_eq!(
            msg_rej.is_business_reject(),
            expected_is_business_reject,
            "Expected is_business_reject to be true\n",
        );
    }

    #[test]
    fn test_incorrect_data_format_for_value() {
        let expected_error_string = "Incorrect data format for value";
        let expected_reject_reason = 6;
        let expected_ref_tag_id: Tag = 44;
        let expected_is_business_reject = false;

        let msg_rej = incorrect_data_format_for_value(expected_ref_tag_id);

        assert_eq!(
            msg_rej.to_string(),
            expected_error_string,
            "expected: {expected_error_string}, got: {msg_rej}\n",
        );
        assert_eq!(
            msg_rej.reject_reason(),
            expected_reject_reason,
            "expected: {}, got: {}\n",
            expected_reject_reason,
            msg_rej.reject_reason(),
        );
        assert_eq!(
            msg_rej.ref_tag_id(),
            Some(expected_ref_tag_id),
            "expected: {}, got: {}\n",
            expected_ref_tag_id,
            msg_rej.ref_tag_id().unwrap(),
        );
        assert_eq!(
            msg_rej.is_business_reject(),
            expected_is_business_reject,
            "Expected is_business_reject to be false\n",
        );
    }

    #[test]
    fn test_value_is_incorrect() {
        let expected_error_string = "Value is incorrect (out of range) for this tag";
        let expected_reject_reason = 5;
        let expected_ref_tag_id: Tag = 44;
        let expected_is_business_reject = false;

        let msg_rej = value_is_incorrect(expected_ref_tag_id);

        assert_eq!(
            msg_rej.ref_tag_id(),
            Some(expected_ref_tag_id),
            "expected: {}, got: {}\n",
            expected_ref_tag_id,
            msg_rej.ref_tag_id().unwrap(),
        );
        assert_eq!(
            msg_rej.to_string(),
            expected_error_string,
            "expected: {expected_error_string}, got: {msg_rej}\n",
        );
        assert_eq!(
            msg_rej.reject_reason(),
            expected_reject_reason,
            "expected: {}, got: {}\n",
            expected_reject_reason,
            msg_rej.reject_reason(),
        );
        assert_eq!(
            msg_rej.is_business_reject(),
            expected_is_business_reject,
            "Expected is_business_reject to be false\n",
        );
    }

    #[test]
    fn test_conditionally_required_field_missing() {
        let expected_reject_reason = 5;
        let expected_ref_tag_id: Tag = 44;
        let expected_error_string =
            format!("Conditionally Required Field Missing ({expected_ref_tag_id})",);
        let expected_is_business_reject = true;

        let msg_rej = conditionally_required_field_missing(expected_ref_tag_id);

        assert_eq!(
            msg_rej.ref_tag_id(),
            Some(expected_ref_tag_id),
            "expected: {}, got: {}\n",
            expected_ref_tag_id,
            msg_rej.ref_tag_id().unwrap(),
        );
        assert_eq!(
            msg_rej.to_string(),
            expected_error_string,
            "expected: {expected_error_string}, got: {msg_rej}\n",
        );
        assert_eq!(
            msg_rej.reject_reason(),
            expected_reject_reason,
            "expected: {}, got: {}\n",
            expected_reject_reason,
            msg_rej.reject_reason(),
        );
        assert_eq!(
            msg_rej.is_business_reject(),
            expected_is_business_reject,
            "Expected is_business_reject to be true\n",
        );
    }

    #[test]
    fn test_invalid_message_type() {
        let expected_error_string = "Invalid MsgType";
        let expected_reject_reason = 11;
        let expected_is_business_reject = false;

        let msg_rej = invalid_message_type();

        assert_eq!(
            msg_rej.to_string(),
            expected_error_string,
            "expected: {expected_error_string}, got: {msg_rej}\n",
        );
        assert_eq!(
            msg_rej.reject_reason(),
            expected_reject_reason,
            "expected: {}, got: {}\n",
            expected_reject_reason,
            msg_rej.reject_reason(),
        );
        assert_eq!(
            msg_rej.ref_tag_id(),
            None,
            "expected: None, got: {}\n",
            msg_rej.ref_tag_id().unwrap(),
        );
        assert_eq!(
            msg_rej.is_business_reject(),
            expected_is_business_reject,
            "Expected is_business_reject to be false\n",
        );
    }

    #[test]
    fn test_unsupported_message_type() {
        let expected_reject_reason = 3;
        let expected_error_string = "Unsupported Message Type";
        let expected_is_business_reject = true;

        let msg_rej = unsupported_message_type();

        assert_eq!(
            msg_rej.ref_tag_id(),
            None,
            "expected: None, got: {}\n",
            msg_rej.ref_tag_id().unwrap(),
        );
        assert_eq!(
            msg_rej.to_string(),
            expected_error_string,
            "expected: {expected_error_string}, got: {msg_rej}\n",
        );
        assert_eq!(
            msg_rej.reject_reason(),
            expected_reject_reason,
            "expected: {}, got: {}\n",
            expected_reject_reason,
            msg_rej.reject_reason(),
        );
        assert_eq!(
            msg_rej.is_business_reject(),
            expected_is_business_reject,
            "Expected is_business_reject to be true\n",
        );
    }

    #[test]
    fn test_tag_not_defined_for_this_message_type() {
        let expected_error_string = "Tag not defined for this message type";
        let expected_reject_reason = 2;
        let expected_ref_tag_id: Tag = 44;
        let expected_is_business_reject = false;

        let msg_rej = tag_not_defined_for_this_message_type(expected_ref_tag_id);

        assert_eq!(
            msg_rej.to_string(),
            expected_error_string,
            "expected: {expected_error_string}, got: {msg_rej}\n",
        );
        assert_eq!(
            msg_rej.reject_reason(),
            expected_reject_reason,
            "expected: {}, got: {}\n",
            expected_reject_reason,
            msg_rej.reject_reason(),
        );
        assert_eq!(
            msg_rej.ref_tag_id(),
            Some(expected_ref_tag_id),
            "expected: {}, got: {}\n",
            expected_ref_tag_id,
            msg_rej.ref_tag_id().unwrap(),
        );
        assert_eq!(
            msg_rej.is_business_reject(),
            expected_is_business_reject,
            "Expected is_business_reject to be false\n",
        );
    }

    #[test]
    fn test_required_tag_missing() {
        let expected_error_string = "Required tag missing";
        let expected_reject_reason = 1;
        let expected_ref_tag_id: Tag = 44;
        let expected_is_business_reject = false;

        let msg_rej = required_tag_missing(expected_ref_tag_id);

        assert_eq!(
            msg_rej.to_string(),
            expected_error_string,
            "expected: {expected_error_string}, got: {msg_rej}\n",
        );
        assert_eq!(
            msg_rej.reject_reason(),
            expected_reject_reason,
            "expected: {}, got: {}\n",
            expected_reject_reason,
            msg_rej.reject_reason(),
        );
        assert_eq!(
            msg_rej.ref_tag_id(),
            Some(expected_ref_tag_id),
            "expected: {}, got: {}\n",
            expected_ref_tag_id,
            msg_rej.ref_tag_id().unwrap(),
        );
        assert_eq!(
            msg_rej.is_business_reject(),
            expected_is_business_reject,
            "Expected is_business_reject to be false\n",
        );
    }

    #[test]
    fn test_tag_specified_without_a_value() {
        let expected_error_string = "Tag specified without a value";
        let expected_reject_reason = 4;
        let expected_ref_tag_id: Tag = 44;
        let expected_is_business_reject = false;

        let msg_rej = tag_specified_without_a_value(expected_ref_tag_id);

        assert_eq!(
            msg_rej.to_string(),
            expected_error_string,
            "expected: {expected_error_string}, got: {msg_rej}\n",
        );
        assert_eq!(
            msg_rej.reject_reason(),
            expected_reject_reason,
            "expected: {}, got: {}\n",
            expected_reject_reason,
            msg_rej.reject_reason(),
        );
        assert_eq!(
            msg_rej.ref_tag_id(),
            Some(expected_ref_tag_id),
            "expected: {}, got: {}\n",
            expected_ref_tag_id,
            msg_rej.ref_tag_id().unwrap(),
        );
        assert_eq!(
            msg_rej.is_business_reject(),
            expected_is_business_reject,
            "Expected is_business_reject to be false\n",
        );
    }

    #[test]
    fn test_invalid_tag_number() {
        let expected_error_string = "Invalid tag number";
        let expected_reject_reason = 0;
        let expected_ref_tag_id: Tag = 44;
        let expected_is_business_reject = false;

        let msg_rej = invalid_tag_number(expected_ref_tag_id);

        assert_eq!(
            msg_rej.to_string(),
            expected_error_string,
            "expected: {expected_error_string}, got: {msg_rej}\n",
        );
        assert_eq!(
            msg_rej.reject_reason(),
            expected_reject_reason,
            "expected: {}, got: {}\n",
            expected_reject_reason,
            msg_rej.reject_reason(),
        );
        assert_eq!(
            msg_rej.ref_tag_id(),
            Some(expected_ref_tag_id),
            "expected: {}, got: {}\n",
            expected_ref_tag_id,
            msg_rej.ref_tag_id().unwrap(),
        );
        assert_eq!(
            msg_rej.is_business_reject(),
            expected_is_business_reject,
            "Expected is_business_reject to be false\n",
        );
    }
}