quickfix-tokio 0.2.0

A pure-Rust FIX protocol engine built natively on tokio
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
//! FIX data dictionary: loads the standard QuickFIX XML specs
//! (`spec/FIX44.xml` etc.) and validates messages against them.

use std::collections::{HashMap, HashSet};

use quick_xml::events::Event;

use crate::error::{Error, RejectError, Result, SessionRejectReason};
use crate::field_map::GroupTemplate;
use crate::message::{Message, Tag};
use crate::tags;

/// Tags >= this are user-defined per the FIX spec.
pub const USER_DEFINED_TAG_MIN: Tag = 5000;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FieldType {
    Int,
    Length,
    SeqNum,
    NumInGroup,
    DayOfMonth,
    Float,
    Qty,
    Price,
    PriceOffset,
    Amt,
    Percentage,
    Char,
    Boolean,
    String,
    Data,
    UtcTimestamp,
    UtcDateOnly,
    UtcTimeOnly,
    LocalMktDate,
    MonthYear,
    Other,
}

impl FieldType {
    fn from_name(name: &str) -> Self {
        match name {
            "INT" => Self::Int,
            "LENGTH" => Self::Length,
            "SEQNUM" => Self::SeqNum,
            "NUMINGROUP" => Self::NumInGroup,
            "DAYOFMONTH" => Self::DayOfMonth,
            "FLOAT" => Self::Float,
            "QTY" | "QUANTITY" => Self::Qty,
            "PRICE" => Self::Price,
            "PRICEOFFSET" => Self::PriceOffset,
            "AMT" => Self::Amt,
            "PERCENTAGE" => Self::Percentage,
            "CHAR" => Self::Char,
            "BOOLEAN" => Self::Boolean,
            "STRING" | "MULTIPLEVALUESTRING" | "MULTIPLESTRINGVALUE" | "MULTIPLECHARVALUE"
            | "COUNTRY" | "CURRENCY" | "EXCHANGE" | "LANGUAGE" => Self::String,
            "DATA" | "XMLDATA" => Self::Data,
            "UTCTIMESTAMP" | "TIME" => Self::UtcTimestamp,
            "UTCDATEONLY" | "UTCDATE" | "DATE" => Self::UtcDateOnly,
            "UTCTIMEONLY" => Self::UtcTimeOnly,
            "LOCALMKTDATE" => Self::LocalMktDate,
            "MONTHYEAR" => Self::MonthYear,
            _ => Self::Other,
        }
    }
}

#[derive(Debug, Clone)]
pub struct FieldDef {
    pub tag: Tag,
    pub name: String,
    pub field_type: FieldType,
    /// Allowed values (enum fields); empty = unrestricted.
    pub values: HashSet<String>,
    /// Enum values with their descriptions, in spec order (for codegen).
    pub enum_values: Vec<(String, String)>,
}

#[derive(Debug, Clone, Default)]
pub struct GroupDef {
    pub counter: Tag,
    pub delimiter: Tag,
    /// All member tags, including nested groups' tags.
    pub tags: HashSet<Tag>,
    pub required: Vec<Tag>,
    pub groups: HashMap<Tag, GroupDef>,
    /// Direct members in spec order (nested groups appear as their counter).
    pub member_order: Vec<Tag>,
}

#[derive(Debug, Clone, Default)]
pub struct MessageDef {
    pub name: String,
    pub msg_type: String,
    pub tags: HashSet<Tag>,
    pub required: Vec<Tag>,
    pub groups: HashMap<Tag, GroupDef>,
    /// Top-level fields in spec order, components expanded in place
    /// (groups appear as their counter tag).
    pub field_order: Vec<Tag>,
}

#[derive(Debug, Clone, Default)]
pub struct DataDictionary {
    pub begin_string: String,
    pub fields_by_tag: HashMap<Tag, FieldDef>,
    pub tags_by_name: HashMap<String, Tag>,
    pub header_tags: HashSet<Tag>,
    pub header_required: Vec<Tag>,
    pub trailer_tags: HashSet<Tag>,
    pub trailer_required: Vec<Tag>,
    pub messages: HashMap<String, MessageDef>,
}

/// Validation toggles (classic QuickFIX setting names).
#[derive(Debug, Clone)]
pub struct ValidationSettings {
    pub check_fields_out_of_order: bool,
    pub check_fields_have_values: bool,
    pub check_user_defined_fields: bool,
    pub allow_unknown_message_fields: bool,
}

impl Default for ValidationSettings {
    fn default() -> Self {
        Self {
            check_fields_out_of_order: true,
            check_fields_have_values: true,
            check_user_defined_fields: true,
            allow_unknown_message_fields: false,
        }
    }
}

// ----- XML loading -----

/// Minimal DOM for the dictionary document.
struct Node {
    name: String,
    attrs: HashMap<String, String>,
    children: Vec<Node>,
}

fn parse_xml(text: &str) -> Result<Node> {
    let mut reader = quick_xml::Reader::from_str(text);
    reader.config_mut().trim_text(true);
    let mut stack: Vec<Node> = vec![Node {
        name: "(root)".into(),
        attrs: HashMap::new(),
        children: Vec::new(),
    }];

    let read_node = |e: &quick_xml::events::BytesStart<'_>| -> Result<Node> {
        let name = String::from_utf8_lossy(e.name().as_ref()).into_owned();
        let mut attrs = HashMap::new();
        for attr in e.attributes() {
            let attr = attr.map_err(|e| Error::Dictionary(format!("bad attribute: {e}")))?;
            attrs.insert(
                String::from_utf8_lossy(attr.key.as_ref()).into_owned(),
                String::from_utf8_lossy(&attr.value).into_owned(),
            );
        }
        Ok(Node { name, attrs, children: Vec::new() })
    };

    loop {
        match reader.read_event() {
            Ok(Event::Start(e)) => stack.push(read_node(&e)?),
            Ok(Event::Empty(e)) => {
                let node = read_node(&e)?;
                stack.last_mut().unwrap().children.push(node);
            }
            Ok(Event::End(_)) => {
                let node = stack.pop().unwrap();
                stack
                    .last_mut()
                    .ok_or_else(|| Error::Dictionary("unbalanced XML".into()))?
                    .children
                    .push(node);
            }
            Ok(Event::Eof) => break,
            Ok(_) => {}
            Err(e) => return Err(Error::Dictionary(format!("XML parse error: {e}"))),
        }
    }
    let mut root = stack.pop().ok_or_else(|| Error::Dictionary("empty document".into()))?;
    root.children
        .drain(..)
        .find(|n| n.name == "fix")
        .ok_or_else(|| Error::Dictionary("no <fix> root element".into()))
}

impl DataDictionary {
    pub fn parse(text: &str) -> Result<Self> {
        let fix = parse_xml(text)?;
        let major = fix.attrs.get("major").cloned().unwrap_or_default();
        let minor = fix.attrs.get("minor").cloned().unwrap_or_default();
        let fixt = fix.attrs.get("type").map(|t| t == "FIXT").unwrap_or(false);
        let mut dd = DataDictionary {
            begin_string: if fixt {
                format!("FIXT.{major}.{minor}")
            } else {
                format!("FIX.{major}.{minor}")
            },
            ..Default::default()
        };
        // Pre-FIX.4.2 dictionaries type most fields as CHAR meaning "string";
        // single-character CHAR semantics only exist from 4.2 on.
        let char_is_string = !fixt && dd.begin_string.as_str() < "FIX.4.2";

        // Pass 1: field definitions (name -> tag/type/enums).
        let fields = child(&fix, "fields");
        if let Some(fields) = fields {
            for f in fields.children.iter().filter(|c| c.name == "field") {
                let tag: Tag = f
                    .attrs
                    .get("number")
                    .and_then(|n| n.parse().ok())
                    .ok_or_else(|| Error::Dictionary("field without number".into()))?;
                let name = f.attrs.get("name").cloned().unwrap_or_default();
                let mut field_type =
                    FieldType::from_name(f.attrs.get("type").map(|s| s.as_str()).unwrap_or(""));
                if char_is_string && field_type == FieldType::Char {
                    field_type = FieldType::String;
                }
                let enum_values: Vec<(String, String)> = f
                    .children
                    .iter()
                    .filter(|c| c.name == "value")
                    .filter_map(|c| {
                        c.attrs.get("enum").map(|v| {
                            (v.clone(), c.attrs.get("description").cloned().unwrap_or_default())
                        })
                    })
                    .collect();
                let values = enum_values.iter().map(|(v, _)| v.clone()).collect();
                dd.tags_by_name.insert(name.clone(), tag);
                dd.fields_by_tag
                    .insert(tag, FieldDef { tag, name, field_type, values, enum_values });
            }
        }

        // Component definitions, unexpanded.
        let mut components: HashMap<&str, &Node> = HashMap::new();
        if let Some(comps) = child(&fix, "components") {
            for c in comps.children.iter().filter(|c| c.name == "component") {
                if let Some(name) = c.attrs.get("name") {
                    components.insert(name, c);
                }
            }
        }

        // Header / trailer.
        if let Some(header) = child(&fix, "header") {
            let mut def = MessageDef::default();
            dd.collect_message(header, &components, &mut def)?;
            dd.header_tags = def.tags;
            dd.header_required = def.required;
        }
        if let Some(trailer) = child(&fix, "trailer") {
            let mut def = MessageDef::default();
            dd.collect_message(trailer, &components, &mut def)?;
            dd.trailer_tags = def.tags;
            dd.trailer_required = def.required;
        }

        // Messages.
        if let Some(messages) = child(&fix, "messages") {
            for m in messages.children.iter().filter(|c| c.name == "message") {
                let mut def = MessageDef {
                    name: m.attrs.get("name").cloned().unwrap_or_default(),
                    msg_type: m
                        .attrs
                        .get("msgtype")
                        .cloned()
                        .ok_or_else(|| Error::Dictionary("message without msgtype".into()))?,
                    ..Default::default()
                };
                dd.collect_message(m, &components, &mut def)?;
                dd.messages.insert(def.msg_type.clone(), def);
            }
        }
        Ok(dd)
    }

    pub async fn load(path: impl AsRef<std::path::Path>) -> Result<Self> {
        let text = tokio::fs::read_to_string(path).await?;
        Self::parse(&text)
    }

    /// Recursively collect fields/components/groups of a message-like node.
    fn collect_message(
        &self,
        node: &Node,
        components: &HashMap<&str, &Node>,
        def: &mut MessageDef,
    ) -> Result<()> {
        // Split borrows for the recursive walk.
        let MessageDef { tags, required, groups, field_order, .. } = def;
        self.collect(node, components, tags, required, groups, field_order)
    }

    fn collect(
        &self,
        node: &Node,
        components: &HashMap<&str, &Node>,
        tags_out: &mut HashSet<Tag>,
        required_out: &mut Vec<Tag>,
        groups_out: &mut HashMap<Tag, GroupDef>,
        order_out: &mut Vec<Tag>,
    ) -> Result<()> {
        for c in &node.children {
            let required = c.attrs.get("required").map(|r| r == "Y").unwrap_or(false);
            match c.name.as_str() {
                "field" => {
                    let name = c.attrs.get("name").cloned().unwrap_or_default();
                    let tag = *self
                        .tags_by_name
                        .get(&name)
                        .ok_or_else(|| Error::Dictionary(format!("unknown field {name}")))?;
                    if tags_out.insert(tag) {
                        order_out.push(tag);
                    }
                    if required {
                        required_out.push(tag);
                    }
                }
                "component" => {
                    let name = c.attrs.get("name").cloned().unwrap_or_default();
                    let def = components
                        .get(name.as_str())
                        .ok_or_else(|| Error::Dictionary(format!("unknown component {name}")))?;
                    // Component fields are required only if the component is.
                    let mut comp_required = Vec::new();
                    self.collect(def, components, tags_out, &mut comp_required, groups_out, order_out)?;
                    if required {
                        required_out.extend(comp_required);
                    }
                }
                "group" => {
                    let name = c.attrs.get("name").cloned().unwrap_or_default();
                    let counter = *self
                        .tags_by_name
                        .get(&name)
                        .ok_or_else(|| Error::Dictionary(format!("unknown group {name}")))?;
                    if tags_out.insert(counter) {
                        order_out.push(counter);
                    }
                    if required {
                        required_out.push(counter);
                    }
                    let mut g = GroupDef { counter, ..Default::default() };
                    let mut member_order: Vec<Tag> = Vec::new();
                    self.collect_group(c, components, &mut g, &mut member_order)?;
                    g.delimiter = *member_order
                        .first()
                        .ok_or_else(|| Error::Dictionary(format!("empty group {name}")))?;
                    g.member_order = member_order;
                    // Group member tags also count as "in message" for
                    // tag-allowed checks.
                    tags_out.extend(g.tags.iter().copied());
                    groups_out.insert(counter, g);
                }
                _ => {}
            }
        }
        Ok(())
    }

    fn collect_group(
        &self,
        node: &Node,
        components: &HashMap<&str, &Node>,
        group: &mut GroupDef,
        member_order: &mut Vec<Tag>,
    ) -> Result<()> {
        for c in &node.children {
            let required = c.attrs.get("required").map(|r| r == "Y").unwrap_or(false);
            match c.name.as_str() {
                "field" => {
                    let name = c.attrs.get("name").cloned().unwrap_or_default();
                    let tag = *self
                        .tags_by_name
                        .get(&name)
                        .ok_or_else(|| Error::Dictionary(format!("unknown field {name}")))?;
                    group.tags.insert(tag);
                    member_order.push(tag);
                    if required {
                        group.required.push(tag);
                    }
                }
                "component" => {
                    let name = c.attrs.get("name").cloned().unwrap_or_default();
                    let def = components
                        .get(name.as_str())
                        .ok_or_else(|| Error::Dictionary(format!("unknown component {name}")))?;
                    self.collect_group(def, components, group, member_order)?;
                }
                "group" => {
                    let name = c.attrs.get("name").cloned().unwrap_or_default();
                    let counter = *self
                        .tags_by_name
                        .get(&name)
                        .ok_or_else(|| Error::Dictionary(format!("unknown group {name}")))?;
                    group.tags.insert(counter);
                    member_order.push(counter);
                    if required {
                        group.required.push(counter);
                    }
                    let mut nested = GroupDef { counter, ..Default::default() };
                    let mut nested_order = Vec::new();
                    self.collect_group(c, components, &mut nested, &mut nested_order)?;
                    nested.delimiter = *nested_order
                        .first()
                        .ok_or_else(|| Error::Dictionary(format!("empty group {name}")))?;
                    nested.member_order = nested_order;
                    group.tags.extend(nested.tags.iter().copied());
                    group.groups.insert(counter, nested);
                }
                _ => {}
            }
        }
        Ok(())
    }

    /// Combine a FIXT transport dictionary (header/trailer/admin messages)
    /// with an application dictionary (app messages and fields) into one
    /// effective dictionary for validation.
    pub fn merged_with_app(mut self, app: &DataDictionary) -> DataDictionary {
        for (tag, field) in &app.fields_by_tag {
            self.fields_by_tag.entry(*tag).or_insert_with(|| field.clone());
        }
        for (name, tag) in &app.tags_by_name {
            self.tags_by_name.entry(name.clone()).or_insert(*tag);
        }
        for (msg_type, def) in &app.messages {
            self.messages.entry(msg_type.clone()).or_insert_with(|| def.clone());
        }
        self
    }

    /// Reorder a message body into the canonical form the reference engines
    /// produce: top-level fields ascending by tag, with each repeating-group
    /// block kept intact (in received/insertion order) under its counter tag.
    pub fn canonicalize_body(&self, msg: &mut Message) {
        let Ok(mt) = msg.msg_type() else { return };
        let Some(def) = self.messages.get(&mt) else { return };
        let fields = msg.body.take_fields();

        let mut segments: Vec<(Tag, Vec<crate::field_map::TagValue>)> = Vec::new();
        let mut i = 0;
        while i < fields.len() {
            let tag = fields[i].tag;
            let mut seg = vec![fields[i].clone()];
            i += 1;
            if let Some(group) = def.groups.get(&tag) {
                while i < fields.len() && group.tags.contains(&fields[i].tag) {
                    seg.push(fields[i].clone());
                    i += 1;
                }
            }
            segments.push((tag, seg));
        }
        segments.sort_by_key(|s| s.0);
        msg.body.set_fields(segments.into_iter().flat_map(|(_, seg)| seg).collect());
    }

    /// A ready-made [`GroupTemplate`] for reading a repeating group of this
    /// message type (top-level groups only).
    pub fn group_template(&self, msg_type: &str, counter: Tag) -> Option<GroupTemplate> {
        let g = self.messages.get(msg_type)?.groups.get(&counter)?;
        let mut members = vec![g.delimiter];
        members.extend(g.tags.iter().copied().filter(|&t| t != g.delimiter));
        Some(GroupTemplate::new(counter, members))
    }

    // ----- validation -----

    /// Validate a parsed message. Returns the reject that should be sent
    /// when it fails.
    pub fn validate(
        &self,
        msg: &Message,
        settings: &ValidationSettings,
    ) -> std::result::Result<(), RejectError> {
        let msg_type = msg
            .header
            .get_string(tags::MSG_TYPE)
            .map_err(|_| RejectError::with_tag(SessionRejectReason::RequiredTagMissing, tags::MSG_TYPE))?;
        // XMLnonFIX (35=n) is accepted without a message definition,
        // matching QuickFIX/n.
        if msg_type == "n" {
            return Ok(());
        }
        let def = self.messages.get(&msg_type).ok_or_else(|| {
            // Only FIX.4.2 cites RefTagID=35 on an invalid MsgType.
            if self.begin_string == "FIX.4.2" {
                RejectError::with_tag(SessionRejectReason::InvalidMsgType, tags::MSG_TYPE)
            } else {
                RejectError::new(SessionRejectReason::InvalidMsgType)
            }
        })?;

        if settings.check_fields_out_of_order {
            if let Some(tag) = msg.structure_error() {
                return Err(RejectError::with_tag(
                    SessionRejectReason::TagSpecifiedOutOfRequiredOrder,
                    tag,
                ));
            }
        }

        // A repeating group's first entry must begin with its delimiter.
        // Groups are checked in wire order so the first violation wins.
        let body_fields: Vec<_> = msg.body.iter().collect();
        for (i, f) in body_fields.iter().enumerate() {
            let Some(group) = def.groups.get(&f.tag) else { continue };
            let declared: u64 = msg.body.get_opt(group.counter).ok().flatten().unwrap_or(0);
            if declared == 0 {
                continue;
            }
            if let Some(first) = body_fields.get(i + 1) {
                if group.tags.contains(&first.tag) && first.tag != group.delimiter {
                    return Err(RejectError::other(
                        format!(
                            "Group {}'s first entry does not start with delimiter {}",
                            group.counter, group.delimiter
                        ),
                        group.counter,
                    ));
                }
            }
        }

        // Required fields.
        for &tag in &self.header_required {
            if !msg.header.contains(tag) {
                return Err(RejectError::with_tag(SessionRejectReason::RequiredTagMissing, tag));
            }
        }
        for &tag in &self.trailer_required {
            if !msg.trailer.contains(tag) {
                return Err(RejectError::with_tag(SessionRejectReason::RequiredTagMissing, tag));
            }
        }
        for &tag in &def.required {
            if !msg.body.contains(tag) {
                return Err(RejectError::with_tag(SessionRejectReason::RequiredTagMissing, tag));
            }
        }

        // Duplicate tags: only repeating-group members may repeat.
        let mut group_member_tags: HashSet<Tag> = HashSet::new();
        for g in def.groups.values() {
            group_member_tags.extend(g.tags.iter().copied());
        }
        let mut seen: HashSet<Tag> = HashSet::new();
        for f in msg.body.iter() {
            if !group_member_tags.contains(&f.tag) && !seen.insert(f.tag) {
                return Err(RejectError::with_tag(
                    SessionRejectReason::TagAppearsMoreThanOnce,
                    f.tag,
                ));
            }
        }

        // Group counts (declared NumInGroup vs actual delimiter count) —
        // checked before per-field content so a count mismatch wins over
        // value errors inside the group.
        for group in def.groups.values() {
            if let Some(raw) = msg.body.get_raw(group.counter) {
                let declared: usize = std::str::from_utf8(raw)
                    .ok()
                    .and_then(|s| s.parse().ok())
                    .ok_or_else(|| {
                        RejectError::with_tag(
                            SessionRejectReason::IncorrectDataFormatForValue,
                            group.counter,
                        )
                    })?;
                let actual =
                    msg.body.iter().filter(|f| f.tag == group.delimiter).count();
                if declared != actual {
                    return Err(RejectError::with_tag(
                        SessionRejectReason::IncorrectNumInGroupCountForRepeatingGroup,
                        group.counter,
                    ));
                }
            }
        }

        // Per-field checks.
        for section in [&msg.header, &msg.body, &msg.trailer] {
            let is_body = std::ptr::eq(section, &msg.body);
            for f in section.iter() {
                self.check_field(f.tag, &f.value, is_body, def, settings)?;
            }
        }
        Ok(())
    }

    fn check_field(
        &self,
        tag: Tag,
        value: &[u8],
        is_body: bool,
        def: &MessageDef,
        settings: &ValidationSettings,
    ) -> std::result::Result<(), RejectError> {
        if tag >= USER_DEFINED_TAG_MIN && !settings.check_user_defined_fields {
            return Ok(());
        }
        if settings.check_fields_have_values && value.is_empty() {
            return Err(RejectError::with_tag(
                SessionRejectReason::TagSpecifiedWithoutAValue,
                tag,
            ));
        }
        let Some(field) = self.fields_by_tag.get(&tag) else {
            // User-defined tags were already skipped above when
            // ValidateUserDefinedFields=N; here an unknown tag is an error
            // unless unknown fields are allowed outright.
            if settings.allow_unknown_message_fields {
                return Ok(());
            }
            return Err(RejectError::with_tag(SessionRejectReason::InvalidTagNumber, tag));
        };
        if is_body
            && !settings.allow_unknown_message_fields
            && !def.tags.contains(&tag)
            && tag < USER_DEFINED_TAG_MIN
        {
            return Err(RejectError::with_tag(
                SessionRejectReason::TagNotDefinedForThisMessageType,
                tag,
            ));
        }
        // Format before enum membership: a malformed value is "incorrect
        // data format" (373=6), not "value out of range" (373=5).
        self.check_format(field, value).map_err(|_| {
            RejectError::with_tag(SessionRejectReason::IncorrectDataFormatForValue, tag)
        })?;
        if !field.values.is_empty() {
            let v = String::from_utf8_lossy(value);
            // MultipleValue fields carry space-separated entries.
            let ok = v.split(' ').all(|part| field.values.contains(part));
            if !ok {
                return Err(RejectError::with_tag(SessionRejectReason::ValueIsIncorrect, tag));
            }
        }
        Ok(())
    }

    fn check_format(&self, field: &FieldDef, value: &[u8]) -> std::result::Result<(), ()> {
        use crate::value::{FixDate, FixDecode, UtcTimestamp};
        let ok = match field.field_type {
            FieldType::Int => i64::decode(field.tag, value).is_ok(),
            FieldType::Length | FieldType::SeqNum | FieldType::NumInGroup => {
                u64::decode(field.tag, value).is_ok()
            }
            FieldType::DayOfMonth => {
                u64::decode(field.tag, value).map(|d| (1..=31).contains(&d)).unwrap_or(false)
            }
            FieldType::Float
            | FieldType::Qty
            | FieldType::Price
            | FieldType::PriceOffset
            | FieldType::Amt
            | FieldType::Percentage => f64::decode(field.tag, value).is_ok(),
            FieldType::Char => value.len() == 1,
            FieldType::Boolean => matches!(value, b"Y" | b"N"),
            FieldType::UtcTimestamp => UtcTimestamp::decode(field.tag, value).is_ok(),
            FieldType::UtcDateOnly | FieldType::LocalMktDate => {
                FixDate::decode(field.tag, value).is_ok()
            }
            FieldType::MonthYear => {
                value.len() >= 6 && value[..6].iter().all(|b| b.is_ascii_digit())
            }
            FieldType::UtcTimeOnly | FieldType::String | FieldType::Data | FieldType::Other => {
                true
            }
        };
        if ok { Ok(()) } else { Err(()) }
    }
}

fn child<'a>(node: &'a Node, name: &str) -> Option<&'a Node> {
    node.children.iter().find(|c| c.name == name)
}

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

    fn fix44() -> DataDictionary {
        let text = std::fs::read_to_string(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/spec/FIX44.xml"
        ))
        .unwrap();
        DataDictionary::parse(&text).unwrap()
    }

    fn valid_order() -> Message {
        let mut m = Message::with_type("D");
        m.header.set(tags::BEGIN_STRING, "FIX.4.4");
        m.header.set(tags::SENDER_COMP_ID, "A");
        m.header.set(tags::TARGET_COMP_ID, "B");
        m.header.set(tags::MSG_SEQ_NUM, 2u64);
        m.stamp_sending_time(crate::value::UtcTimestamp::now());
        m.set(11, "ORDER-1");
        m.set(55, "TSLA");
        m.set(54, '1');
        m.set(60, crate::value::UtcTimestamp::now());
        m.set(40, '1');
        m
    }

    #[test]
    fn loads_fix44_spec() {
        let dd = fix44();
        assert_eq!(dd.begin_string, "FIX.4.4");
        assert!(dd.messages.contains_key("D"), "NewOrderSingle should exist");
        assert!(dd.header_tags.contains(&tags::MSG_SEQ_NUM));
        assert_eq!(dd.fields_by_tag[&54].field_type, FieldType::Char);
        assert!(dd.fields_by_tag[&54].values.contains("1"));
        // NewOrderSingle has the NoPartyIDs group via the Parties component.
        let d = &dd.messages["D"];
        assert!(d.groups.contains_key(&453), "NoPartyIDs group expected");
        assert_eq!(d.groups[&453].delimiter, 448);
    }

    #[test]
    fn validates_good_message() {
        let dd = fix44();
        let msg =
            Message::parse(&valid_order().to_bytes(), true).expect("roundtrip");
        dd.validate(&msg, &ValidationSettings::default()).expect("should validate");
    }

    #[test]
    fn missing_required_field_rejected() {
        let dd = fix44();
        let mut order = valid_order();
        order.body.remove(11); // ClOrdID is required='Y' in NewOrderSingle
        let msg = Message::parse(&order.to_bytes(), true).unwrap();
        let err = dd.validate(&msg, &ValidationSettings::default()).unwrap_err();
        assert_eq!(err.reason, SessionRejectReason::RequiredTagMissing);
        assert_eq!(err.ref_tag, Some(11));
    }

    #[test]
    fn bad_enum_value_rejected() {
        let dd = fix44();
        let mut order = valid_order();
        order.set(54, 'Z'); // not a valid Side
        let msg = Message::parse(&order.to_bytes(), true).unwrap();
        let err = dd.validate(&msg, &ValidationSettings::default()).unwrap_err();
        assert_eq!(err.reason, SessionRejectReason::ValueIsIncorrect);
        assert_eq!(err.ref_tag, Some(54));
    }

    #[test]
    fn undefined_tag_rejected() {
        let dd = fix44();
        let mut order = valid_order();
        order.set(4999, "bogus"); // not defined in FIX44
        let msg = Message::parse(&order.to_bytes(), true).unwrap();
        let err = dd.validate(&msg, &ValidationSettings::default()).unwrap_err();
        assert_eq!(err.reason, SessionRejectReason::InvalidTagNumber);
    }

    #[test]
    fn tag_not_defined_for_message_type() {
        let dd = fix44();
        let mut order = valid_order();
        order.set(112, "TR1"); // TestReqID doesn't belong in NewOrderSingle
        let msg = Message::parse(&order.to_bytes(), true).unwrap();
        let err = dd.validate(&msg, &ValidationSettings::default()).unwrap_err();
        assert_eq!(err.reason, SessionRejectReason::TagNotDefinedForThisMessageType);
        assert_eq!(err.ref_tag, Some(112));
    }

    #[test]
    fn wrong_group_count_rejected() {
        let dd = fix44();
        let mut order = valid_order();
        order.set(453, 2u32); // declare two parties...
        order.body.push(448, "PARTY-A"); // ...but provide one
        order.body.push(447, 'D');
        order.body.push(452, 1u32);
        let msg = Message::parse(&order.to_bytes(), true).unwrap();
        let err = dd.validate(&msg, &ValidationSettings::default()).unwrap_err();
        assert_eq!(
            err.reason,
            SessionRejectReason::IncorrectNumInGroupCountForRepeatingGroup
        );
    }

    #[test]
    fn unknown_msg_type_rejected() {
        let dd = fix44();
        let mut m = valid_order();
        m.header.set(tags::MSG_TYPE, "ZZ");
        let msg = Message::parse(&m.to_bytes(), true).unwrap();
        let err = dd.validate(&msg, &ValidationSettings::default()).unwrap_err();
        assert_eq!(err.reason, SessionRejectReason::InvalidMsgType);
    }

    #[test]
    fn group_template_reads_parties() {
        let dd = fix44();
        let tpl = dd.group_template("D", 453).unwrap();
        assert_eq!(tpl.num_tag, 453);
        assert_eq!(tpl.delimiter(), 448);

        let mut order = valid_order();
        let mut g1 = crate::field_map::FieldMap::new();
        g1.push(448, "PARTY-A");
        g1.push(447, 'D');
        let mut g2 = crate::field_map::FieldMap::new();
        g2.push(448, "PARTY-B");
        g2.push(447, 'D');
        order.body.write_groups(&tpl, &[g1, g2]);

        let msg = Message::parse(&order.to_bytes(), true).unwrap();
        dd.validate(&msg, &ValidationSettings::default()).expect("groups valid");
        let groups = msg.body.read_groups(&tpl).unwrap();
        assert_eq!(groups.len(), 2);
        assert_eq!(groups[1].get_string(448).unwrap(), "PARTY-B");
    }
}