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 length_tag: Option<Tag>,
43 pub values: Vec<EnumValue>,
44}
45
46#[derive(Clone, Debug, Eq, PartialEq)]
48pub struct Component {
49 pub name: String,
51 pub members: Vec<Member>,
53}
54
55#[derive(Clone, Debug, Eq, PartialEq)]
59pub enum Member {
60 Field(FieldRef),
61 Component(ComponentRef),
62 Group(Group),
63}
64
65#[derive(Clone, Debug, Eq, PartialEq)]
67pub struct FieldRef {
68 pub tag: Tag,
70 pub is_required: bool,
71}
72
73#[derive(Clone, Debug, Eq, PartialEq)]
75pub struct ComponentRef {
76 pub name: String,
78 pub is_required: bool,
79}
80
81#[derive(Clone, Debug, Eq, PartialEq)]
84pub struct Group {
85 pub count_tag: Tag,
87 pub is_required: bool,
88 pub members: Vec<Member>,
90}
91
92#[derive(Clone, Debug, Eq, Hash, PartialEq)]
97pub enum MemberContext {
98 Message(String),
99 Component(String),
100 Header,
101 Trailer,
102 Group {
104 name: String,
105 parent: Box<MemberContext>,
106 },
107}
108
109impl MemberContext {
110 pub fn group(&self, name: &str) -> MemberContext {
112 MemberContext::Group {
113 name: name.to_owned(),
114 parent: Box::new(self.clone()),
115 }
116 }
117}
118
119impl fmt::Display for MemberContext {
120 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
121 match self {
122 Self::Message(name) => write!(f, "message '{name}'"),
123 Self::Component(name) => write!(f, "component '{name}'"),
124 Self::Header => write!(f, "the header"),
125 Self::Trailer => write!(f, "the trailer"),
126 Self::Group { name, parent } => write!(f, "group '{name}' in {parent}"),
127 }
128 }
129}
130
131#[derive(Clone, Debug, Eq, PartialEq)]
135pub struct EnumValue {
136 pub value: String,
137 pub description: String,
138}
139
140#[derive(Clone, Debug, Eq, PartialEq)]
142pub enum DataType {
143 String,
144 Int,
145 Length,
146 NumInGroup,
147 SeqNum,
148 TagNum,
149 DayOfMonth,
150 Float,
151 Qty,
152 Price,
153 PriceOffset,
154 Amt,
155 Percentage,
156 Char,
157 Boolean,
158 MultipleCharValue,
159 MultipleStringValue,
161 Currency,
162 Exchange,
163 Country,
164 Language,
165 Xid,
166 XidRef,
167 UtcTimestamp,
169 UtcTimeOnly,
170 UtcDateOnly,
172 LocalMktDate,
174 LocalMktTime,
175 MonthYear,
176 TzTimestamp,
177 TzTimeOnly,
178 Data,
179 XmlData,
180 Other(String),
182}
183
184impl fmt::Display for DataType {
187 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
188 let name = match self {
189 Self::String => "STRING",
190 Self::Int => "INT",
191 Self::Length => "LENGTH",
192 Self::NumInGroup => "NUMINGROUP",
193 Self::SeqNum => "SEQNUM",
194 Self::TagNum => "TAGNUM",
195 Self::DayOfMonth => "DAYOFMONTH",
196 Self::Float => "FLOAT",
197 Self::Qty => "QTY",
198 Self::Price => "PRICE",
199 Self::PriceOffset => "PRICEOFFSET",
200 Self::Amt => "AMT",
201 Self::Percentage => "PERCENTAGE",
202 Self::Char => "CHAR",
203 Self::Boolean => "BOOLEAN",
204 Self::MultipleCharValue => "MULTIPLECHARVALUE",
205 Self::MultipleStringValue => "MULTIPLESTRINGVALUE",
206 Self::Currency => "CURRENCY",
207 Self::Exchange => "EXCHANGE",
208 Self::Country => "COUNTRY",
209 Self::Language => "LANGUAGE",
210 Self::Xid => "XID",
211 Self::XidRef => "XIDREF",
212 Self::UtcTimestamp => "UTCTIMESTAMP",
213 Self::UtcTimeOnly => "UTCTIMEONLY",
214 Self::UtcDateOnly => "UTCDATEONLY",
215 Self::LocalMktDate => "LOCALMKTDATE",
216 Self::LocalMktTime => "LOCALMKTTIME",
217 Self::MonthYear => "MONTHYEAR",
218 Self::TzTimestamp => "TZTIMESTAMP",
219 Self::TzTimeOnly => "TZTIMEONLY",
220 Self::Data => "DATA",
221 Self::XmlData => "XMLDATA",
222 Self::Other(name) => name,
223 };
224 f.write_str(name)
225 }
226}
227
228impl DataType {
229 pub fn is_int_based(&self) -> bool {
231 matches!(
232 self,
233 Self::Int
234 | Self::Length
235 | Self::NumInGroup
236 | Self::SeqNum
237 | Self::TagNum
238 | Self::DayOfMonth
239 )
240 }
241
242 pub fn is_float_based(&self) -> bool {
244 matches!(
245 self,
246 Self::Float
247 | Self::Qty
248 | Self::Price
249 | Self::PriceOffset
250 | Self::Amt
251 | Self::Percentage
252 )
253 }
254
255 pub fn is_multiple_value(&self) -> bool {
257 matches!(self, Self::MultipleCharValue | Self::MultipleStringValue)
258 }
259
260 pub(crate) fn has_length_field(&self) -> bool {
263 matches!(self, Self::Data | Self::XmlData)
264 }
265
266 pub(crate) fn canonical_code<'a>(&self, code: &'a str) -> Cow<'a, str> {
270 match code.parse::<i64>() {
271 Ok(number) if self.is_int_based() => Cow::Owned(number.to_string()),
272 _ => Cow::Borrowed(code),
273 }
274 }
275}
276
277#[derive(Clone, Copy, Debug, Eq, PartialEq)]
279pub struct Version {
280 pub protocol: Protocol,
281 pub major: u8,
282 pub minor: u8,
283 pub service_pack: u8,
284}
285
286#[derive(Clone, Copy, Debug, Eq, PartialEq)]
288pub enum Protocol {
289 Fix,
292 Fixt,
295}
296
297#[derive(Clone, Copy, Debug, Eq, PartialEq)]
299pub enum Category {
300 Admin,
302 App,
304}
305
306#[cfg(test)]
307mod tests {
308 use super::*;
309
310 #[test]
311 fn the_int_family_is_int_based() {
312 for data_type in [
313 DataType::Int,
314 DataType::Length,
315 DataType::NumInGroup,
316 DataType::SeqNum,
317 DataType::TagNum,
318 DataType::DayOfMonth,
319 ] {
320 assert!(data_type.is_int_based(), "{data_type:?}");
321 }
322 }
323
324 #[test]
325 fn other_types_are_not_int_based() {
326 for data_type in [
327 DataType::String,
328 DataType::Char,
329 DataType::MultipleStringValue,
330 DataType::Price,
331 DataType::Other("CUSTOM".to_owned()),
332 ] {
333 assert!(!data_type.is_int_based(), "{data_type:?}");
334 }
335 }
336
337 #[test]
338 fn the_float_family_is_float_based() {
339 for data_type in [
340 DataType::Float,
341 DataType::Qty,
342 DataType::Price,
343 DataType::PriceOffset,
344 DataType::Amt,
345 DataType::Percentage,
346 ] {
347 assert!(data_type.is_float_based(), "{data_type:?}");
348 }
349 }
350
351 #[test]
352 fn other_types_are_not_float_based() {
353 for data_type in [
354 DataType::Int,
355 DataType::String,
356 DataType::UtcTimestamp,
357 DataType::Other("CUSTOM".to_owned()),
358 ] {
359 assert!(!data_type.is_float_based(), "{data_type:?}");
360 }
361 }
362
363 #[test]
364 fn the_multiple_value_types_hold_multiple_values() {
365 assert!(DataType::MultipleCharValue.is_multiple_value());
366 assert!(DataType::MultipleStringValue.is_multiple_value());
367 assert!(!DataType::String.is_multiple_value());
368 assert!(!DataType::Char.is_multiple_value());
369 }
370
371 #[test]
372 fn int_codes_are_canonical_as_numbers() {
373 assert_eq!(DataType::Int.canonical_code("01"), "1");
374 assert_eq!(DataType::Int.canonical_code("-007"), "-7");
375 assert_eq!(DataType::Char.canonical_code("01"), "01");
376 }
377
378 #[test]
379 fn a_header_group_names_the_header() {
380 assert_eq!(
381 MemberContext::Header.group("NoHops").to_string(),
382 "group 'NoHops' in the header"
383 );
384 assert_eq!(MemberContext::Trailer.to_string(), "the trailer");
385 }
386
387 #[test]
388 fn a_nested_group_context_names_its_whole_path() {
389 let context = MemberContext::Group {
390 name: "NoPartySubIDs".to_owned(),
391 parent: Box::new(MemberContext::Group {
392 name: "NoPartyIDs".to_owned(),
393 parent: Box::new(MemberContext::Component("Parties".to_owned())),
394 }),
395 };
396
397 assert_eq!(
398 context.to_string(),
399 "group 'NoPartySubIDs' in group 'NoPartyIDs' in component 'Parties'"
400 );
401
402 assert_eq!(
403 context.to_string(),
404 "group 'NoPartySubIDs' in group 'NoPartyIDs' in component 'Parties'"
405 );
406 }
407}