Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Extension XML

Schema annotations the SBE grammar does not own (extension attributes and foreign-namespace child elements) reach the generator as Extensions on the parse-time Token/Ir. Three generic ways use them, none of which knows any attribute name:

WayWhat you writeWhat you get
with_extension_consts(true)nothingevery mark as a &'static str const next to its item
Hooks (with_hook, with_try_hook)a closure over ItemContextany code: label methods, field pairs, renderers
ConversionSelector::extension_attra selectora domain type or conversion on marked fields

With none opted in, generated code is byte-identical (the empty golden diff is the gate), and no way touches an encoder, a setter or the wire.

What is captured

Every schema node keeps its own marks, matched local-name first: any prefix works (venue:jsonValue, ext:jsonValue), and so does no prefix.

Grammar attributes are read only without a prefix, as sbe-tool reads them. A prefixed grammar name (x:offset, x:blockLength, x:presence) is a mark like any other: it never changes layout or code, whatever its position, and nothing warns about it.

On the schemaCaptured as
Namespaced attribute (x:scaleBy="priceScale")ExtensionAttr { local_name, namespace, value }, document order
Unprefixed attribute the grammar does not define (scale="2")the same, with namespace: None
Foreign-namespace child elementExtensionElement { local_name, namespace, attributes, text, elements }, the whole subtree, text verbatim
xmlns… declarationsnever captured

On <messageSchema>, <message>, <field>, <group> and <data>, two kinds of unprefixed attribute are rejected as typos rather than captured: a grammar attribute the element does not take (blockLength on a <field>) and a near-miss of any grammar attribute (presense, semanticTpye, primitivType: within one edit per four letters). The type-level elements (<types>, <type>, <composite> and its members, <enum>, <validValue>, <set>, <choice>, <ref>) reject neither: a typo there is accepted, as a mark where the element keeps marks (and so as a const with with_extension_consts). A child element in the SBE namespace is always grammar, so a misspelt sbe:enumm fails; an unnamespaced unknown child element fails too.

How other tools treat marks:

CheckPrefixed mark (x:scale)Unprefixed mark (scale)
ergo-sbe parsecapturedcaptured
validate_against_sbe_xsd / with_xsd_validationacceptedrejected, except the sbe-tool extensions it allows (unit on a top-level <type>, jsonValue on <validValue> and <choice>) and where it does not check attributes: composite members and anything inside a <group>
the official sbe.xsd (xmllint --schema)rejectedrejected
sbe-toolXSD-checked only with -Dsbe.validation.xsd, then rejectedthe same

Use a namespace prefix when the schema must pass with_xsd_validation.

Look marks up with attr (any prefix), attr_in (exact namespace URI), child and is_empty; an extension element also exposes text():

#![allow(unused)]
fn main() {
use ergo_sbe::{ExtensionAttr, Extensions};

let ext = Extensions {
    attributes: vec![ExtensionAttr {
        local_name: "exponent".into(),
        namespace: Some("urn:example".into()),
        value: "priceExponent".into(),
    }],
    elements: vec![],
};
assert_eq!(ext.attr("exponent"), Some("priceExponent"));
assert_eq!(ext.attr_in("urn:example", "exponent"), Some("priceExponent"));
assert!(!ext.is_empty());
}

Grammar attributes (name, id, semanticType, …) are not copied into Extensions; read them off the Token’s own fields.

Every mark as a const

GenerationConfig::with_extension_consts(true) emits each extension attribute and each foreign child element (its text, then each of its attributes, then its own foreign children the same way) as an associated &'static str, named in upper snake case:

Mark onConst
<message>{Name}Schema::{ATTR}
message <field>, <group>, <data>{Name}Schema::{FIELD}_{ATTR}
<field>, <group>, <data> inside a group{Group}Decoder::{FIELD}_{ATTR}
<enum>, <set>, <composite>{Type}::{ATTR}
<validValue>, <choice>, composite member{Type}::{VALUE}_{ATTR}
foreign child <x:meta source="a">primary</x:meta>…::META = "primary", …::META_SOURCE = "a"
nested foreign child <x:meta><x:origin desk="b">feed</x:origin></x:meta>…::META_ORIGIN = "feed", …::META_ORIGIN_DESK = "b"
#![allow(unused)]
fn main() {
use ergo_sbe::GenerationConfig;

// <field name="maxQty" x:exponent="qtyExponent" x:jsonPath="limit"/> in
// message MaxAssetFilter gives MaxAssetFilterSchema::MAX_QTY_EXPONENT ==
// "qtyExponent" and MaxAssetFilterSchema::MAX_QTY_JSON_PATH == "limit".
let config = GenerationConfig::new("msgs").with_extension_consts(true);
}

Values are the schema’s own strings, never parsed. A const name that is already on its type fails generation with GenerateError::InvalidConfiguration rather than emitting a duplicate: two marks that spell one name (one local name in two namespaces, a repeated child element), a mark that spells a const the generator emits there (x:templateId on a <message> gives TEMPLATE_ID), or an <enum> mark that spells one of its variants. A mark whose name has no ASCII letter or digit (x:é) fails the same way, since it maps to no const name. Two generated or hook consts that share a name on one type, with or without the switch (a field named template gives TEMPLATE_ID), fail generation with GenerateError::NameClash. Repeats under different cfgs or on different instantiations of a generic type (impl G<u8>, impl G<u16>, or generic impls such as impl<T> H<T, u8>, impl<T> H<T, u16>) generate, as rustc accepts them. Repeats the check cannot see (a type alias, cfgs that can both hold such as cfg(test) and cfg(all(test))) are left to rustc.

Hooks on extension marks

A hook receives the marks of every node it can see on ItemContext; every captured node reaches one:

Captured nodeHook context
messageSchema root, <types>, includesany context, through schema.ir.extensions
<enum> / <validValue>Enum { extensions, variants[].extensions }
<set> / <choice>Set { extensions, choices[].extensions }
<composite> / memberComposite { extensions, fields[].extensions }
<message> and its foreign childrenMessageDecoder / MessageEncoder { extensions }
message-level <field> (constants included), <group>, <data>MessageDecoder.fields[]
<group> elementGroupEntryDecoder { extensions }, once per entry type, nested groups included
fields, nested <group>, <data> inside a group, any depthGroupEntryDecoder.fields[]
a group entry’s enclosing blocksGroupEntryDecoder.enclosing[]: the message, then each parent entry

Decoder contexts name the generated code a hook emits against, so it never re-derives a rule: each FieldInfo has schema_name (as the schema spells it, which is what a mark naming another field holds), accessor (the decoder method: price_wire under a conversion, type_ for a keyword, the domain getter under a domain type) and accessor_type (its exact return type, read from the generated decoder). MessageDecoder.schema_marker names the {Name}Schema type; GroupEntryDecoder gives the entry and group decoder type names. Registration, with_try_hook and the field table: Code-generation hooks.

Example hooks

sbe/tests/common/ext_recipes.rs holds venue-neutral hooks: ergo-sbe’s tests use it, the exchange sample’s build.rs includes it, and the code below is quoted from it. Copy and change it freely; it is not ergo-sbe API. Attribute and method names come from the caller.

Labels: labels("jsonValue", "json_value") adds fn json_value(self) -> &'static str to every enum or set with at least one marked value. The built-in as_str and Display keep the variant name.

/// `attr` on enum values or set choices -> `pub const fn {method}(self) -> &'static str`.
///
/// Emitted only for a type at least one of whose values carries `attr`. An
/// unmarked enum value gives its variant name and `NullVal` gives `"NullVal"`;
/// an unmarked set choice gives its schema name, and a set word that is not
/// exactly one choice gives `"UNKNOWN"`.
pub fn labels(
    attr: &'static str,
    method: &'static str,
) -> impl Fn(&ItemContext) -> Out + Send + Sync + 'static {
    move |ctx| {
        let (ty, body) = match ctx {
            ItemContext::Enum { name, variants, .. }
                if variants.iter().any(|v| v.extensions.attr(attr).is_some()) =>
            {
                let arms = variants.iter().map(|v| {
                    let var = format_ident!("{}", v.name);
                    let label = v.extensions.attr(attr).unwrap_or(&v.name);
                    quote! { Self::#var => #label, }
                });
                let body = quote! { match self { #(#arms)* Self::NullVal => "NullVal" } };
                (ident(name)?, body)
            }
            ItemContext::Set { name, choices, .. }
                if choices.iter().any(|c| c.extensions.attr(attr).is_some()) =>
            {
                let arms = choices.iter().map(|c| {
                    // The match runs on the set word: one bit per choice.
                    let word = 1u128.checked_shl(c.bit_position.into()).unwrap_or(0);
                    let word = Literal::u128_unsuffixed(word);
                    let label = c.extensions.attr(attr).unwrap_or(&c.label);
                    quote! { #word => #label, }
                });
                let body = quote! { match self.0 { #(#arms)* _ => "UNKNOWN" } };
                (ident(name)?, body)
            }
            _ => return Ok(vec![]),
        };
        let method = ident(method)?;
        let doc = format!("Label from the schema extension attribute `{attr}`.");
        Ok(vec![quote! {
            impl #ty {
                #[doc = #doc]
                #[must_use]
                pub const fn #method(self) -> &'static str { #body }
            }
        }])
    }
}

Field pairs: pairs("exponent", "with_exponent") adds {field}_with_exponent for every fixed field whose mark names another fixed field, returning both accessors’ values with their exact types (Option, domain types and [u8; 16] arrays included). A target in an enclosing block (a group mantissa whose exponent sits on the message) makes the helper take that block’s decoder; a target found nowhere fails generation.

/// `attr` on a fixed field whose value names another fixed field ->
/// `{field}_{suffix}` returning both decoder accessors' values, exactly typed.
///
/// A target in the same block gives `fn x_{suffix}(&self) -> (A, B)`. A target
/// only in an enclosing block (a group field naming a message field) gives
/// `fn x_{suffix}(&self, outer: &Outer<'a>) -> (A, B)`, reading the target
/// from the nearest enclosing decoder that has it. A target found nowhere
/// fails generation.
pub fn pairs(
    attr: &'static str,
    suffix: &'static str,
) -> impl Fn(&ItemContext) -> Out + Send + Sync + 'static {
    move |ctx| {
        let (owner, fields, enclosing) = match ctx {
            ItemContext::MessageDecoder { name, fields, .. } => (name, fields, &[][..]),
            ItemContext::GroupEntryDecoder {
                name,
                fields,
                enclosing,
                ..
            } => (name, fields, enclosing.as_slice()),
            _ => return Ok(vec![]),
        };
        let mut methods = Vec::new();
        for f in fields {
            let Some(target) = f.extensions.attr(attr) else {
                continue;
            };
            let (fa, fty) = accessor(f)?;
            let helper = ident(&format!("{}_{suffix}", f.name))?;
            if let Some(t) = fields.iter().find(|t| t.schema_name == target) {
                let (ta, tty) = accessor(t)?;
                methods.push(quote! {
                    #[inline]
                    pub fn #helper(&self) -> (#fty, #tty) { (self.#fa(), self.#ta()) }
                });
                continue;
            }
            let Some((outer, t)) = enclosing.iter().rev().find_map(|b| {
                b.fields
                    .iter()
                    .find(|t| t.schema_name == target)
                    .map(|t| (b, t))
            }) else {
                return Err(format!(
                    "{owner}: `{}` {attr}=\"{target}\" names no field",
                    f.schema_name
                ));
            };
            let (ta, tty) = accessor(t)?;
            let outer = ident(&outer.decoder)?;
            methods.push(quote! {
                #[inline]
                pub fn #helper(&self, outer: &#outer<'a>) -> (#fty, #tty) {
                    (self.#fa(), outer.#ta())
                }
            });
        }
        if methods.is_empty() {
            return Ok(vec![]);
        }
        let owner = ident(owner)?;
        Ok(vec![quote! { impl<'a> #owner<'a> { #(#methods)* } }])
    }
}

Shared helpers:

/// One Rust identifier from user text, or an error.
pub fn ident(s: &str) -> Result<Ident, String> {
    let mut it = s
        .parse::<TokenStream>()
        .map_err(|e| format!("`{s}`: {e}"))?
        .into_iter();
    match (it.next(), it.next()) {
        (Some(TokenTree::Ident(i)), None) => Ok(i),
        _ => Err(format!("`{s}` is not one Rust identifier")),
    }
}

/// A fixed field's decoder accessor and its exact return type.
pub fn accessor(f: &FieldInfo) -> Result<(Ident, TokenStream), String> {
    match (f.kind(), &f.accessor, &f.accessor_type) {
        (FieldKind::Fixed, Some(name), Some(ty)) => {
            let ty = ty.parse().map_err(|e| format!("`{ty}`: {e}"))?;
            Ok((ident(name)?, ty))
        }
        _ => Err(format!(
            "`{}` must be a fixed field with a decoder accessor",
            f.schema_name
        )),
    }
}

Register them as try-hooks, so a bad mark fails the build with GenerateError::Hook naming the item:

let config = ergo_sbe::GenerationConfig::new("msgs")
    .with_try_hook(ext_recipes::labels("jsonValue", "json_value"))
    .with_try_hook(ext_recipes::pairs("exponent", "with_exponent"))
    .with_extension_consts(true);

ExtensionAttr conversions

ConversionSelector::extension_attr(local_name) selects message and group fields carrying a mark; extension_attr_value(local_name, value) also pins the value exactly:

#![allow(unused)]
fn main() {
use ergo_sbe::{ConversionSelector, GenerationConfig};

let config = GenerationConfig::new("msgs").with_domain_type(
    ConversionSelector::extension_attr_value("exponent", "priceExponent"),
    "my_app::PriceMantissa",
);
}

Precedence is FieldPath > SemanticType > ExtensionAttr > NamedType; two extension domain selectors tying on one field with different Rust types fail generation instead of falling back to registration order. Scope is message and group fields only: marks on <data>, <group> elements and type-level declarations reach hooks but not this selector. The Rust type always comes from build.rs, never from the schema. Details: with_conversion vs with_domain_type.

Worked example

The exchange example builds a working tool on a published venue schema through these generic parts only: a try-hook generates a write_json renderer from the schema’s jsonPath, exponent and jsonDefaultValue marks, and recorded real SBE responses render to JSON equal to the venue’s own JSON responses.