Codegen as a library
Generator-API examples showing how to call ergo-sbe as a library to generate and
inspect codecs programmatically (without a build.rs).
The other samples (l3-book, sbe-feature-tour, exchange-example) all use
build.rs to generate codecs at compile time and then exercise them
end-to-end. These examples complement them by showing the generator API
itself, which is useful for tooling, golden-file workflows, and understanding what the
generator produces.
From schema XML to generated Rust
Animation, in words: it follows a small illustrative Quote schema (two
fixed fields, a legs group and a venue var-data field), not the car schema
the examples use. generate(source, config) is parse, Schema::from_ir,
then Generator::generate.
- An optional XSD shape check (
with_xsd_validation(), off by default) runs on the root document’s text before anything is parsed. parseturns the XML into one flat stream of 46 Begin/End tokens, with each type reference inlined.- Still inside
parse,resolve_schemafills the same tokens in place:priceat offset 0,qtyat 8, a block length of 12, a group entry length of 8, null/min/max defaults, and checks for duplicate template ids. Schema::from_ironly wraps the result. TheGeneratorchecks theGenerationConfig, then splits the tokens into typed messages, groups and fields.- One decoder call per message emits
QuoteDecoder, the stagedinto_*chain,ordered()andmemoized(). The encoder call emits the type-stateQuoteEncoderandcompute_encoded_length(legs_count, venue_len), which is 40 bytes for 2 legs and a 4-byte venue (48 with the header). synandprettypleaseproduce one in-memory module, which is where these examples readset.modules().write_to_out_dir()writes$OUT_DIR/quote.rs(all or none) and printsrerun-if-changedfor the schema.sbe_mod!(quote)includes it, and a plaininclude!does the same without a normal dependency onergo-sbe.
If a field names an undefined type, parse stops there with
ergo_sbe::schema_parse::invalid and the line underlined. resolve and the
generator never run. All of this happens at build time, so a running program
never parses a schema. The cost is a rebuild for each schema change, and an
sbe_rt in every module unless you use with_shared_module or
with_external_sbe_rt. The book records no timings for the pipeline.
Examples
# From the repository root; this crate is not a workspace member.
cargo run --manifest-path samples/sbe-codegen-examples/Cargo.toml --example flyweight
cargo run --manifest-path samples/sbe-codegen-examples/Cargo.toml --example domain_objects
cargo run --manifest-path samples/sbe-codegen-examples/Cargo.toml --example l3_nested
cargo run --manifest-path samples/sbe-codegen-examples/Cargo.toml --example dump_gen
What each shows
| Example | Demonstrates |
|---|---|
flyweight | Generator::generate() with default config, giving zero-copy flyweights |
domain_objects | with_domain_objects(DomainVarData::Strings): owned DTOs (String var-data) + try_from_decoder |
l3_nested | The full type graph for 3-level nested groups (L1, L2 and L3 entry types) |
dump_gen | The complete generated Rust source for inspection |
All examples parse the canonical car schema.