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

Generated wrappers

The compiler generates typed wrappers from a schema, so application code does not hand-build Value arrays. For a simple Rust-only project, generate the module once with the CLI instead of adding a build script:

predsync schema_build --set schema/telemetry.yaml --out src/telemetry.rs

Or, from a source checkout:

pixi run schema-build --set schema/telemetry.yaml --out src/telemetry.rs

Use a build script when you need Cargo to regenerate the module when schemas change, or when you register custom predictors:

fn main() {
    predsync_compiler::Builder::new()
        .set("schema/telemetry.yaml")
        .generate("telemetry.rs")
        .expect("generate telemetry wrappers");
}

Builder::generate emits rerun-if-changed directives for every schema file.

Include the generated file in a module:

#![allow(unused)]
fn main() {
include!(concat!(env!("OUT_DIR"), "/telemetry.rs"));
}

The generated Rust module exposes:

  • namespace and message modules;
  • ID and FIELD_* constants;
  • one Rust enum per schema enum, with index() and from_index();
  • a struct with alloc::String/alloc::Vec<T> fields;
  • to_values() and from_values(&[Value]) -> Option<Self>;
  • SCHEMA_REF and STORAGE_LAYOUT for fixed-storage callers;
  • a generated PredictorSet implementing the public predictor traits.

The wrappers depend only on predsync_core and alloc; they never mention std. from_values returns None for the wrong field count, wrong value kind or an out-of-range enum index. Generation re-runs the message-ID checks, so a colliding schema set fails the build before emitting code.

Generated Python and C wrappers follow the same schema. Python emits enum and message classes with to_values/from_values; C emits fixed-capacity structs plus *_pack and *_unpack helpers that take caller-owned scratch buffers. The schema_build command writes Rust modules only; use generate_python or generate_c from predsync_compiler for the other languages.

The working example is examples/generated_example/; its tests exercise both the alloc API and borrowed fixed storage.