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}
72
73impl fmt::Display for Error {
74 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75 match self {
76 Error::DuplicateTag { tag } => {
77 write!(f, "tag {tag} is defined more than once")
78 }
79 Error::NonIntegerValue { field, value } => {
80 write!(f, "value '{value}' of field '{field}' is not an integer")
81 }
82 Error::RepeatedCode {
83 field,
84 first,
85 second,
86 } if first == second => {
87 write!(f, "field '{field}' lists code {first} more than once")
88 }
89 Error::RepeatedCode {
90 field,
91 first,
92 second,
93 } => {
94 write!(
95 f,
96 "field '{field}' lists both {first} and {second}, which are the same number"
97 )
98 }
99 Error::DuplicateComponent { component } => {
100 write!(f, "component '{component}' is defined more than once")
101 }
102 Error::DuplicateMsgType {
103 msg_type,
104 first,
105 second,
106 } => {
107 write!(
108 f,
109 "messages '{first}' and '{second}' share MsgType '{msg_type}'"
110 )
111 }
112 Error::UnexpectedLengthTag { field, tag } => {
113 write!(
114 f,
115 "field '{field}' takes its length from tag {tag} but is not a data field"
116 )
117 }
118 Error::UnknownLengthTag { field, tag } => {
119 write!(f, "field '{field}' takes its length from unknown tag {tag}")
120 }
121 Error::NonLengthField {
122 field,
123 length_field,
124 } => {
125 write!(
126 f,
127 "field '{field}' takes its length from '{length_field}', which is not a LENGTH field"
128 )
129 }
130 Error::SharedLengthField {
131 length_field,
132 first,
133 second,
134 } => {
135 write!(
136 f,
137 "fields '{first}' and '{second}' both take their length from '{length_field}'"
138 )
139 }
140 Error::MissingLengthTag { field, context } => {
141 write!(f, "data field '{field}' in {context} has no length field")
142 }
143 Error::UnknownField { context, tag } => {
144 write!(f, "{context} references unknown tag {tag}")
145 }
146 Error::UnknownComponent { context, component } => {
147 write!(f, "{context} references unknown component '{component}'")
148 }
149 Error::CircularComponent { component } => {
150 write!(f, "component '{component}' is part of a reference cycle")
151 }
152 Error::EmptyGroup { context } => {
153 write!(f, "{context} has no members")
154 }
155 }
156 }
157}
158
159impl std::error::Error for Error {}
160
161#[cfg(test)]
162mod tests {
163 use super::*;
164
165 #[test]
166 fn a_shared_msg_type_names_both_messages() {
167 let error = Error::DuplicateMsgType {
168 msg_type: "D".to_owned(),
169 first: "NewOrderSingle".to_owned(),
170 second: "NewOrderCross".to_owned(),
171 };
172
173 assert_eq!(
174 error.to_string(),
175 "messages 'NewOrderSingle' and 'NewOrderCross' share MsgType 'D'"
176 );
177 }
178
179 #[test]
180 fn a_shared_length_field_names_both_data_fields() {
181 let error = Error::SharedLengthField {
182 length_field: "RawDataLength".to_owned(),
183 first: "RawData".to_owned(),
184 second: "SecureData".to_owned(),
185 };
186
187 assert_eq!(
188 error.to_string(),
189 "fields 'RawData' and 'SecureData' both take their length from 'RawDataLength'"
190 );
191 }
192
193 #[test]
194 fn a_repeated_code_reads_as_listed_twice() {
195 let error = Error::RepeatedCode {
196 field: "MatchType".to_owned(),
197 first: "M1".to_owned(),
198 second: "M1".to_owned(),
199 };
200
201 assert_eq!(
202 error.to_string(),
203 "field 'MatchType' lists code M1 more than once"
204 );
205 }
206
207 #[test]
208 fn a_code_spelled_twice_names_both_spellings() {
209 let error = Error::RepeatedCode {
210 field: "PriceType".to_owned(),
211 first: "1".to_owned(),
212 second: "01".to_owned(),
213 };
214
215 assert_eq!(
216 error.to_string(),
217 "field 'PriceType' lists both 1 and 01, which are the same number"
218 );
219 }
220}