1use refix_message::Tag;
2use std::borrow::Cow;
3use std::fmt;
4
5#[derive(Clone, Debug, Eq, PartialEq)]
11pub struct Spec {
12 pub version: Version,
13 pub header: Vec<Member>,
15 pub messages: Vec<Message>,
16 pub trailer: Vec<Member>,
18 pub fields: Vec<Field>,
19 pub components: Vec<Component>,
20}
21
22#[derive(Clone, Debug, Eq, PartialEq)]
24pub struct Message {
25 pub name: String,
27 pub msg_type: String,
29 pub members: Vec<Member>,
31 pub category: Category,
33}
34
35#[derive(Clone, Debug, Eq, PartialEq)]
37pub struct Field {
38 pub name: String,
39 pub tag: Tag,
40 pub data_type: DataType,
41 pub values: Vec<EnumValue>,
42}
43
44#[derive(Clone, Debug, Eq, PartialEq)]
46pub struct Component {
47 pub name: String,
49 pub members: Vec<Member>,
51}
52
53#[derive(Clone, Debug, Eq, PartialEq)]
57pub enum Member {
58 Field(FieldRef),
59 Component(ComponentRef),
60 Group(Group),
61}
62
63#[derive(Clone, Debug, Eq, PartialEq)]
65pub struct FieldRef {
66 pub tag: Tag,
68 pub is_required: bool,
69}
70
71#[derive(Clone, Debug, Eq, PartialEq)]
73pub struct ComponentRef {
74 pub name: String,
76 pub is_required: bool,
77}
78
79#[derive(Clone, Debug, Eq, PartialEq)]
82pub struct Group {
83 pub count_tag: Tag,
85 pub is_required: bool,
86 pub members: Vec<Member>,
88}
89
90#[derive(Clone, Debug, Eq, Hash, PartialEq)]
95pub enum MemberContext {
96 Message(String),
97 Component(String),
98 Header,
99 Trailer,
100 Group {
102 name: String,
103 parent: Box<MemberContext>,
104 },
105}
106
107impl MemberContext {
108 pub fn group(&self, name: &str) -> MemberContext {
110 MemberContext::Group {
111 name: name.to_owned(),
112 parent: Box::new(self.clone()),
113 }
114 }
115}
116
117impl fmt::Display for MemberContext {
118 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
119 match self {
120 Self::Message(name) => write!(f, "message '{name}'"),
121 Self::Component(name) => write!(f, "component '{name}'"),
122 Self::Header => write!(f, "the header"),
123 Self::Trailer => write!(f, "the trailer"),
124 Self::Group { name, parent } => write!(f, "group '{name}' in {parent}"),
125 }
126 }
127}
128
129#[derive(Clone, Debug, Eq, PartialEq)]
133pub struct EnumValue {
134 pub value: String,
135 pub description: String,
136}
137
138#[derive(Clone, Debug, Eq, PartialEq)]
140pub enum DataType {
141 String,
142 Int,
143 Length,
144 NumInGroup,
145 SeqNum,
146 TagNum,
147 DayOfMonth,
148 Float,
149 Qty,
150 Price,
151 PriceOffset,
152 Amt,
153 Percentage,
154 Char,
155 Boolean,
156 MultipleCharValue,
157 MultipleStringValue,
159 Currency,
160 Exchange,
161 Country,
162 Language,
163 Xid,
164 XidRef,
165 UtcTimestamp,
167 UtcTimeOnly,
168 UtcDateOnly,
170 LocalMktDate,
172 LocalMktTime,
173 MonthYear,
174 TzTimestamp,
175 TzTimeOnly,
176 Data,
177 XmlData,
178 Other(String),
180}
181
182impl fmt::Display for DataType {
185 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
186 let name = match self {
187 Self::String => "STRING",
188 Self::Int => "INT",
189 Self::Length => "LENGTH",
190 Self::NumInGroup => "NUMINGROUP",
191 Self::SeqNum => "SEQNUM",
192 Self::TagNum => "TAGNUM",
193 Self::DayOfMonth => "DAYOFMONTH",
194 Self::Float => "FLOAT",
195 Self::Qty => "QTY",
196 Self::Price => "PRICE",
197 Self::PriceOffset => "PRICEOFFSET",
198 Self::Amt => "AMT",
199 Self::Percentage => "PERCENTAGE",
200 Self::Char => "CHAR",
201 Self::Boolean => "BOOLEAN",
202 Self::MultipleCharValue => "MULTIPLECHARVALUE",
203 Self::MultipleStringValue => "MULTIPLESTRINGVALUE",
204 Self::Currency => "CURRENCY",
205 Self::Exchange => "EXCHANGE",
206 Self::Country => "COUNTRY",
207 Self::Language => "LANGUAGE",
208 Self::Xid => "XID",
209 Self::XidRef => "XIDREF",
210 Self::UtcTimestamp => "UTCTIMESTAMP",
211 Self::UtcTimeOnly => "UTCTIMEONLY",
212 Self::UtcDateOnly => "UTCDATEONLY",
213 Self::LocalMktDate => "LOCALMKTDATE",
214 Self::LocalMktTime => "LOCALMKTTIME",
215 Self::MonthYear => "MONTHYEAR",
216 Self::TzTimestamp => "TZTIMESTAMP",
217 Self::TzTimeOnly => "TZTIMEONLY",
218 Self::Data => "DATA",
219 Self::XmlData => "XMLDATA",
220 Self::Other(name) => name,
221 };
222 f.write_str(name)
223 }
224}
225
226impl DataType {
227 pub fn is_int_based(&self) -> bool {
229 matches!(
230 self,
231 Self::Int
232 | Self::Length
233 | Self::NumInGroup
234 | Self::SeqNum
235 | Self::TagNum
236 | Self::DayOfMonth
237 )
238 }
239
240 pub fn is_float_based(&self) -> bool {
242 matches!(
243 self,
244 Self::Float
245 | Self::Qty
246 | Self::Price
247 | Self::PriceOffset
248 | Self::Amt
249 | Self::Percentage
250 )
251 }
252
253 pub fn is_multiple_value(&self) -> bool {
255 matches!(self, Self::MultipleCharValue | Self::MultipleStringValue)
256 }
257
258 pub(crate) fn canonical_code<'a>(&self, code: &'a str) -> Cow<'a, str> {
262 match code.parse::<i64>() {
263 Ok(number) if self.is_int_based() => Cow::Owned(number.to_string()),
264 _ => Cow::Borrowed(code),
265 }
266 }
267}
268
269#[derive(Clone, Copy, Debug, Eq, PartialEq)]
271pub struct Version {
272 pub protocol: Protocol,
273 pub major: u8,
274 pub minor: u8,
275 pub service_pack: u8,
276}
277
278#[derive(Clone, Copy, Debug, Eq, PartialEq)]
280pub enum Protocol {
281 Fix,
284 Fixt,
287}
288
289#[derive(Clone, Copy, Debug, Eq, PartialEq)]
291pub enum Category {
292 Admin,
294 App,
296}
297
298#[cfg(test)]
299mod tests {
300 use super::*;
301
302 #[test]
303 fn the_int_family_is_int_based() {
304 for data_type in [
305 DataType::Int,
306 DataType::Length,
307 DataType::NumInGroup,
308 DataType::SeqNum,
309 DataType::TagNum,
310 DataType::DayOfMonth,
311 ] {
312 assert!(data_type.is_int_based(), "{data_type:?}");
313 }
314 }
315
316 #[test]
317 fn other_types_are_not_int_based() {
318 for data_type in [
319 DataType::String,
320 DataType::Char,
321 DataType::MultipleStringValue,
322 DataType::Price,
323 DataType::Other("CUSTOM".to_owned()),
324 ] {
325 assert!(!data_type.is_int_based(), "{data_type:?}");
326 }
327 }
328
329 #[test]
330 fn the_float_family_is_float_based() {
331 for data_type in [
332 DataType::Float,
333 DataType::Qty,
334 DataType::Price,
335 DataType::PriceOffset,
336 DataType::Amt,
337 DataType::Percentage,
338 ] {
339 assert!(data_type.is_float_based(), "{data_type:?}");
340 }
341 }
342
343 #[test]
344 fn other_types_are_not_float_based() {
345 for data_type in [
346 DataType::Int,
347 DataType::String,
348 DataType::UtcTimestamp,
349 DataType::Other("CUSTOM".to_owned()),
350 ] {
351 assert!(!data_type.is_float_based(), "{data_type:?}");
352 }
353 }
354
355 #[test]
356 fn the_multiple_value_types_hold_multiple_values() {
357 assert!(DataType::MultipleCharValue.is_multiple_value());
358 assert!(DataType::MultipleStringValue.is_multiple_value());
359 assert!(!DataType::String.is_multiple_value());
360 assert!(!DataType::Char.is_multiple_value());
361 }
362
363 #[test]
364 fn int_codes_are_canonical_as_numbers() {
365 assert_eq!(DataType::Int.canonical_code("01"), "1");
366 assert_eq!(DataType::Int.canonical_code("-007"), "-7");
367 assert_eq!(DataType::Char.canonical_code("01"), "01");
368 }
369
370 #[test]
371 fn a_header_group_names_the_header() {
372 assert_eq!(
373 MemberContext::Header.group("NoHops").to_string(),
374 "group 'NoHops' in the header"
375 );
376 assert_eq!(MemberContext::Trailer.to_string(), "the trailer");
377 }
378
379 #[test]
380 fn a_nested_group_context_names_its_whole_path() {
381 let context = MemberContext::Group {
382 name: "NoPartySubIDs".to_owned(),
383 parent: Box::new(MemberContext::Group {
384 name: "NoPartyIDs".to_owned(),
385 parent: Box::new(MemberContext::Component("Parties".to_owned())),
386 }),
387 };
388
389 assert_eq!(
390 context.to_string(),
391 "group 'NoPartySubIDs' in group 'NoPartyIDs' in component 'Parties'"
392 );
393
394 assert_eq!(
395 context.to_string(),
396 "group 'NoPartySubIDs' in group 'NoPartyIDs' in component 'Parties'"
397 );
398 }
399}