refix_dictionary/dictionary/
error.rs1use crate::{MemberContext, Tag};
2use std::fmt;
3
4#[derive(Clone, Debug, Eq, PartialEq)]
7pub enum Error {
8 DuplicateTag {
9 tag: Tag,
10 },
11 NonIntegerValue {
13 field: String,
14 value: String,
15 },
16 RepeatedCode {
19 field: String,
20 first: String,
21 second: String,
22 },
23 DuplicateComponent {
24 component: String,
25 },
26 DuplicateMsgType {
28 msg_type: String,
29 first: String,
30 second: String,
31 },
32 UnexpectedLengthTag {
34 field: String,
35 tag: Tag,
36 },
37 UnknownLengthTag {
38 field: String,
39 tag: Tag,
40 },
41 NonLengthField {
43 field: String,
44 length_field: String,
45 },
46 SharedLengthField {
48 length_field: String,
49 first: String,
50 second: String,
51 },
52 MissingLengthTag {
54 field: String,
55 context: MemberContext,
56 },
57 DuplicateField {
58 tag: Tag,
59 first: MemberContext,
60 second: MemberContext,
61 },
62 UnknownField {
63 tag: Tag,
64 context: MemberContext,
65 },
66 UnknownComponent {
67 component: String,
68 context: MemberContext,
69 },
70 CircularComponent {
71 component: String,
72 },
73 EmptyGroup {
74 context: MemberContext,
75 },
76}
77
78impl fmt::Display for Error {
79 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
80 match self {
81 Error::DuplicateTag { tag } => {
82 write!(f, "tag {tag} is defined more than once")
83 }
84 Error::NonIntegerValue { field, value } => {
85 write!(f, "value '{value}' of field '{field}' is not an integer")
86 }
87 Error::RepeatedCode {
88 field,
89 first,
90 second,
91 } if first == second => {
92 write!(f, "field '{field}' lists code {first} more than once")
93 }
94 Error::RepeatedCode {
95 field,
96 first,
97 second,
98 } => {
99 write!(
100 f,
101 "field '{field}' lists both {first} and {second}, which are the same number"
102 )
103 }
104 Error::DuplicateComponent { component } => {
105 write!(f, "component '{component}' is defined more than once")
106 }
107 Error::DuplicateMsgType {
108 msg_type,
109 first,
110 second,
111 } => {
112 write!(
113 f,
114 "messages '{first}' and '{second}' share MsgType '{msg_type}'"
115 )
116 }
117 Error::UnexpectedLengthTag { field, tag } => {
118 write!(
119 f,
120 "field '{field}' takes its length from tag {tag} but is not a data field"
121 )
122 }
123 Error::UnknownLengthTag { field, tag } => {
124 write!(f, "field '{field}' takes its length from unknown tag {tag}")
125 }
126 Error::NonLengthField {
127 field,
128 length_field,
129 } => {
130 write!(
131 f,
132 "field '{field}' takes its length from '{length_field}', which is not a LENGTH field"
133 )
134 }
135 Error::SharedLengthField {
136 length_field,
137 first,
138 second,
139 } => {
140 write!(
141 f,
142 "fields '{first}' and '{second}' both take their length from '{length_field}'"
143 )
144 }
145 Error::MissingLengthTag { field, context } => {
146 write!(f, "data field '{field}' in {context} has no length field")
147 }
148 Error::DuplicateField { tag, first, second } if first == second => {
149 write!(f, "{first} contains tag {tag} more than once")
150 }
151 Error::DuplicateField { tag, first, second } => {
152 write!(f, "tag {tag} appears in both {first} and {second}")
153 }
154 Error::UnknownField { context, tag } => {
155 write!(f, "{context} references unknown tag {tag}")
156 }
157 Error::UnknownComponent { context, component } => {
158 write!(f, "{context} references unknown component '{component}'")
159 }
160 Error::CircularComponent { component } => {
161 write!(f, "component '{component}' is part of a reference cycle")
162 }
163 Error::EmptyGroup { context } => {
164 write!(f, "{context} has no members")
165 }
166 }
167 }
168}
169
170impl std::error::Error for Error {}
171
172#[cfg(test)]
173mod tests {
174 use super::*;
175
176 #[test]
177 fn a_duplicate_in_one_place_reads_as_contained_twice() {
178 let context = MemberContext::Component("Top".to_owned());
179 let error = Error::DuplicateField {
180 tag: Tag(58),
181 first: context.clone(),
182 second: context,
183 };
184
185 assert_eq!(
186 error.to_string(),
187 "component 'Top' contains tag 58 more than once"
188 );
189 }
190
191 #[test]
192 fn a_shared_msg_type_names_both_messages() {
193 let error = Error::DuplicateMsgType {
194 msg_type: "D".to_owned(),
195 first: "NewOrderSingle".to_owned(),
196 second: "NewOrderCross".to_owned(),
197 };
198
199 assert_eq!(
200 error.to_string(),
201 "messages 'NewOrderSingle' and 'NewOrderCross' share MsgType 'D'"
202 );
203 }
204
205 #[test]
206 fn a_shared_length_field_names_both_data_fields() {
207 let error = Error::SharedLengthField {
208 length_field: "RawDataLength".to_owned(),
209 first: "RawData".to_owned(),
210 second: "SecureData".to_owned(),
211 };
212
213 assert_eq!(
214 error.to_string(),
215 "fields 'RawData' and 'SecureData' both take their length from 'RawDataLength'"
216 );
217 }
218
219 #[test]
220 fn a_repeated_code_reads_as_listed_twice() {
221 let error = Error::RepeatedCode {
222 field: "MatchType".to_owned(),
223 first: "M1".to_owned(),
224 second: "M1".to_owned(),
225 };
226
227 assert_eq!(
228 error.to_string(),
229 "field 'MatchType' lists code M1 more than once"
230 );
231 }
232
233 #[test]
234 fn a_code_spelled_twice_names_both_spellings() {
235 let error = Error::RepeatedCode {
236 field: "PriceType".to_owned(),
237 first: "1".to_owned(),
238 second: "01".to_owned(),
239 };
240
241 assert_eq!(
242 error.to_string(),
243 "field 'PriceType' lists both 1 and 01, which are the same number"
244 );
245 }
246
247 #[test]
248 fn a_duplicate_in_two_places_names_both() {
249 let message = MemberContext::Message("NewOrderSingle".to_owned());
250 let error = Error::DuplicateField {
251 tag: Tag(448),
252 first: message.clone(),
253 second: message.group("NoPartyIDs"),
254 };
255
256 assert_eq!(
257 error.to_string(),
258 "tag 448 appears in both message 'NewOrderSingle' and group 'NoPartyIDs' in message 'NewOrderSingle'"
259 );
260 }
261}