Skip to main content

Module codegen

Module codegen 

Source
Expand description

Rust code generation: compile a Schema into typed readers and writers.

The generated reader is the novel part — an identity fast path: every message carries its writer-schema hash, so the generated read() compares it against the generation-time SCHEMA_ID constant. On a match (the overwhelmingly common case) every getter is a bounds-checked load at a constant offset computed here from the deterministic layout — the same code shape flatc emits. On a mismatch it transparently falls back to the resolver-driven plan path, keeping full schema evolution. Compile the happy path, interpret the evolution path.

The generated writer is a direct single-pass encoder over typed …Args structs — no dynamic Value tree.

Codegen v1 supports: all scalars, strings, bytes, enums (as raw u32), nested structs, list<scalar>, list<string>, list<struct>. Not yet: list<bytes>, list<enum>, nested lists (use the dynamic API for those).

Functions§

generate_cpp
Compile a schema to a single self-contained C++ header of structs with to_verit / from_verit, over the header-only veritate.hpp port. Like the other emitters it maps each struct to/from the dynamic Value/reader form and supports every schema shape (all struct modes, all list element types). Fields are std::optional<T> (absent = std::nullopt). The embedded canonical schema + 128-bit id are the cross-language contract.
generate_go
Compile a schema to a Go package of typed structs with ToVerit / <Type>FromVerit over the stdlib-only veritate port. A small reflect runtime maps each struct to/from the port’s Struct value form, so every schema the dynamic reader supports is supported here too. Optional fields are pointers (nil = absent). The embedded canonical schema + 128-bit id are the cross-language contract.
generate_python
Compile a schema to a Python module of @dataclasses with to_verit / from_verit, over the pure-stdlib veritate port. Unlike the Rust emitter, this supports every schema the dynamic reader does (all struct modes, all list element types) — the dataclasses map to/from the {field_id: value} form the port already speaks. The embedded canonical schema + 128-bit id are the cross-language contract.
generate_rust
Generate a self-contained Rust module (as source text) for schema. Include it with mod foo; / #[path = "..."] mod foo; — it depends only on the verit crate’s public API.
generate_ts
Compile a schema to a TypeScript module of classes with toVerit / fromVerit over the pure-veritate.ts port. Like the Python emitter it supports every schema the dynamic reader does; classes map to/from the port’s st([[id, value]]) form. The embedded canonical schema + 128-bit id are the cross-language contract. Runs under Node’s type stripping (the port is imported as ./veritate.ts), so no separate compile step is needed.