ergo-sbe 0.1.20

Simple Binary Encoding (SBE) — opinionated, idiomatic Rust code generation for low-latency trading protocols.
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
//! Optional SBE **XSD-shaped** structural validation (not a full W3C engine).
//!
//! | Item | Purpose |
//! |------|---------|
//! | [`SBE_XSD`] | Official FPL `sbe.xsd` text (for external tools / docs) |
//! | [`validate_against_sbe_xsd`] | Pure-Rust checks: root, children, known attributes |
//! | [`crate::parse_with_xsd_validation`] | Validate then [`crate::parse`] |
//!
//! Semantic IR rules (offsets, types, duplicates) still come from the main
//! parser / resolver.
//!
//! # Relationship to [`crate::parse`]
//!
//! [`parse`](crate::parse) is **always on** and rejects malformed XML, a bad
//! root, unexpected elements, and unknown attributes on its own. This module
//! is an **opt-in, deliberately stricter** gate for schema authors; it is
//! not a prerequisite for parsing.
//!
//! Attribute allow-lists are shared with the parser (one private
//! `schema_attrs` module owns them) so the two cannot drift apart.
//!
//! Where the published XSD is stricter than sbe-tool itself, sbe-tool wins:
//! the XSD marks `messageSchema/@version` `use="required"`, but upstream's own
//! test resources omit it, so requiring it here would reject schemas the
//! reference implementation accepts. Only `@id` is required.
//!
//! Vendor extensions the published XSD does not declare but sbe-tool accepts
//! (`characterEncoding` on `<data>`, `unit` on `<type>`, `jsonValue` on
//! `<validValue>` / `<choice>`, `package` on `<types>`) are permitted, as are
//! all namespaced attributes (`xsi:*`, `xi:*`, and vendor namespaces such as
//! Binance's `mbx:*`). Rejecting those would fail real-world schemas.

use miette::Diagnostic;
use thiserror::Error;

/// Official FPL SBE 1.0 Draft Standard XSD, embedded for tooling / external
/// validators. Content matches Real Logic's `sbe-tool` resource
/// `fpl/sbe.xsd`.
pub const SBE_XSD: &str = include_str!("xsd/sbe.xsd");

/// Errors from [`validate_against_sbe_xsd`].
#[derive(Debug, Error, Diagnostic)]
pub enum XsdValidationError {
    /// Input is not well-formed XML.
    #[error("XML parse error: {0}")]
    #[diagnostic(code(ergo_sbe::xsd::malformed))]
    MalformedXml(String),

    /// Root element is not `messageSchema`.
    #[error("root element must be messageSchema, found `{found}`")]
    #[diagnostic(code(ergo_sbe::xsd::bad_root))]
    BadRoot {
        /// Tag that was found.
        found: String,
    },

    /// Required schema attribute is missing.
    #[error("messageSchema is missing required attribute `{attr}`")]
    #[diagnostic(code(ergo_sbe::xsd::missing_attr))]
    MissingAttribute {
        /// Attribute name.
        attr: &'static str,
    },

    /// Element is not allowed by the SBE XSD element model.
    #[error("element `{element}` is not allowed under `{parent}` by sbe.xsd")]
    #[diagnostic(code(ergo_sbe::xsd::unexpected_element))]
    UnexpectedElement {
        /// Parent element local name.
        parent: String,
        /// Child element local name.
        element: String,
    },

    /// Attribute is not recognised on this element.
    #[error("attribute `{attr}` is not allowed on `{element}` by sbe.xsd")]
    #[diagnostic(code(ergo_sbe::xsd::unexpected_attr))]
    UnexpectedAttribute {
        /// Element local name.
        element: String,
        /// Attribute name.
        attr: String,
    },
}

fn local_name(tag: &str) -> &str {
    tag.rsplit(':').next().unwrap_or(tag)
}

/// Validate `xml` against the SBE XSD element model (structural, pure Rust).
///
/// This is **not** a full XSD processor. It catches schema-shape mistakes
/// that the XSD would reject (wrong root, illegal children, unknown attrs on
/// core elements). Semantic checks (duplicate ids, type resolution, …) remain
/// in [`crate::parse`] / resolve.
///
/// # Example
///
/// ```rust
/// use ergo_sbe::{validate_against_sbe_xsd, SBE_XSD};
/// # let xml = r#"<?xml version="1.0"?><messageSchema package="t" id="1" version="0"
/// # byteOrder="littleEndian"><types><composite name="messageHeader">
/// # <type name="blockLength" primitiveType="uint16"/>
/// # <type name="templateId" primitiveType="uint16"/>
/// # <type name="schemaId" primitiveType="uint16"/>
/// # <type name="version" primitiveType="uint16"/>
/// # </composite></types></messageSchema>"#;
/// // Also validates against the bundled SBE XSD:
/// validate_against_sbe_xsd(xml)?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn validate_against_sbe_xsd(xml: &str) -> Result<(), XsdValidationError> {
    let doc = roxmltree::Document::parse(xml)
        .map_err(|e| XsdValidationError::MalformedXml(e.to_string()))?;
    let root = doc.root_element();
    let root_name = local_name(root.tag_name().name());
    if root_name != "messageSchema" {
        return Err(XsdValidationError::BadRoot {
            found: root_name.to_string(),
        });
    }

    // `id` is required: without it there is no schema identity to encode.
    //
    // `version` is NOT required here even though the published XSD marks it
    // `use="required"`. sbe-tool's own test resources ship schemas that omit
    // it (e.g. `basic-schema.xml`, `new-order-single-schema.xml`), so the
    // reference implementation does not enforce it either, and `parse`
    // defaults it to 0. Enforcing it would reject upstream-mirrored fixtures
    // — a validator stricter than both the parser and the reference tool only
    // produces false positives.
    if root.attribute("id").is_none() {
        return Err(XsdValidationError::MissingAttribute { attr: "id" });
    }
    check_attrs("messageSchema", root, crate::schema_attrs::MESSAGE_SCHEMA)?;

    for child in root.children().filter(|n| n.is_element()) {
        let name = local_name(child.tag_name().name());
        match name {
            "types" => validate_types(child)?,
            "message" => validate_message(child)?,
            // XInclude is outside the stock XSD but supported by both tools.
            "include" => {}
            other => {
                return Err(XsdValidationError::UnexpectedElement {
                    parent: "messageSchema".into(),
                    element: other.into(),
                });
            }
        }
    }
    Ok(())
}

fn check_attrs(
    element: &str,
    node: roxmltree::Node<'_, '_>,
    allowed: &[&str],
) -> Result<(), XsdValidationError> {
    for attr in node.attributes() {
        // Any namespaced attribute is outside the SBE grammar — `xsi:*`,
        // `xi:*`, and vendor extensions alike (Binance ships `mbx:exponent`).
        // Note `attr.name()` is the LOCAL name, so a `contains(':')` test
        // never fires; the namespace is what identifies these.
        if attr.namespace().is_some() {
            continue;
        }
        let name = local_name(attr.name());
        if name.starts_with("xmlns") {
            continue;
        }
        if !allowed.contains(&name) {
            return Err(XsdValidationError::UnexpectedAttribute {
                element: element.into(),
                attr: name.into(),
            });
        }
    }
    Ok(())
}

fn validate_types(node: roxmltree::Node<'_, '_>) -> Result<(), XsdValidationError> {
    // `package` on <types> is not in the published XSD but sbe-tool emits and
    // accepts it (it scopes generated types for that block).
    check_attrs("types", node, &["package"])?;
    for child in node.children().filter(|n| n.is_element()) {
        let name = local_name(child.tag_name().name());
        match name {
            "type" => check_attrs(
                "type",
                child,
                &[
                    "name",
                    "primitiveType",
                    "length",
                    "presence",
                    "nullValue",
                    "minValue",
                    "maxValue",
                    "characterEncoding",
                    "epoch",
                    "timeUnit",
                    // `unit` is an sbe-tool extension carried by real schemas.
                    "unit",
                    "semanticType",
                    "description",
                    "sinceVersion",
                    "deprecated",
                    "offset",
                    "valueRef",
                ],
            )?,
            "composite" => validate_composite(child)?,
            "enum" => validate_enum(child)?,
            "set" => validate_set(child)?,
            other => {
                return Err(XsdValidationError::UnexpectedElement {
                    parent: "types".into(),
                    element: other.into(),
                });
            }
        }
    }
    Ok(())
}

fn validate_composite(node: roxmltree::Node<'_, '_>) -> Result<(), XsdValidationError> {
    check_attrs(
        "composite",
        node,
        &[
            "name",
            "description",
            "semanticType",
            "sinceVersion",
            "deprecated",
            "offset",
        ],
    )?;
    for child in node.children().filter(|n| n.is_element()) {
        let name = local_name(child.tag_name().name());
        match name {
            "type" | "enum" | "set" | "ref" | "composite" => {}
            "description" | "comment" => {}
            other => {
                return Err(XsdValidationError::UnexpectedElement {
                    parent: "composite".into(),
                    element: other.into(),
                });
            }
        }
    }
    Ok(())
}

fn validate_enum(node: roxmltree::Node<'_, '_>) -> Result<(), XsdValidationError> {
    check_attrs("enum", node, crate::schema_attrs::ENUM)?;
    for child in node.children().filter(|n| n.is_element()) {
        let name = local_name(child.tag_name().name());
        match name {
            "validValue" => check_attrs(
                "validValue",
                child,
                // `jsonValue` is an sbe-tool extension used by real schemas.
                &[
                    "name",
                    "description",
                    "sinceVersion",
                    "deprecated",
                    "jsonValue",
                ],
            )?,
            "description" | "comment" => {}
            other => {
                return Err(XsdValidationError::UnexpectedElement {
                    parent: "enum".into(),
                    element: other.into(),
                });
            }
        }
    }
    Ok(())
}

fn validate_set(node: roxmltree::Node<'_, '_>) -> Result<(), XsdValidationError> {
    check_attrs(
        "set",
        node,
        &[
            "name",
            "encodingType",
            "description",
            "sinceVersion",
            "deprecated",
            "semanticType",
        ],
    )?;
    for child in node.children().filter(|n| n.is_element()) {
        let name = local_name(child.tag_name().name());
        match name {
            "choice" => check_attrs(
                "choice",
                child,
                // `jsonValue` is an sbe-tool extension used by real schemas.
                &[
                    "name",
                    "description",
                    "sinceVersion",
                    "deprecated",
                    "jsonValue",
                ],
            )?,
            "description" | "comment" => {}
            other => {
                return Err(XsdValidationError::UnexpectedElement {
                    parent: "set".into(),
                    element: other.into(),
                });
            }
        }
    }
    Ok(())
}

fn validate_message(node: roxmltree::Node<'_, '_>) -> Result<(), XsdValidationError> {
    check_attrs("message", node, crate::schema_attrs::MESSAGE)?;
    for child in node.children().filter(|n| n.is_element()) {
        let name = local_name(child.tag_name().name());
        match name {
            "field" => check_attrs("field", child, crate::schema_attrs::FIELD_LIKE)?,
            "group" => validate_group(child)?,
            "data" => check_attrs("data", child, crate::schema_attrs::FIELD_LIKE)?,
            "description" | "comment" => {}
            other => {
                return Err(XsdValidationError::UnexpectedElement {
                    parent: "message".into(),
                    element: other.into(),
                });
            }
        }
    }
    Ok(())
}

fn validate_group(node: roxmltree::Node<'_, '_>) -> Result<(), XsdValidationError> {
    check_attrs("group", node, crate::schema_attrs::GROUP)?;
    for child in node.children().filter(|n| n.is_element()) {
        let name = local_name(child.tag_name().name());
        match name {
            "field" | "group" | "data" | "description" | "comment" => {}
            other => {
                return Err(XsdValidationError::UnexpectedElement {
                    parent: "group".into(),
                    element: other.into(),
                });
            }
        }
    }
    Ok(())
}

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

    #[test]
    fn accepts_minimal_valid_schema() -> Result<(), Box<dyn std::error::Error>> {
        let xml = r#"<?xml version="1.0"?>
        <messageSchema package="t" id="1" version="0" byteOrder="littleEndian">
          <types>
            <composite name="messageHeader">
              <type name="blockLength" primitiveType="uint16"/>
              <type name="templateId" primitiveType="uint16"/>
              <type name="schemaId" primitiveType="uint16"/>
              <type name="version" primitiveType="uint16"/>
            </composite>
            <type name="u32" primitiveType="uint32"/>
          </types>
          <message name="M" id="1">
            <field name="x" id="1" type="u32"/>
          </message>
        </messageSchema>"#;
        validate_against_sbe_xsd(xml)?;
        Ok(())
    }

    #[test]
    fn rejects_bad_root() {
        let xml = r#"<?xml version="1.0"?><notSchema id="1" version="0"/>"#;
        assert!(matches!(
            validate_against_sbe_xsd(xml),
            Err(XsdValidationError::BadRoot { .. })
        ));
    }

    #[test]
    fn rejects_unknown_message_child() {
        let xml = r#"<?xml version="1.0"?>
        <messageSchema id="1" version="0">
          <types/>
          <message name="M" id="1">
            <notAField name="x"/>
          </message>
        </messageSchema>"#;
        assert!(matches!(
            validate_against_sbe_xsd(xml),
            Err(XsdValidationError::UnexpectedElement { .. })
        ));
    }

    #[test]
    fn accepts_enum_set_group_and_var_data_shapes() -> Result<(), Box<dyn std::error::Error>> {
        let xml = r#"<?xml version="1.0"?>
        <messageSchema package="t" id="1" version="0">
          <types>
            <composite name="messageHeader">
              <type name="blockLength" primitiveType="uint16"/>
              <type name="templateId" primitiveType="uint16"/>
              <type name="schemaId" primitiveType="uint16"/>
              <type name="version" primitiveType="uint16"/>
            </composite>
            <composite name="groupSizeEncoding">
              <type name="blockLength" primitiveType="uint16"/>
              <type name="numInGroup" primitiveType="uint16"/>
            </composite>
            <composite name="varStringEncoding">
              <type name="length" primitiveType="uint32"/>
              <type name="varData" primitiveType="uint8" length="0"/>
            </composite>
            <enum name="Side" encodingType="uint8">
              <validValue name="Buy">1</validValue>
              <validValue name="Sell">2</validValue>
            </enum>
            <set name="Flags" encodingType="uint8">
              <choice name="Firm">0</choice>
            </set>
          </types>
          <message name="Order" id="1">
            <field name="side" id="1" type="Side"/>
            <group name="fills" id="2" dimensionType="groupSizeEncoding">
              <field name="quantity" id="3" type="uint32"/>
              <data name="venue" id="4" type="varStringEncoding"/>
            </group>
            <data name="account" id="5" type="varStringEncoding"/>
          </message>
        </messageSchema>"#;

        validate_against_sbe_xsd(xml)?;
        Ok(())
    }

    #[test]
    fn rejects_malformed_missing_attributes_and_unknown_type_shapes() {
        type ValidationCase<'a> = (&'a str, &'a str, fn(&XsdValidationError) -> bool);
        let cases: [ValidationCase<'_>; 4] = [
            (
                "<messageSchema",
                "malformed XML",
                |error: &XsdValidationError| matches!(error, XsdValidationError::MalformedXml(_)),
            ),
            (
                r#"<messageSchema package="t" version="0"/>"#,
                "missing schema id",
                |error: &XsdValidationError| {
                    matches!(error, XsdValidationError::MissingAttribute { attr: "id" })
                },
            ),
            (
                r#"<messageSchema package="t" id="1" version="0" surprise="yes"/>"#,
                "unknown root attribute",
                |error: &XsdValidationError| {
                    matches!(error, XsdValidationError::UnexpectedAttribute { .. })
                },
            ),
            (
                r#"<messageSchema package="t" id="1" version="0"><types><unknown/></types></messageSchema>"#,
                "unknown type element",
                |error: &XsdValidationError| {
                    matches!(error, XsdValidationError::UnexpectedElement { .. })
                },
            ),
        ];

        for (xml, context, predicate) in cases {
            let result = validate_against_sbe_xsd(xml);
            assert!(
                result.as_ref().is_err_and(predicate),
                "{context}: {result:?}"
            );
        }
    }

    #[test]
    fn embedded_xsd_is_present() {
        assert!(SBE_XSD.contains("messageSchema"));
        assert!(SBE_XSD.contains("xs:schema"));
    }

    fn enum_null_schema(encoding: &str, null_value: &str) -> String {
        format!(
            r#"<?xml version="1.0"?>
            <messageSchema package="t" id="1" version="0" byteOrder="littleEndian">
              <types>
                <composite name="messageHeader">
                  <type name="blockLength" primitiveType="uint16"/>
                  <type name="templateId" primitiveType="uint16"/>
                  <type name="schemaId" primitiveType="uint16"/>
                  <type name="version" primitiveType="uint16"/>
                </composite>
                <enum name="Code" encodingType="{encoding}" nullValue="{null_value}">
                  <validValue name="Ok">0</validValue>
                </enum>
              </types>
              <message name="M" id="1">
                <field name="code" id="1" type="Code"/>
              </message>
            </messageSchema>"#
        )
    }

    #[test]
    fn accepts_unsigned_enum_null_value() -> Result<(), Box<dyn std::error::Error>> {
        let xml = enum_null_schema("uint8", "99");
        validate_against_sbe_xsd(&xml)?;
        crate::parse_with_xsd_validation(&xml)?;
        Ok(())
    }

    #[test]
    fn accepts_signed_enum_null_value() -> Result<(), Box<dyn std::error::Error>> {
        let xml = enum_null_schema("int8", "-1");
        validate_against_sbe_xsd(&xml)?;
        crate::parse_with_xsd_validation(&xml)?;
        Ok(())
    }

    #[test]
    fn rejects_unknown_non_namespaced_enum_attribute() {
        let xml = r#"<?xml version="1.0"?>
        <messageSchema package="t" id="1" version="0">
          <types>
            <enum name="Code" encodingType="uint8" surprise="yes">
              <validValue name="Ok">0</validValue>
            </enum>
          </types>
        </messageSchema>"#;
        assert!(matches!(
            validate_against_sbe_xsd(xml),
            Err(XsdValidationError::UnexpectedAttribute {
                element,
                attr
            }) if element == "enum" && attr == "surprise"
        ));
    }
}