Generate in build.rs
Two APIs — pick whichever fits your project:
generate_to_dir
writes generated codecs to a directory you control (prefer src/generated/ for
IDE go-to-definition). The feature-tour sample uses this pattern:
#![allow(unused)]
fn main() {
let generated_dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src/generated");
let config = ergo_sbe::GenerationConfig::new("feature_tour")
.with_domain_objects(ergo_sbe::DomainVarData::Strings)
.with_domain_type(
ergo_sbe::ConversionSelector::named_type("BooleanType"),
"bool",
)
.with_domain_type(
ergo_sbe::ConversionSelector::semantic_type("UTCTimestamp"),
"chrono::DateTime<chrono::Utc>",
)
.with_conversion(ergo_sbe::ConversionSelector::named_type("Decimal"))
// Same shape as Decimal, but the app supplies the impl itself — see
// demo_domain_type_manual_impl in src/lib.rs.
.with_manual_domain_type(
ergo_sbe::ConversionSelector::named_type("ManualDecimal"),
"rust_decimal::Decimal",
);
ergo_sbe::generate_to_dir("schemas/feature-tour.xml", config, &generated_dir)?;
}
generate_to_out_dir
writes to Cargo’s $OUT_DIR and emits cargo::rerun-if-changed automatically.
Use with include!(concat!(env!("OUT_DIR"), …)) — simpler, but
rust-analyzer usually cannot jump into generated code.
Schema from a string / include_str!:
generate_str_to_out_dir.
Need multi-schema or custom output paths? Use the lower-level
parse_file +
Generator
API (same steps the helper runs). For shared types across schemas, see
Multi-Schema Patterns below.