fixer 0.11.0

A full featured messaging engine for the FIX protocol implemented in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
use crate::tag::Tag;
use rustc_hash::FxHashMap;
use std::collections::HashSet;
use std::fmt::Write as _;

/// Bidirectional abbreviation maps and message structure for FIXML encoding.
///
/// Maps between FIX tag numbers / field names and their FIXML abbreviated
/// names, plus message→component→field structure from `MsgContents.xml`.
#[derive(Debug, Clone, Default)]
pub struct FixmlAbbreviations {
    /// Tag number → abbreviated XML attribute name (e.g., 55 → "Sym")
    pub tag_to_abbr: FxHashMap<Tag, String>,
    /// Abbreviated XML attribute name → tag number (e.g., "Sym" → 55)
    pub abbr_to_tag: FxHashMap<String, Tag>,
    /// `MsgType` code → abbreviated XML element name (e.g., "D" → "Order")
    pub msg_type_to_abbr: FxHashMap<String, String>,
    /// Abbreviated XML element name → `MsgType` code (e.g., "Order" → "D")
    pub abbr_to_msg_type: FxHashMap<String, String>,
    /// Component name → abbreviated XML element name (e.g., "Instrument" → "Instrmt")
    pub component_to_abbr: FxHashMap<String, String>,
    /// Abbreviated XML element name → component name (e.g., "Instrmt" → "Instrument")
    pub abbr_to_component: FxHashMap<String, String>,
    /// `MsgType` code → `NotReqXML` flag (true = session message, skip in FIXML)
    pub msg_not_req_xml: FxHashMap<String, bool>,
    /// FIX version string (e.g., "FIX.4.4")
    pub fix_version: String,

    // --- Message structure (from MsgContents.xml) ---

    /// `ComponentID` → ordered list of content entries for that message/component.
    pub contents: FxHashMap<String, Vec<ContentEntry>>,
    /// `MsgType` → `ComponentID` (from `Messages.xml`)
    pub msg_type_to_component_id: FxHashMap<String, String>,
    /// Component name → `ComponentID` (from `Components.xml`)
    pub component_name_to_id: FxHashMap<String, String>,
    /// Component name → `ComponentType` (e.g., "Block", "`BlockRepeating`")
    pub component_type: FxHashMap<String, String>,
    /// Tags that belong to a component (component name → set of tags).
    /// Built by resolving `MsgContents` entries for each component.
    pub component_tags: FxHashMap<String, HashSet<Tag>>,
    /// Every tag reachable from a component, including through its
    /// sub-components. Used to find where a repeating group's run of fields
    /// ends when walking a flattened message.
    pub component_all_tags: FxHashMap<String, HashSet<Tag>>,
}

/// A single entry from `MsgContents.xml`.
#[derive(Debug, Clone)]
pub struct ContentEntry {
    /// Either a tag number (field) or a component name.
    pub tag_text: String,
    /// True if `tag_text` is numeric (a field tag), false if it's a component name.
    pub is_field: bool,
    /// Nesting level (0 = top-level, 1+ = inside repeating group).
    pub indent: usize,
    /// Ordering position.
    pub position: usize,
    /// Required flag.
    pub required: bool,
}

impl FixmlAbbreviations {
    /// Build from FIX repository XML files.
    pub fn from_fix_repository(base_path: &str) -> Result<Self, String> {
        let mut abbr = Self::default();

        load_field_abbreviations(&format!("{base_path}/Fields.xml"), &mut abbr)?;
        load_message_abbreviations(&format!("{base_path}/Messages.xml"), &mut abbr)?;
        load_component_abbreviations(&format!("{base_path}/Components.xml"), &mut abbr)?;
        load_msg_contents(&format!("{base_path}/MsgContents.xml"), &mut abbr)?;
        build_component_tags(&mut abbr);

        Ok(abbr)
    }

    /// Loads the abbreviations bundled with the crate for a FIX version.
    ///
    /// Supported versions are `FIX.4.4`, `FIX.5.0`, `FIX.5.0SP1`,
    /// `FIX.5.0SP2` and `FIXT.1.1` — the FIX repository defines the
    /// `AbbrName` values FIXML is built on only from FIX.4.4 onward.
    ///
    /// The FIX repository these are derived from is not redistributable in
    /// full and is ~7 MB of XML per edition, so the fields FIXML actually
    /// needs are extracted into a compact table under `fixer/data/fixml/` and
    /// embedded here. Use [`from_fix_repository`](Self::from_fix_repository)
    /// to load a repository edition this crate does not bundle.
    pub fn bundled(version: &str) -> Result<Self, String> {
        let raw = match version {
            "FIX.4.4" => include_str!("../../../data/fixml/FIX.4.4.tsv"),
            "FIX.5.0" => include_str!("../../../data/fixml/FIX.5.0.tsv"),
            "FIX.5.0SP1" => include_str!("../../../data/fixml/FIX.5.0SP1.tsv"),
            "FIX.5.0SP2" => include_str!("../../../data/fixml/FIX.5.0SP2.tsv"),
            "FIXT.1.1" => include_str!("../../../data/fixml/FIXT.1.1.tsv"),
            "FIX.4.0" | "FIX.4.1" | "FIX.4.2" | "FIX.4.3" => {
                return Err(format!(
                    "{version} has no FIXML abbreviations: the FIX repository \
                     only defines AbbrName from FIX.4.4 onward"
                ));
            }
            other => return Err(format!("no bundled FIXML abbreviations for {other}")),
        };
        Self::from_compact(raw)
    }

    /// Serializes the primary data to the compact table format read by
    /// [`from_compact`](Self::from_compact).
    ///
    /// Only the data loaded from the repository is written; the reverse
    /// lookups and `component_tags` are rebuilt on load.
    pub fn to_compact(&self) -> String {
        let mut out = String::new();
        out.push_str("V\t");
        out.push_str(&self.fix_version);
        out.push('\n');

        let mut fields: Vec<(&Tag, &String)> = self.tag_to_abbr.iter().collect();
        fields.sort_by_key(|(t, _)| **t);
        for (tag, abbr) in fields {
            let _ = writeln!(out, "F\t{tag}\t{abbr}");
        }

        let mut msg_types: Vec<&String> = self
            .msg_not_req_xml
            .keys()
            .chain(self.msg_type_to_abbr.keys())
            .chain(self.msg_type_to_component_id.keys())
            .collect();
        msg_types.sort();
        msg_types.dedup();
        for mt in msg_types {
            let abbr = self.msg_type_to_abbr.get(mt).cloned().unwrap_or_default();
            let cid = self
                .msg_type_to_component_id
                .get(mt)
                .cloned()
                .unwrap_or_default();
            let nr = u8::from(*self.msg_not_req_xml.get(mt).unwrap_or(&false));
            let _ = writeln!(out, "M\t{mt}\t{abbr}\t{cid}\t{nr}");
        }

        let mut comps: Vec<&String> = self
            .component_name_to_id
            .keys()
            .chain(self.component_to_abbr.keys())
            .chain(self.component_type.keys())
            .collect();
        comps.sort();
        comps.dedup();
        for name in comps {
            let abbr = self.component_to_abbr.get(name).cloned().unwrap_or_default();
            let cid = self
                .component_name_to_id
                .get(name)
                .cloned()
                .unwrap_or_default();
            let ct = self.component_type.get(name).cloned().unwrap_or_default();
            let _ = writeln!(out, "C\t{name}\t{abbr}\t{cid}\t{ct}");
        }

        let mut cids: Vec<&String> = self.contents.keys().collect();
        cids.sort();
        for cid in cids {
            for e in &self.contents[cid] {
                let reqd = u8::from(e.required);
                let _ = writeln!(
                    out,
                    "X\t{cid}\t{}\t{}\t{}\t{reqd}",
                    e.tag_text, e.indent, e.position
                );
            }
        }

        out
    }

    /// Parses the compact table format written by
    /// [`to_compact`](Self::to_compact).
    pub fn from_compact(raw: &str) -> Result<Self, String> {
        let mut abbr = Self::default();

        for (n, line) in raw.lines().enumerate() {
            if line.is_empty() {
                continue;
            }
            let f: Vec<&str> = line.split('\t').collect();
            let bad = || format!("malformed FIXML abbreviation table at line {}", n + 1);
            match f[0] {
                "V" if f.len() == 2 => abbr.fix_version = f[1].to_string(),
                "F" if f.len() == 3 => {
                    let tag: Tag = f[1].parse().map_err(|_| bad())?;
                    abbr.add_field(tag, f[2]);
                }
                "M" if f.len() == 5 => {
                    if !f[2].is_empty() {
                        abbr.add_message(f[1], f[2]);
                    }
                    if !f[3].is_empty() {
                        abbr.msg_type_to_component_id
                            .insert(f[1].to_string(), f[3].to_string());
                    }
                    abbr.msg_not_req_xml.insert(f[1].to_string(), f[4] == "1");
                }
                "C" if f.len() == 5 => {
                    if !f[2].is_empty() {
                        abbr.add_component(f[1], f[2]);
                    }
                    if !f[3].is_empty() {
                        abbr.component_name_to_id
                            .insert(f[1].to_string(), f[3].to_string());
                    }
                    if !f[4].is_empty() {
                        abbr.component_type
                            .insert(f[1].to_string(), f[4].to_string());
                    }
                }
                "X" if f.len() == 6 => {
                    let entry = ContentEntry {
                        tag_text: f[2].to_string(),
                        is_field: f[2].parse::<Tag>().is_ok(),
                        indent: f[3].parse().map_err(|_| bad())?,
                        position: f[4].parse().map_err(|_| bad())?,
                        required: f[5] == "1",
                    };
                    abbr.contents.entry(f[1].to_string()).or_default().push(entry);
                }
                _ => return Err(bad()),
            }
        }

        build_component_tags(&mut abbr);
        Ok(abbr)
    }

    pub fn new() -> Self {
        Self::default()
    }

    pub fn add_field(&mut self, tag: Tag, abbr_name: &str) {
        self.tag_to_abbr.insert(tag, abbr_name.to_string());
        self.abbr_to_tag
            .entry(abbr_name.to_string())
            .or_insert(tag);
    }

    pub fn add_message(&mut self, msg_type: &str, abbr_name: &str) {
        self.msg_type_to_abbr
            .insert(msg_type.to_string(), abbr_name.to_string());
        self.abbr_to_msg_type
            .insert(abbr_name.to_string(), msg_type.to_string());
    }

    pub fn add_component(&mut self, component_name: &str, abbr_name: &str) {
        self.component_to_abbr
            .insert(component_name.to_string(), abbr_name.to_string());
        self.abbr_to_component
            .insert(abbr_name.to_string(), component_name.to_string());
    }

    /// Get the ordered component entries for a given `MsgType`.
    pub fn msg_contents(&self, msg_type: &str) -> Option<&Vec<ContentEntry>> {
        let cid = self.msg_type_to_component_id.get(msg_type)?;
        self.contents.get(cid)
    }

    /// Get the ordered entries for a component by name.
    pub fn component_contents(&self, component_name: &str) -> Option<&Vec<ContentEntry>> {
        let cid = self.component_name_to_id.get(component_name)?;
        self.contents.get(cid)
    }

    /// Check if a component is a repeating group (`BlockRepeating`).
    /// The nesting level a component's own members sit at.
    ///
    /// A repeating group puts its `NumInGroup` counter at level 0 and its
    /// members at level 1; a plain block has its fields at level 0.
    pub fn member_indent(&self, component_name: &str) -> usize {
        usize::from(self.is_repeating(component_name))
    }

    /// The `NumInGroup` tag of a repeating component, i.e. its single
    /// level-0 field entry.
    pub fn group_counter_tag(&self, component_name: &str) -> Option<Tag> {
        if !self.is_repeating(component_name) {
            return None;
        }
        self.component_contents(component_name)?
            .iter()
            .find(|e| e.is_field && e.indent == 0)
            .and_then(|e| e.tag_text.parse::<Tag>().ok())
    }

    /// The component's own field tags, in repository order.
    ///
    /// Order matters: the first is a repeating group's delimiter, and it also
    /// keeps generated XML attribute order stable.
    pub fn member_fields(&self, component_name: &str) -> Vec<Tag> {
        let indent = self.member_indent(component_name);
        self.component_contents(component_name)
            .map(|entries| {
                entries
                    .iter()
                    .filter(|e| e.is_field && e.indent == indent)
                    .filter_map(|e| e.tag_text.parse::<Tag>().ok())
                    .collect()
            })
            .unwrap_or_default()
    }

    /// The component's own sub-components, in repository order.
    pub fn member_components(&self, component_name: &str) -> Vec<String> {
        let indent = self.member_indent(component_name);
        self.component_contents(component_name)
            .map(|entries| {
                entries
                    .iter()
                    .filter(|e| !e.is_field && e.indent == indent)
                    .map(|e| e.tag_text.clone())
                    .collect()
            })
            .unwrap_or_default()
    }

    /// Whether a component repeats, i.e. is a FIX repeating group.
    ///
    /// Decided by structure, not by `ComponentType`. The repository is not
    /// consistent about the type: `Hop` is an `ImplicitBlock` yet repeats, FIX
    /// 5.0 adds `OptimisedImplicitBlockRepeating`, and FIXT.1.1 has an
    /// `ImplicitBlockRepeating` that does not. What every repeating component
    /// does share is the layout — the `NumInGroup` counter at indent 0 and the
    /// members below it.
    pub fn is_repeating(&self, component_name: &str) -> bool {
        if let Some(entries) = self.component_contents(component_name) {
            return entries.iter().any(|e| e.indent > 0);
        }
        self.component_type
            .get(component_name)
            .is_some_and(|t| t.contains("Repeating"))
    }
}

// ---------------------------------------------------------------------------
// Repository XML loaders
// ---------------------------------------------------------------------------

fn parse_xml(path: &str) -> Result<fastxml::XmlDocument, String> {
    let content =
        std::fs::read_to_string(path).map_err(|e| format!("Failed to read {path}: {e}"))?;
    fastxml::Parser::from(content.as_bytes())
        .parse()
        .map_err(|e| format!("Failed to parse {path}: {e}"))
}

fn child_text(node: &fastxml::XmlNode, name: &str) -> Option<String> {
    node.get_child_elements()
        .into_iter()
        .find(|c| c.get_name() == name)
        .and_then(|c| c.get_content())
}

fn load_field_abbreviations(path: &str, abbr: &mut FixmlAbbreviations) -> Result<(), String> {
    let doc = parse_xml(path)?;
    let root = doc
        .get_root_element()
        .map_err(|e| format!("No root in {path}: {e}"))?;

    for node in root.get_child_elements() {
        if node.get_name() != "Field" {
            continue;
        }
        if let (Some(tag), Some(a)) = (
            child_text(&node, "Tag").and_then(|s| s.parse::<Tag>().ok()),
            child_text(&node, "AbbrName"),
        ) {
            abbr.add_field(tag, &a);
        }
    }
    Ok(())
}

fn load_message_abbreviations(path: &str, abbr: &mut FixmlAbbreviations) -> Result<(), String> {
    let doc = parse_xml(path)?;
    let root = doc
        .get_root_element()
        .map_err(|e| format!("No root in {path}: {e}"))?;

    // Capture FIX version from the root element attributes.
    if let Some(v) = root.get_attribute("version") {
        abbr.fix_version = v;
    }

    for node in root.get_child_elements() {
        if node.get_name() != "Message" {
            continue;
        }
        let msg_type = child_text(&node, "MsgType");
        let abbr_name = child_text(&node, "AbbrName");
        let component_id = child_text(&node, "ComponentID");
        let not_req_xml = child_text(&node, "NotReqXML");

        if let (Some(mt), Some(a)) = (&msg_type, &abbr_name) {
            abbr.add_message(mt, a);
        }
        if let (Some(mt), Some(cid)) = (&msg_type, &component_id) {
            abbr.msg_type_to_component_id
                .insert(mt.clone(), cid.clone());
        }
        if let Some(mt) = &msg_type {
            let is_not_req = not_req_xml.as_deref() == Some("1");
            abbr.msg_not_req_xml.insert(mt.clone(), is_not_req);
        }
    }
    Ok(())
}

fn load_component_abbreviations(path: &str, abbr: &mut FixmlAbbreviations) -> Result<(), String> {
    let doc = parse_xml(path)?;
    let root = doc
        .get_root_element()
        .map_err(|e| format!("No root in {path}: {e}"))?;

    for node in root.get_child_elements() {
        if node.get_name() != "Component" {
            continue;
        }
        let name = child_text(&node, "Name");
        let abbr_name = child_text(&node, "AbbrName");
        let component_id = child_text(&node, "ComponentID");
        let comp_type = child_text(&node, "ComponentType");

        if let (Some(n), Some(a)) = (&name, &abbr_name) {
            abbr.add_component(n, a);
        }
        if let (Some(n), Some(cid)) = (&name, &component_id) {
            abbr.component_name_to_id.insert(n.clone(), cid.clone());
        }
        if let (Some(n), Some(ct)) = (&name, &comp_type) {
            abbr.component_type.insert(n.clone(), ct.clone());
        }
    }
    Ok(())
}

fn load_msg_contents(path: &str, abbr: &mut FixmlAbbreviations) -> Result<(), String> {
    let doc = parse_xml(path)?;
    let root = doc
        .get_root_element()
        .map_err(|e| format!("No root in {path}: {e}"))?;

    for node in root.get_child_elements() {
        if node.get_name() != "MsgContent" {
            continue;
        }
        let Some(cid) = child_text(&node, "ComponentID") else {
            continue;
        };
        let Some(tag_text) = child_text(&node, "TagText") else {
            continue;
        };
        let indent: usize = child_text(&node, "Indent")
            .and_then(|s| s.parse().ok())
            .unwrap_or(0);
        let position: usize = child_text(&node, "Position")
            .and_then(|s| s.parse().ok())
            .unwrap_or(0);
        let required = child_text(&node, "Reqd").as_deref() == Some("1");
        let is_field = tag_text.chars().next().is_some_and(|c| c.is_ascii_digit());

        abbr.contents.entry(cid).or_default().push(ContentEntry {
            tag_text,
            is_field,
            indent,
            position,
            required,
        });
    }

    // Sort each component's entries by position.
    for entries in abbr.contents.values_mut() {
        entries.sort_by_key(|e| e.position);
    }

    Ok(())
}

/// Build the `component_tags` map: for each component, collect the set of
/// field tags that belong directly to it (Indent=0 fields).
/// Every tag reachable from `component`, following sub-components.
fn collect_all_tags(
    abbr: &FixmlAbbreviations,
    component: &str,
    seen: &mut HashSet<String>,
    out: &mut HashSet<Tag>,
) {
    if !seen.insert(component.to_string()) {
        return;
    }
    if let Some(entries) = abbr.component_contents(component) {
        for entry in entries {
            if entry.is_field {
                if let Ok(tag) = entry.tag_text.parse::<Tag>() {
                    out.insert(tag);
                }
            } else {
                collect_all_tags(abbr, &entry.tag_text, seen, out);
            }
        }
    }
}

fn build_component_tags(abbr: &mut FixmlAbbreviations) {
    // Clone component_name_to_id to avoid borrow issues.
    let comp_ids: Vec<(String, String)> = abbr
        .component_name_to_id
        .iter()
        .map(|(n, cid)| (n.clone(), cid.clone()))
        .collect();

    for (comp_name, cid) in &comp_ids {
        let mut tags = HashSet::new();
        if let Some(entries) = abbr.contents.get(cid) {
            for entry in entries {
                if entry.is_field
                    && entry.indent == 0
                    && let Ok(tag) = entry.tag_text.parse::<Tag>()
                {
                    tags.insert(tag);
                }
            }
        }
        abbr.component_tags.insert(comp_name.clone(), tags);
    }

    for (comp_name, _) in &comp_ids {
        let mut all = HashSet::new();
        collect_all_tags(abbr, comp_name, &mut HashSet::new(), &mut all);
        abbr.component_all_tags.insert(comp_name.clone(), all);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_manual_abbreviations() {
        let mut abbr = FixmlAbbreviations::new();
        abbr.add_field(55, "Sym");
        abbr.add_field(44, "Px");
        abbr.add_message("D", "Order");
        abbr.add_component("Instrument", "Instrmt");

        assert_eq!(abbr.tag_to_abbr.get(&55).unwrap(), "Sym");
        assert_eq!(abbr.abbr_to_tag.get("Px").unwrap(), &44);
        assert_eq!(abbr.msg_type_to_abbr.get("D").unwrap(), "Order");
        assert_eq!(abbr.abbr_to_msg_type.get("Order").unwrap(), "D");
        assert_eq!(abbr.component_to_abbr.get("Instrument").unwrap(), "Instrmt");
        assert_eq!(abbr.abbr_to_component.get("Instrmt").unwrap(), "Instrument");
    }

    /// The bundled tables are the only abbreviation source most users have, so
    /// this must exercise them directly rather than skipping when the FIX
    /// repository is absent.
    #[test]
    fn test_bundled_fix44() {
        let abbr = FixmlAbbreviations::bundled("FIX.4.4").unwrap();

        assert_eq!(abbr.tag_to_abbr.get(&55).unwrap(), "Sym");
        assert_eq!(abbr.tag_to_abbr.get(&44).unwrap(), "Px");
        assert_eq!(abbr.tag_to_abbr.get(&1).unwrap(), "Acct");
        assert_eq!(abbr.abbr_to_tag.get("Sym").unwrap(), &55);

        assert_eq!(abbr.msg_type_to_abbr.get("D").unwrap(), "Order");
        assert_eq!(abbr.msg_type_to_abbr.get("8").unwrap(), "ExecRpt");

        assert_eq!(abbr.component_to_abbr.get("Instrument").unwrap(), "Instrmt");
        assert_eq!(abbr.component_to_abbr.get("Parties").unwrap(), "Pty");

        let contents = abbr.msg_contents("D").unwrap();
        assert!(!contents.is_empty());
        assert_eq!(contents[0].tag_text, "StandardHeader");
        assert!(!contents[0].is_field);

        let instrmt_tags = abbr.component_tags.get("Instrument").unwrap();
        assert!(instrmt_tags.contains(&55));
        assert!(instrmt_tags.contains(&48));

        assert!(abbr.is_repeating("Parties"));
        assert!(!abbr.is_repeating("Instrument"));

        assert_eq!(abbr.msg_not_req_xml.get("0"), Some(&true)); // Heartbeat
        assert_eq!(abbr.msg_not_req_xml.get("D"), Some(&false)); // NewOrderSingle

        assert_eq!(abbr.fix_version, "FIX.4.4");
    }

    #[test]
    fn test_bundled_all_versions() {
        for version in [
            "FIX.4.4",
            "FIX.5.0",
            "FIX.5.0SP1",
            "FIX.5.0SP2",
            "FIXT.1.1",
        ] {
            let abbr = FixmlAbbreviations::bundled(version)
                .unwrap_or_else(|e| panic!("{version}: {e}"));
            assert_eq!(abbr.fix_version, version);
            assert!(!abbr.tag_to_abbr.is_empty(), "{version} has no fields");
            assert!(!abbr.contents.is_empty(), "{version} has no contents");
        }
    }

    #[test]
    fn test_bundled_rejects_versions_without_abbreviations() {
        // The repository defines AbbrName only from FIX.4.4 onward; bundling a
        // table for these would silently fall back to numeric attribute names.
        for version in ["FIX.4.0", "FIX.4.1", "FIX.4.2", "FIX.4.3"] {
            let err = FixmlAbbreviations::bundled(version).unwrap_err();
            assert!(err.contains("FIX.4.4"), "unexpected error: {err}");
        }
        assert!(FixmlAbbreviations::bundled("FIX.9.9").is_err());
    }

    #[test]
    fn test_compact_round_trip() {
        let abbr = FixmlAbbreviations::bundled("FIX.4.4").unwrap();
        let once = abbr.to_compact();
        let reparsed = FixmlAbbreviations::from_compact(&once).unwrap();
        assert_eq!(once, reparsed.to_compact());
    }

    /// Keeps the vendored tables honest: they must reproduce exactly what a
    /// FIX repository checkout produces.
    ///
    /// The repository is not redistributable and so is not in this tree.
    /// Point `FIX_REPOSITORY` at a copy to run this; without it there is
    /// nothing to compare against and the test is skipped. Regenerate with
    /// `make fixml-abbr FIX_REPOSITORY=...`.
    #[test]
    fn test_bundled_matches_fix_repository() {
        let Ok(repo) = std::env::var("FIX_REPOSITORY") else {
            return;
        };
        for version in [
            "FIX.4.4",
            "FIX.5.0",
            "FIX.5.0SP1",
            "FIX.5.0SP2",
            "FIXT.1.1",
        ] {
            let base = format!("{repo}/{version}/Base");
            if !std::path::Path::new(&format!("{base}/Fields.xml")).exists() {
                continue;
            }
            let from_repo = FixmlAbbreviations::from_fix_repository(&base).unwrap();
            let bundled = FixmlAbbreviations::bundled(version).unwrap();
            assert_eq!(
                from_repo.to_compact(),
                bundled.to_compact(),
                "{version}: fixer/data/fixml is stale, regenerate it"
            );
        }
    }
}