KiwiCodec.RustlerGenerator (kiwi_codec v0.1.1)

Copy Markdown View Source

Generates Rustler decoder code from Kiwi schemas for RustQ manifests.

This is an experimental bridge for optional native backends. It returns RustQ splice replacements for schema-dependent decoder functions and NIF entrypoints; rustq.exs owns rendering and writing generated files.

Generated Rust expects the template to provide these imports and helpers:

  • rustler::{Binary, Encoder, Env, Error, NifResult, Term}
  • rustler::types::atom::Atom
  • std::sync::OnceLock
  • a Decoder<'a> type with Kiwi primitive reader methods
  • the __rq_rustler_helpers!(); splice emitted by splices/2

The helper splice provides cached atoms, cached struct keys, default struct values, and raw NIF_TERM struct map construction. See the README for a minimal RustQ template skeleton.

Summary

Functions

Returns RustQ splice replacements for a schema.

Types

entrypoint()

@type entrypoint() :: {atom() | String.t(), String.t()}

Functions

splices(schema, opts)

@spec splices(
  KiwiCodec.Schema.t(),
  keyword()
) :: [{atom(), [RustQ.Rust.Fragment.t()]}]

Returns RustQ splice replacements for a schema.

Use this from rustq.exs with render/2. The template must contain __rq_rustler_helpers!();, __rq_definitions!();, and, when NIF entrypoints are requested, __rq_entrypoints!(); splice anchors:

generate :native_decoders, "native/my_nif/src/generated.rs" do
  schema = KiwiCodec.parse_schema!(File.read!("priv/schema.kiwi"))

  render File.read!("native/my_nif/src/generated.template.rs"),
    filename: "native/my_nif/src/generated.template.rs",
    splice: KiwiCodec.RustlerGenerator.splices(schema,
      definitions: ["Node"],
      entrypoints: [decode_node: "Node"],
      module_prefix: "Example.Schema"
    )
end