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

Generate in build.rs

One call parses the schema and generates the codec: generate(source, config). Then write the result:

Write withOutputUse when
.write_to_out_dir()Cargo’s $OUT_DIR; include with ergo_sbe::sbe_mod!(name) or include!Products: nothing generated lands in the source tree
.write_to(dir)A directory you choose, e.g. a gitignored src/generated/rust-analyzer go-to-definition into the generated code

Both publish every module or none, print cargo::rerun-if-changed for the schema file and every resolved include, and print generation warnings as cargo::warning.

SourceMeans
SchemaSource::file(path) (or a &Path or PathBuf)Reads the file; xi:include resolves relative to it
SchemaSource::xml(text)Inline XML, for example from include_str!; nothing to watch, so add your own rerun-if-changed

A bare &str does not compile as a source: say whether it is a path or XML.

The feature-tour sample writes to src/generated/:

#![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(
        ergo_sbe::SchemaSource::file("schemas/feature-tour.xml"),
        config,
    )?
    .write_to(&generated_dir)?;
}

generate is parse + Schema::from_ir + Generator::generate. Call those directly to inspect the token IR or to generate several schemas that share types: see Multi-Schema Patterns.