use crate::tag::Tag;
use rustc_hash::FxHashMap;
use std::collections::HashSet;
use std::fmt::Write as _;
#[derive(Debug, Clone, Default)]
pub struct FixmlAbbreviations {
pub tag_to_abbr: FxHashMap<Tag, String>,
pub abbr_to_tag: FxHashMap<String, Tag>,
pub msg_type_to_abbr: FxHashMap<String, String>,
pub abbr_to_msg_type: FxHashMap<String, String>,
pub component_to_abbr: FxHashMap<String, String>,
pub abbr_to_component: FxHashMap<String, String>,
pub msg_not_req_xml: FxHashMap<String, bool>,
pub fix_version: String,
pub contents: FxHashMap<String, Vec<ContentEntry>>,
pub msg_type_to_component_id: FxHashMap<String, String>,
pub component_name_to_id: FxHashMap<String, String>,
pub component_type: FxHashMap<String, String>,
pub component_tags: FxHashMap<String, HashSet<Tag>>,
pub component_all_tags: FxHashMap<String, HashSet<Tag>>,
}
#[derive(Debug, Clone)]
pub struct ContentEntry {
pub tag_text: String,
pub is_field: bool,
pub indent: usize,
pub position: usize,
pub required: bool,
}
impl FixmlAbbreviations {
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)
}
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)
}
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
}
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());
}
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)
}
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)
}
pub fn member_indent(&self, component_name: &str) -> usize {
usize::from(self.is_repeating(component_name))
}
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())
}
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()
}
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()
}
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"))
}
}
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}"))?;
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,
});
}
for entries in abbr.contents.values_mut() {
entries.sort_by_key(|e| e.position);
}
Ok(())
}
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) {
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");
}
#[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)); assert_eq!(abbr.msg_not_req_xml.get("D"), Some(&false));
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() {
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());
}
#[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"
);
}
}
}