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 UnknownField {
58 tag: Tag,
59 context: MemberContext,
60 },
61 UnknownComponent {
62 component: String,
63 context: MemberContext,
64 },
65 CircularComponent {
66 component: String,
67 },
68 EmptyGroup {
69 context: MemberContext,
70 },
71 AmbiguousTag {
77 tag: Tag,
78 first: MemberContext,
79 second: MemberContext,
80 },
81}
82
83impl fmt::Display for Error {
84 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85 match self {
86 Error::DuplicateTag { tag } => {
87 write!(f, "tag {tag} is defined more than once")
88 }
89 Error::NonIntegerValue { field, value } => {
90 write!(f, "value '{value}' of field '{field}' is not an integer")
91 }
92 Error::RepeatedCode {
93 field,
94 first,
95 second,
96 } if first == second => {
97 write!(f, "field '{field}' lists code {first} more than once")
98 }
99 Error::RepeatedCode {
100 field,
101 first,
102 second,
103 } => {
104 write!(
105 f,
106 "field '{field}' lists both {first} and {second}, which are the same number"
107 )
108 }
109 Error::DuplicateComponent { component } => {
110 write!(f, "component '{component}' is defined more than once")
111 }
112 Error::DuplicateMsgType {
113 msg_type,
114 first,
115 second,
116 } => {
117 write!(
118 f,
119 "messages '{first}' and '{second}' share MsgType '{msg_type}'"
120 )
121 }
122 Error::UnexpectedLengthTag { field, tag } => {
123 write!(
124 f,
125 "field '{field}' takes its length from tag {tag} but is not a data field"
126 )
127 }
128 Error::UnknownLengthTag { field, tag } => {
129 write!(f, "field '{field}' takes its length from unknown tag {tag}")
130 }
131 Error::NonLengthField {
132 field,
133 length_field,
134 } => {
135 write!(
136 f,
137 "field '{field}' takes its length from '{length_field}', which is not a LENGTH field"
138 )
139 }
140 Error::SharedLengthField {
141 length_field,
142 first,
143 second,
144 } => {
145 write!(
146 f,
147 "fields '{first}' and '{second}' both take their length from '{length_field}'"
148 )
149 }
150 Error::MissingLengthTag { field, context } => {
151 write!(f, "data field '{field}' in {context} has no length field")
152 }
153 Error::UnknownField { context, tag } => {
154 write!(f, "{context} references unknown tag {tag}")
155 }
156 Error::UnknownComponent { context, component } => {
157 write!(f, "{context} references unknown component '{component}'")
158 }
159 Error::CircularComponent { component } => {
160 write!(f, "component '{component}' is part of a reference cycle")
161 }
162 Error::EmptyGroup { context } => {
163 write!(f, "{context} has no members")
164 }
165 Error::AmbiguousTag { tag, first, second } if first == second => {
166 write!(
167 f,
168 "tag {tag} counts a group in {first}, which lists it again"
169 )
170 }
171 Error::AmbiguousTag { tag, first, second } => {
172 write!(f, "tag {tag} is listed in {first} and in {second}")
173 }
174 }
175 }
176}
177
178impl std::error::Error for Error {}
179
180#[cfg(test)]
181mod tests {
182 use super::*;
183
184 #[test]
185 fn a_shared_msg_type_names_both_messages() {
186 let error = Error::DuplicateMsgType {
187 msg_type: "D".to_owned(),
188 first: "NewOrderSingle".to_owned(),
189 second: "NewOrderCross".to_owned(),
190 };
191
192 assert_eq!(
193 error.to_string(),
194 "messages 'NewOrderSingle' and 'NewOrderCross' share MsgType 'D'"
195 );
196 }
197
198 #[test]
199 fn a_shared_length_field_names_both_data_fields() {
200 let error = Error::SharedLengthField {
201 length_field: "RawDataLength".to_owned(),
202 first: "RawData".to_owned(),
203 second: "SecureData".to_owned(),
204 };
205
206 assert_eq!(
207 error.to_string(),
208 "fields 'RawData' and 'SecureData' both take their length from 'RawDataLength'"
209 );
210 }
211
212 #[test]
213 fn a_repeated_code_reads_as_listed_twice() {
214 let error = Error::RepeatedCode {
215 field: "MatchType".to_owned(),
216 first: "M1".to_owned(),
217 second: "M1".to_owned(),
218 };
219
220 assert_eq!(
221 error.to_string(),
222 "field 'MatchType' lists code M1 more than once"
223 );
224 }
225
226 #[test]
227 fn a_code_spelled_twice_names_both_spellings() {
228 let error = Error::RepeatedCode {
229 field: "PriceType".to_owned(),
230 first: "1".to_owned(),
231 second: "01".to_owned(),
232 };
233
234 assert_eq!(
235 error.to_string(),
236 "field 'PriceType' lists both 1 and 01, which are the same number"
237 );
238 }
239
240 #[test]
241 fn a_tag_inside_a_group_and_outside_it_names_both_places() {
242 let message = MemberContext::Message("NewOrderSingle".to_owned());
243 let error = Error::AmbiguousTag {
244 tag: Tag(448),
245 first: message.clone(),
246 second: message.group("NoPartyIDs"),
247 };
248
249 assert_eq!(
250 error.to_string(),
251 "tag 448 is listed in message 'NewOrderSingle' and in group 'NoPartyIDs' in message \
252 'NewOrderSingle'"
253 );
254 }
255
256 #[test]
257 fn a_count_tag_listed_again_names_its_one_place() {
258 let message = MemberContext::Message("NewOrderSingle".to_owned());
259 let error = Error::AmbiguousTag {
260 tag: Tag(453),
261 first: message.clone(),
262 second: message,
263 };
264
265 assert_eq!(
266 error.to_string(),
267 "tag 453 counts a group in message 'NewOrderSingle', which lists it again"
268 );
269 }
270}