Skip to main content

refix_dictionary/dictionary/
error.rs

1use crate::{MemberContext, Tag};
2use std::fmt;
3
4/// A defect found while resolving a [`crate::Spec`] into a
5/// [`crate::Dictionary`].
6#[derive(Clone, Debug, Eq, PartialEq)]
7pub enum Error {
8    DuplicateTag {
9        tag: Tag,
10    },
11    /// A value listed for an int-based field that is not a FIX integer.
12    NonIntegerValue {
13        field: String,
14        value: String,
15    },
16    /// Two values listed under one code of a field, `1` and `01` being one
17    /// code on an int-based field.
18    RepeatedCode {
19        field: String,
20        first: String,
21        second: String,
22    },
23    DuplicateComponent {
24        component: String,
25    },
26    DuplicateField {
27        tag: Tag,
28        first: MemberContext,
29        second: MemberContext,
30    },
31    UnknownField {
32        tag: Tag,
33        context: MemberContext,
34    },
35    UnknownComponent {
36        component: String,
37        context: MemberContext,
38    },
39    CircularComponent {
40        component: String,
41    },
42    EmptyGroup {
43        context: MemberContext,
44    },
45}
46
47impl fmt::Display for Error {
48    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49        match self {
50            Error::DuplicateTag { tag } => {
51                write!(f, "tag {tag} is defined more than once")
52            }
53            Error::NonIntegerValue { field, value } => {
54                write!(f, "value '{value}' of field '{field}' is not an integer")
55            }
56            Error::RepeatedCode {
57                field,
58                first,
59                second,
60            } if first == second => {
61                write!(f, "field '{field}' lists code {first} more than once")
62            }
63            Error::RepeatedCode {
64                field,
65                first,
66                second,
67            } => {
68                write!(
69                    f,
70                    "field '{field}' lists both {first} and {second}, which are the same number"
71                )
72            }
73            Error::DuplicateComponent { component } => {
74                write!(f, "component '{component}' is defined more than once")
75            }
76            Error::DuplicateField { tag, first, second } if first == second => {
77                write!(f, "{first} contains tag {tag} more than once")
78            }
79            Error::DuplicateField { tag, first, second } => {
80                write!(f, "tag {tag} appears in both {first} and {second}")
81            }
82            Error::UnknownField { context, tag } => {
83                write!(f, "{context} references unknown tag {tag}")
84            }
85            Error::UnknownComponent { context, component } => {
86                write!(f, "{context} references unknown component '{component}'")
87            }
88            Error::CircularComponent { component } => {
89                write!(f, "component '{component}' is part of a reference cycle")
90            }
91            Error::EmptyGroup { context } => {
92                write!(f, "{context} has no members")
93            }
94        }
95    }
96}
97
98impl std::error::Error for Error {}
99
100#[cfg(test)]
101mod tests {
102    use super::*;
103
104    #[test]
105    fn a_duplicate_in_one_place_reads_as_contained_twice() {
106        let context = MemberContext::Component("Top".to_owned());
107        let error = Error::DuplicateField {
108            tag: Tag(58),
109            first: context.clone(),
110            second: context,
111        };
112
113        assert_eq!(
114            error.to_string(),
115            "component 'Top' contains tag 58 more than once"
116        );
117    }
118
119    #[test]
120    fn a_repeated_code_reads_as_listed_twice() {
121        let error = Error::RepeatedCode {
122            field: "MatchType".to_owned(),
123            first: "M1".to_owned(),
124            second: "M1".to_owned(),
125        };
126
127        assert_eq!(
128            error.to_string(),
129            "field 'MatchType' lists code M1 more than once"
130        );
131    }
132
133    #[test]
134    fn a_code_spelled_twice_names_both_spellings() {
135        let error = Error::RepeatedCode {
136            field: "PriceType".to_owned(),
137            first: "1".to_owned(),
138            second: "01".to_owned(),
139        };
140
141        assert_eq!(
142            error.to_string(),
143            "field 'PriceType' lists both 1 and 01, which are the same number"
144        );
145    }
146
147    #[test]
148    fn a_duplicate_in_two_places_names_both() {
149        let message = MemberContext::Message("NewOrderSingle".to_owned());
150        let error = Error::DuplicateField {
151            tag: Tag(448),
152            first: message.clone(),
153            second: message.group("NoPartyIDs"),
154        };
155
156        assert_eq!(
157            error.to_string(),
158            "tag 448 appears in both message 'NewOrderSingle' and group 'NoPartyIDs' in message 'NewOrderSingle'"
159        );
160    }
161}