refix-codegen 0.8.0

Code generator for typed FIX protocol messages, for the ReFIX engine
Documentation
use crate::flat;
use refix_dictionary::spec::{
    Category, Component, ComponentRef, DataType, Field, FieldRef, Group, Member, Message, Protocol,
    Spec, Version,
};
use refix_dictionary::{Dictionary, Tag, dictionary, quickfix};

/// A dictionary whose one message, `NewOrderSingle`, holds each of `fields`.
pub fn message_with(fields: Vec<Field>) -> Dictionary {
    let members = fields
        .iter()
        .map(|field| {
            Member::Field(FieldRef {
                tag: field.tag,
                is_required: false,
            })
        })
        .collect();
    Spec {
        version: Version {
            protocol: Protocol::Fix,
            major: 4,
            minor: 4,
            service_pack: 0,
        },
        header: vec![],
        trailer: vec![],
        messages: vec![Message {
            name: "NewOrderSingle".to_owned(),
            msg_type: "D".to_owned(),
            members,
            category: Category::App,
        }],
        fields,
        components: vec![],
    }
    .resolve()
    .unwrap()
    .dictionary
}

/// A dictionary whose one message, `NewOrderSingle`, includes `Instrument`
/// and `OrderQtyData`. `Instrument` holds a field, a group through
/// `SecAltIDGrp` and the fields of `SecurityXML`.
pub fn instrument_dictionary() -> Dictionary {
    let xml = r#"<fix type="FIX" major="4" minor="4" servicepack="0">
  <header/>
  <messages>
    <message name="NewOrderSingle" msgtype="D" msgcat="app">
      <field name="ClOrdID" required="Y"/>
      <component name="Instrument" required="Y"/>
      <component name="OrderQtyData" required="N"/>
    </message>
  </messages>
  <trailer/>
  <components>
    <component name="Instrument">
      <field name="Symbol" required="Y"/>
      <component name="SecAltIDGrp" required="N"/>
      <component name="SecurityXML" required="N"/>
    </component>
    <component name="SecAltIDGrp">
      <group name="NoSecurityAltID" required="N">
        <field name="SecurityAltID" required="N"/>
      </group>
    </component>
    <component name="SecurityXML">
      <field name="SecurityXMLSchema" required="N"/>
    </component>
    <component name="OrderQtyData">
      <field name="OrderQty" required="N"/>
    </component>
  </components>
  <fields>
    <field number="11" name="ClOrdID" type="STRING"/>
    <field number="55" name="Symbol" type="STRING"/>
    <field number="454" name="NoSecurityAltID" type="NUMINGROUP"/>
    <field number="455" name="SecurityAltID" type="STRING"/>
    <field number="1186" name="SecurityXMLSchema" type="STRING"/>
    <field number="38" name="OrderQty" type="INT"/>
  </fields>
</fix>"#;
    quickfix::parse(xml).unwrap().dictionary
}

/// Runs `read` on a group counted by `count_field`, declared inline in a
/// message or as the sole member of `component`.
pub fn with_group<R>(
    component: Option<&str>,
    count_field: &str,
    read: impl FnOnce(dictionary::Group<'_>) -> R,
) -> R {
    let group = Member::Group(Group {
        count_tag: Tag(453),
        is_required: false,
        members: vec![Member::Field(FieldRef {
            tag: Tag(448),
            is_required: false,
        })],
    });
    let (members, components) = match component {
        Some(name) => (
            vec![Member::Component(ComponentRef {
                name: name.to_owned(),
                is_required: false,
            })],
            vec![Component {
                name: name.to_owned(),
                members: vec![group],
            }],
        ),
        None => (vec![group], vec![]),
    };
    let field = |name: &str, tag: u32, data_type: DataType| Field {
        name: name.to_owned(),
        tag: Tag(tag),
        data_type,
        length_tag: None,
        values: vec![],
    };
    let dictionary = Spec {
        version: Version {
            protocol: Protocol::Fix,
            major: 4,
            minor: 4,
            service_pack: 0,
        },
        header: vec![],
        trailer: vec![],
        messages: vec![Message {
            name: "NewOrderSingle".to_owned(),
            msg_type: "D".to_owned(),
            members,
            category: Category::App,
        }],
        fields: vec![
            field(count_field, 453, DataType::NumInGroup),
            field("PartyID", 448, DataType::String),
        ],
        components,
    }
    .resolve()
    .unwrap()
    .dictionary;
    let message = dictionary.messages().next().unwrap();
    match flat::members(message.members()).next() {
        Some(flat::Member::Group(group)) => read(group),
        other => panic!("expected a group, found {other:?}"),
    }
}