refix-dictionary 0.7.0

FIX protocol dictionary model and frontends for the ReFIX engine
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
use super::{
    ComponentIndex, Dictionary, Error, FieldIndex, Resolved, ResolvedComponent, ResolvedGroup,
    ResolvedMember, ResolvedMessage, Warning,
};
use crate::spec::{self, DataType, Field, Spec};
use crate::{MemberContext, Tag};
use std::collections::{HashMap, hash_map::Entry};

impl Spec {
    /// Resolves this spec into a [`Dictionary`], checking its integrity.
    ///
    /// A message or group instance and the components it includes may list
    /// the same field. A tag that one list names twice is dropped with a
    /// [`Warning`], as is one that a message names after the header or
    /// trailer. A tag listed again in any other way is an error.
    pub fn resolve(self) -> Result<Resolved, Error> {
        let mut resolver = Resolver::new(&self)?;
        resolver.expand_components()?;
        let mut header = resolver.resolve_members(&self.header, &MemberContext::Header, false)?;
        let mut trailer =
            resolver.resolve_members(&self.trailer, &MemberContext::Trailer, false)?;
        let mut fixed = Listings::default();
        resolver.drop_repeated_tags(&mut header, &Scope::of(&MemberContext::Header), &mut fixed)?;
        resolver.drop_repeated_tags(
            &mut trailer,
            &Scope::of(&MemberContext::Trailer),
            &mut fixed,
        )?;
        let messages = self
            .messages
            .iter()
            .map(|message| resolver.resolve_message(message, &fixed))
            .collect::<Result<_, _>>()?;
        let warnings = resolver.warnings;

        Ok(Resolved {
            dictionary: Dictionary {
                spec: self,
                header,
                messages,
                trailer,
            },
            warnings,
        })
    }
}

/// State shared while resolving one spec.
///
/// It holds the lookup tables, the memoised component expansions, the
/// components currently being expanded and the warnings so far.
struct Resolver<'a> {
    spec: &'a Spec,
    fields_by_tag: HashMap<Tag, FieldIndex>,
    components_by_name: HashMap<&'a str, ComponentIndex>,
    expansions: Vec<Option<Vec<ResolvedMember>>>,
    stack: Vec<ComponentIndex>,
    warnings: Vec<Warning>,
}

impl<'a> Resolver<'a> {
    fn new(spec: &'a Spec) -> Result<Self, Error> {
        check_int_values(&spec.fields)?;
        check_unique_codes(&spec.fields)?;
        check_unique_msg_types(&spec.messages)?;
        let fields_by_tag = index_fields(&spec.fields)?;
        check_length_tags(&spec.fields, &fields_by_tag)?;
        Ok(Self {
            spec,
            fields_by_tag,
            components_by_name: index_components(&spec.components)?,
            expansions: vec![None; spec.components.len()],
            stack: Vec::new(),
            warnings: Vec::new(),
        })
    }

    /// Resolves every component definition into its member list, with
    /// requiredness relative to the component itself.
    fn expand_components(&mut self) -> Result<(), Error> {
        for index in (0..self.spec.components.len()).map(ComponentIndex) {
            self.expand_component(index)?;
        }
        Ok(())
    }

    fn expand_component(&mut self, index: ComponentIndex) -> Result<(), Error> {
        if self.expansions[index.0].is_some() {
            return Ok(());
        }
        let spec = self.spec;
        let component = &spec.components.as_slice()[index];
        if self.stack.contains(&index) {
            return Err(Error::CircularComponent {
                component: component.name.clone(),
            });
        }

        let context = MemberContext::Component(component.name.clone());
        self.stack.push(index);
        let mut members = self.resolve_members(&component.members, &context, false)?;
        self.stack.pop();

        if let [spec::Member::Group(_)] = component.members.as_slice()
            && let [ResolvedMember::Group(group)] = members.as_mut_slice()
        {
            group.component = Some(component.name.clone());
        }

        self.expansions[index.0] = Some(members);

        Ok(())
    }

    /// Resolves a message's members, dropping the tags of the header and
    /// trailer, recorded in `fixed`.
    fn resolve_message(
        &mut self,
        message: &spec::Message,
        fixed: &Listings,
    ) -> Result<ResolvedMessage, Error> {
        let context = MemberContext::Message(message.name.clone());
        let mut members = self.resolve_members(&message.members, &context, false)?;
        self.drop_repeated_tags(&mut members, &Scope::of(&context), &mut fixed.clone())?;
        Ok(ResolvedMessage { members })
    }

    /// Resolves one member list and drops every tag it may not list again.
    ///
    /// A component that carries fields stays one member. The groups of a
    /// component made up of groups alone are spliced in.
    ///
    /// In a group instance a component in first position becomes required,
    /// so that the field every instance starts with is present.
    fn resolve_members(
        &mut self,
        members: &[spec::Member],
        context: &MemberContext,
        is_instance: bool,
    ) -> Result<Vec<ResolvedMember>, Error> {
        let mut resolved = Vec::new();
        for (position, member) in members.iter().enumerate() {
            match member {
                spec::Member::Field(field_ref) => {
                    resolved.push(self.resolve_field_ref(field_ref, context)?);
                }
                spec::Member::Component(component_ref) => {
                    let Some(&child) = self.components_by_name.get(component_ref.name.as_str())
                    else {
                        return Err(Error::UnknownComponent {
                            context: context.clone(),
                            component: component_ref.name.clone(),
                        });
                    };
                    self.expand_component(child)?;
                    let expansion = self.expansions[child.0]
                        .as_ref()
                        .expect("the call above expands the child");
                    let is_required = component_ref.is_required || (is_instance && position == 0);
                    if expansion.iter().any(ResolvedMember::carries_fields) {
                        resolved.push(ResolvedMember::Component(ResolvedComponent {
                            name: component_ref.name.clone(),
                            is_required,
                            members: expansion.clone(),
                        }));
                    } else {
                        extend_with_expansion(&mut resolved, expansion, is_required);
                    }
                }
                spec::Member::Group(group) => {
                    resolved.push(ResolvedMember::Group(self.resolve_group(group, context)?));
                }
            }
        }

        self.drop_repeated_tags(&mut resolved, &Scope::of(context), &mut Listings::default())?;
        Ok(resolved)
    }

    /// Drops every member whose tag its list has named already, in
    /// document order, with a warning for each.
    ///
    /// A component shares the scope of whatever includes it. A field stays
    /// when its earlier listings are in the same scope and came through
    /// other components. A tag listed again in any other way is an error.
    fn drop_repeated_tags(
        &mut self,
        members: &mut Vec<ResolvedMember>,
        scope: &Scope,
        listings: &mut Listings,
    ) -> Result<(), Error> {
        let spec = self.spec;
        let mut kept = Vec::with_capacity(members.len());
        for mut member in members.drain(..) {
            if let ResolvedMember::Component(component) = &mut member {
                let scope = scope.through(&component.name);
                self.drop_repeated_tags(&mut component.members, &scope, listings)?;
                if !component.members.is_empty() {
                    kept.push(member);
                }
                continue;
            }
            let tag = spec.fields.as_slice()[member.first_field()].tag;
            match listings.entry(tag) {
                Entry::Occupied(mut first) => {
                    let first = first.get_mut();
                    if first.is_repeated_by(&member, scope) {
                        self.warnings.push(Warning::DuplicateField {
                            tag,
                            kept: first.context().clone(),
                            dropped: scope.context.clone(),
                        });
                    } else if first.may_be_shared_with(&member, scope) {
                        first.scopes.push(scope.clone());
                        kept.push(member);
                    } else {
                        return Err(Error::AmbiguousTag {
                            tag,
                            first: first.context().clone(),
                            second: scope.context.clone(),
                        });
                    }
                }
                Entry::Vacant(slot) => {
                    slot.insert(Listing::of(&member, scope));
                    if let ResolvedMember::Group(group) = &mut member {
                        let count_field = &spec.fields.as_slice()[group.count_field_index];
                        let instance = scope.context.group(&count_field.name);
                        let scope = Scope::of(&instance);
                        self.drop_repeated_tags(&mut group.members, &scope, listings)?;
                    }
                    kept.push(member);
                }
            }
        }
        *members = kept;
        Ok(())
    }

    /// Resolves a group's count field in the enclosing scope, and its
    /// instance members with requiredness relative to the instance.
    fn resolve_group(
        &mut self,
        group: &spec::Group,
        context: &MemberContext,
    ) -> Result<ResolvedGroup, Error> {
        let count_field_index = self.field_index(group.count_tag, context)?;
        let spec = self.spec;
        let instance = context.group(&spec.fields.as_slice()[count_field_index].name);
        let mut members = self.resolve_members(&group.members, &instance, true)?;
        let delimiter_index = delimit(&mut members, &instance)?;

        Ok(ResolvedGroup {
            count_field_index,
            delimiter_index,
            is_required: group.is_required,
            declared_in: context.clone(),
            component: None,
            members,
        })
    }

    fn resolve_field_ref(
        &self,
        field_ref: &spec::FieldRef,
        context: &MemberContext,
    ) -> Result<ResolvedMember, Error> {
        let field_index = self.field_index(field_ref.tag, context)?;
        let field = &self.spec.fields.as_slice()[field_index];
        if field.data_type.has_length_field() && field.length_tag.is_none() {
            return Err(Error::MissingLengthTag {
                field: field.name.clone(),
                context: context.clone(),
            });
        }
        Ok(ResolvedMember::Field {
            field_index,
            is_required: field_ref.is_required,
        })
    }

    /// The definition `tag` refers to, where it is used in `context`.
    fn field_index(&self, tag: Tag, context: &MemberContext) -> Result<FieldIndex, Error> {
        self.fields_by_tag
            .get(&tag)
            .copied()
            .ok_or_else(|| Error::UnknownField {
                context: context.clone(),
                tag,
            })
    }
}

/// The first listing of each tag in a message, at any depth.
type Listings = HashMap<Tag, Listing>;

/// Where a message, section or group instance lists its members.
#[derive(Clone, Eq, PartialEq)]
struct Scope {
    context: MemberContext,
    /// The components the members are listed through, outermost first.
    path: Vec<String>,
}

impl Scope {
    /// The scope's own member list.
    fn of(context: &MemberContext) -> Self {
        Self {
            context: context.clone(),
            path: Vec::new(),
        }
    }

    /// The member list of `component`, included in this one.
    fn through(&self, component: &str) -> Self {
        let mut path = self.path.clone();
        path.push(component.to_owned());
        Self {
            context: self.context.clone(),
            path,
        }
    }
}

/// The lists that name a tag, the first one first.
#[derive(Clone)]
struct Listing {
    scopes: Vec<Scope>,
    /// Whether the tag counts a group.
    counts_group: bool,
}

impl Listing {
    fn of(member: &ResolvedMember, scope: &Scope) -> Self {
        Self {
            scopes: vec![scope.clone()],
            counts_group: !member.carries_fields(),
        }
    }

    /// Where the tag is listed first.
    fn context(&self) -> &MemberContext {
        &self.scopes[0].context
    }

    /// Whether `member` is a listing its list may not make.
    ///
    /// A list may not name a tag twice. The own list of a message or the
    /// trailer may not name a tag of a section before it.
    fn is_repeated_by(&self, member: &ResolvedMember, scope: &Scope) -> bool {
        let follows_a_section = scope.path.is_empty() && *self.context() != scope.context;
        self.reads_like(member, scope) && (self.scopes.contains(scope) || follows_a_section)
    }

    /// Whether `scope` may list the field as well.
    ///
    /// It may when it is the same scope and reaches the field through
    /// components that have not listed it yet.
    fn may_be_shared_with(&self, member: &ResolvedMember, scope: &Scope) -> bool {
        self.reads_like(member, scope) && !self.counts_group && !self.scopes.contains(scope)
    }

    /// Whether the wire reads `member` in `scope` as it reads this listing.
    ///
    /// Both are fields or both are groups, in one message or group
    /// instance. The header, a message and the trailer are one.
    fn reads_like(&self, member: &ResolvedMember, scope: &Scope) -> bool {
        let is_instance = |context: &MemberContext| matches!(context, MemberContext::Group { .. });
        let is_same_instance = *self.context() == scope.context
            || !(is_instance(self.context()) || is_instance(&scope.context));
        let counts_group = !member.carries_fields();
        is_same_instance && self.counts_group == counts_group
    }
}

impl ResolvedMember {
    /// The field this member starts with on the wire, or a group's count field.
    fn first_field(&self) -> FieldIndex {
        match self {
            ResolvedMember::Field { field_index, .. } => *field_index,
            ResolvedMember::Group(group) => group.count_field_index,
            ResolvedMember::Component(component) => component.members[0].first_field(),
        }
    }

    /// Whether this is a field or a component that carries fields.
    fn carries_fields(&self) -> bool {
        !matches!(self, ResolvedMember::Group(_))
    }

    /// Makes this member required in its scope.
    fn require(&mut self) {
        match self {
            ResolvedMember::Field { is_required, .. } => *is_required = true,
            ResolvedMember::Group(group) => group.is_required = true,
            ResolvedMember::Component(component) => {
                component.is_required = true;
                component.members[0].require();
            }
        }
    }

    /// Keeps this member required only if `is_required` holds as well.
    fn combine_required(&mut self, is_required: bool) {
        match self {
            ResolvedMember::Field {
                is_required: own, ..
            } => *own &= is_required,
            ResolvedMember::Group(group) => group.is_required &= is_required,
            ResolvedMember::Component(component) => component.is_required &= is_required,
        }
    }
}

fn index_fields(fields: &[Field]) -> Result<HashMap<Tag, FieldIndex>, Error> {
    let mut by_tag = HashMap::new();
    for (index, field) in fields.iter().enumerate() {
        if by_tag.insert(field.tag, FieldIndex(index)).is_some() {
            return Err(Error::DuplicateTag { tag: field.tag });
        }
    }
    Ok(by_tag)
}

/// Checks that every value listed for an int-based field is a FIX integer.
fn check_int_values(fields: &[Field]) -> Result<(), Error> {
    let int_fields = fields.iter().filter(|field| field.data_type.is_int_based());
    for field in int_fields {
        if let Some(value) = field.values.iter().find(|value| !is_integer(&value.value)) {
            return Err(Error::NonIntegerValue {
                field: field.name.clone(),
                value: value.value.clone(),
            });
        }
    }
    Ok(())
}

/// Checks that no field lists two values under one code.
fn check_unique_codes(fields: &[Field]) -> Result<(), Error> {
    for field in fields {
        let mut codes = HashMap::new();
        for value in &field.values {
            let code = field.data_type.canonical_code(&value.value);
            if let Some(first) = codes.insert(code, &value.value) {
                return Err(Error::RepeatedCode {
                    field: field.name.clone(),
                    first: first.clone(),
                    second: value.value.clone(),
                });
            }
        }
    }
    Ok(())
}

/// Checks that no two messages declare the same MsgType.
fn check_unique_msg_types(messages: &[spec::Message]) -> Result<(), Error> {
    let mut names = HashMap::new();
    for message in messages {
        if let Some(first) = names.insert(message.msg_type.as_str(), &message.name) {
            return Err(Error::DuplicateMsgType {
                msg_type: message.msg_type.clone(),
                first: first.clone(),
                second: message.name.clone(),
            });
        }
    }
    Ok(())
}

/// Checks that only data fields take a length, each from a LENGTH field of
/// its own.
fn check_length_tags(
    fields: &[Field],
    fields_by_tag: &HashMap<Tag, FieldIndex>,
) -> Result<(), Error> {
    let mut by_length_tag = HashMap::new();
    for field in fields {
        let Some(tag) = field.length_tag else {
            continue;
        };
        if !field.data_type.has_length_field() {
            return Err(Error::UnexpectedLengthTag {
                field: field.name.clone(),
                tag,
            });
        }
        let Some(&index) = fields_by_tag.get(&tag) else {
            return Err(Error::UnknownLengthTag {
                field: field.name.clone(),
                tag,
            });
        };
        let length_field = &fields[index];
        if length_field.data_type != DataType::Length {
            return Err(Error::NonLengthField {
                field: field.name.clone(),
                length_field: length_field.name.clone(),
            });
        }
        if let Some(first) = by_length_tag.insert(tag, &field.name) {
            return Err(Error::SharedLengthField {
                length_field: length_field.name.clone(),
                first: first.clone(),
                second: field.name.clone(),
            });
        }
    }
    Ok(())
}

fn is_integer(value: &str) -> bool {
    let digits = value.strip_prefix('-').unwrap_or(value);
    !digits.is_empty() && digits.bytes().all(|b| b.is_ascii_digit()) && value.parse::<i64>().is_ok()
}

fn index_components(
    components: &[spec::Component],
) -> Result<HashMap<&str, ComponentIndex>, Error> {
    let mut by_name = HashMap::new();
    for (index, component) in components.iter().enumerate() {
        if by_name
            .insert(component.name.as_str(), ComponentIndex(index))
            .is_some()
        {
            return Err(Error::DuplicateComponent {
                component: component.name.clone(),
            });
        }
    }
    Ok(by_name)
}

/// Splices a component's expansion into a scope. The reference's
/// requiredness applies to the expansion's own members; the instances of
/// groups within it keep theirs.
fn extend_with_expansion(
    members: &mut Vec<ResolvedMember>,
    expansion: &[ResolvedMember],
    is_required: bool,
) {
    members.extend(expansion.iter().map(|member| {
        let mut member = member.clone();
        member.combine_required(is_required);
        member
    }));
}

/// Makes the first member of an instance its delimiter, required whatever
/// its declared flag.
fn delimit(members: &mut [ResolvedMember], instance: &MemberContext) -> Result<FieldIndex, Error> {
    let Some(first) = members.first_mut() else {
        return Err(Error::EmptyGroup {
            context: instance.clone(),
        });
    };
    first.require();
    Ok(first.first_field())
}