Skip to content

Rust target

Use the Rust target for Rust consumers that want serde-oriented generated model structures.

emit-targets:
- type: Rust
output-dir: "generated/rust"
test-dir: "generated/rust/tests"
import-path: "fixtures::model"
enum-parsing: "case-insensitive"
protocol-scaffolds: "compile-only"
native-serialization: "serde"
Option Why it matters
output-dir Where generated Rust source is written.
test-dir Where generated Rust tests are written.
import-path Import path used by generated tests.
enum-parsing Controls whether enum parsing is case-sensitive or case-insensitive.
protocol-scaffolds Emits compile-only test scaffolds for generated protocols.
native-serialization "serde" emits feature-gated Serialize/Deserialize impls; "none" opts out. Omission keeps the default cfg-gated serde surface for compatibility.

Generated Rust surfaces include serde-oriented model structures, from_json/to_json and from_yaml/to_yaml helpers, a fallible try_load_from_value for already-parsed values, provider wire mapping, and optional case-insensitive enum parsing.

use fixtures::model::{FixtureRoot, LoadContext, SaveContext};
let root = FixtureRoot::from_json(json_text, &LoadContext::default())?;
let saved = root.to_json(&SaveContext::default())?;

The from_yaml and to_yaml helpers provide the same round-trip against YAML. JSON and YAML helpers return Result, so callers propagate serde errors with ?.

When you already hold a parsed serde_json::Value, use try_load_from_value to validate and load without re-serializing. It is the fallible companion to load_from_value: it applies the same validation policy as from_json and returns Result<Self, serde_json::Error>, so a missing required field surfaces an Err instead of panicking.

let root = FixtureRoot::try_load_from_value(&value, &LoadContext::default())?;

Choose enum-parsing deliberately. Default behavior is case-sensitive unless configured otherwise.

native-serialization: "serde" emits #[cfg(feature = "serde")] implementations for generated models and polymorphic kind enums. Those impls delegate through Typra’s canonical load_from_value and to_value paths instead of field-by-field derives, so serde interop preserves discriminator dispatch, field names, optional omission, typed records, and open-discriminator fallback payloads.