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
use std::{
fs,
path::{Path, PathBuf},
};
use quick_xml::de::from_str;
use super::{
Dictionary, Version,
error::{BuilderError, Error, ValidationError},
};
use crate::xml;
/// Builder for creating and configuring FIX dictionaries.
///
/// This builder provides a fluent interface for configuring and creating
/// a FIX protocol dictionary from XML specification files.
pub struct DictionaryBuilder {
/// Path to the FIXT (transport) XML specification
fixt_xml_path: Option<PathBuf>,
/// Paths to FIX application-level XML specifications
fix_xml_paths: Vec<PathBuf>,
/// Allow custom FIX versions not in the standard list
allow_custom_version: bool,
/// Apply strict validation during parsing
strict_check: bool,
/// Whether to flatten component hierarchies
flatten_components: bool,
}
pub fn read_raw_dictionary(path: &Path) -> Result<xml::Dictionary, Error> {
let xml = fs::read_to_string(path)?;
Ok(from_str::<xml::Dictionary>(&xml)?)
}
pub fn read_raw_fixt_dictionary(path: &Path) -> Result<xml::Dictionary, Error> {
let raw_dictionary = read_raw_dictionary(path)?;
let version = raw_dictionary.version()?;
if !version.is_fixt() || version < Version::FIXT11 {
return Err(Error::Builder(BuilderError::IncompatibleVersion));
}
if raw_dictionary.header.members.is_empty() {
return Err(Error::Validation(ValidationError::EmptyContainer(
"Header".into(),
)));
}
if raw_dictionary.trailer.members.is_empty() {
return Err(Error::Validation(ValidationError::EmptyContainer(
"Trailer".into(),
)));
}
if let Some(msg) = raw_dictionary
.messages
.iter()
.find(|msg| !matches!(msg.msg_cat, xml::MsgCat::Admin))
{
return Err(Error::Validation(
ValidationError::UnexpectedMessageCategory(msg.msg_cat, msg.name.to_string()),
));
}
Ok(raw_dictionary)
}
pub fn read_raw_fix_dictionary(path: &Path) -> Result<xml::Dictionary, Error> {
let raw_dictionary = read_raw_dictionary(path)?;
let version = raw_dictionary.version()?;
if !version.is_fix() {
return Err(Error::Builder(BuilderError::IncompatibleVersion));
} else if version >= Version::FIX50 {
if !raw_dictionary.header.members.is_empty() {
return Err(Error::Validation(ValidationError::EmptyContainer(
"Header".into(),
)));
}
if !raw_dictionary.trailer.members.is_empty() {
return Err(Error::Validation(ValidationError::EmptyContainer(
"Trailer".into(),
)));
}
if let Some(msg) = raw_dictionary
.messages
.iter()
.find(|msg| !matches!(msg.msg_cat, xml::MsgCat::App))
{
return Err(Error::Validation(
ValidationError::UnexpectedMessageCategory(msg.msg_cat, msg.name.to_string()),
));
}
} else {
if raw_dictionary.header.members.is_empty() {
return Err(Error::Validation(ValidationError::EmptyContainer(
"Header".into(),
)));
}
if raw_dictionary.trailer.members.is_empty() {
return Err(Error::Validation(ValidationError::EmptyContainer(
"Trailer".into(),
)));
}
}
Ok(raw_dictionary)
}
impl Default for DictionaryBuilder {
fn default() -> Self {
Self::new()
}
}
impl DictionaryBuilder {
/// Creates a new, empty DictionaryBuilder with default settings
///
/// Use the builder's methods to configure and then call `build()` to create
/// the dictionary.
pub fn new() -> DictionaryBuilder {
DictionaryBuilder {
fixt_xml_path: None,
fix_xml_paths: Vec::new(),
allow_custom_version: false,
strict_check: false,
flatten_components: false,
}
}
/// Sets whether to allow custom FIX versions not in the standard list
///
/// By default, only standard FIX versions are accepted.
pub fn allow_custom_version(mut self, allow_custom_version: bool) -> Self {
self.allow_custom_version = allow_custom_version;
self
}
/// Sets whether to apply strict validation during dictionary parsing
///
/// When enabled, more rigorous checks are applied to the dictionary structure.
pub fn with_strict_check(mut self, strict_check: bool) -> Self {
self.strict_check = strict_check;
self
}
/// Adds a FIX application-level XML specification file to the builder
///
/// For FIX versions prior to 5.0, this is the only XML file needed.
/// For FIX 5.0+, this should be used along with `with_fixt_xml()`.
pub fn with_fix_xml(mut self, path: impl Into<PathBuf>) -> Self {
self.fix_xml_paths.push(path.into());
self
}
/// Adds multiple FIX application-level XML specification files to the builder
///
/// This is useful when working with multiple FIX versions or custom extensions.
pub fn with_fix_xmls(mut self, paths: impl IntoIterator<Item = PathBuf>) -> Self {
self.fix_xml_paths.extend(paths);
self
}
/// Sets the FIXT transport layer XML specification file
///
/// This is required for FIX 5.0+ versions, which separate transport (FIXT)
/// from application (FIX) layer specifications.
pub fn with_fixt_xml(mut self, path: impl Into<PathBuf>) -> Self {
self.fixt_xml_path = Some(path.into());
self
}
/// Sets whether to flatten component hierarchies
///
/// When enabled, nested components are flattened into their parent containers.
/// This simplifies the structure by removing intermediate component layers,
/// resulting in direct field references in messages and groups.
///
/// For example, if Message A contains Component B which contains Field C,
/// flattening would make Message A directly contain Field C. The resulting
/// dictionary has no components left, so code generation never has to
/// resolve one.
pub fn flatten_components(mut self, flatten_components: bool) -> Self {
self.flatten_components = flatten_components;
self
}
/// Builds the Dictionary from the configured sources
///
/// This method parses the XML files and constructs a complete FIX dictionary.
/// It will return an error if the dictionary configuration is invalid or if
/// parsing fails.
pub fn build(self) -> Result<Dictionary, Error> {
match (self.fixt_xml_path, self.fix_xml_paths.as_slice()) {
(None, []) => Err(Error::Builder(BuilderError::Unspecified)),
(None, [fix_xml_path]) => {
// Legacy FIX version
let dict = read_raw_fix_dictionary(fix_xml_path)?;
let dictionary = Dictionary::from_raw_dictionary(
dict,
self.flatten_components,
self.strict_check,
)?;
if self.flatten_components {
Ok(dictionary.flatten()?)
} else {
Ok(dictionary)
}
}
(None, [_, ..]) => Err(Error::Builder(BuilderError::IncompatibleVersion)),
(Some(fixt_xml_path), fix_xml_paths) => {
let fixt = read_raw_fixt_dictionary(&fixt_xml_path)?;
let mut fixt_dict = Dictionary::from_raw_dictionary(
fixt,
self.flatten_components,
self.strict_check,
)?;
for fix_xml_path in fix_xml_paths {
let fix = read_raw_fix_dictionary(fix_xml_path)?;
if fix.major < 5 {
return Err(Error::Builder(BuilderError::IncompatibleVersion));
}
let subdict = Dictionary::from_raw_dictionary(
fix,
self.flatten_components,
self.strict_check,
)?;
fixt_dict.subdictionaries.insert(subdict.version, subdict);
}
if self.flatten_components {
Ok(fixt_dict.flatten()?)
} else {
Ok(fixt_dict)
}
}
}
}
}