Skip to main content

Module codegen

Module codegen 

Source
Expand description

Codec generation (Generator). Rust code generation from a resolved crate::Schema.

Primary type: Generator. Configure with crate::GenerationConfig, call Generator::generate or Generator::generate_multi, write GeneratedModule::source to OUT_DIR, then include! it.

§Pipeline

  1. Partition IR tokens into enums, sets, composites, messages.
  2. Emit type definitions.
  3. Per message: decoder flyweight, encoder + type-state tails, optional domain DTO.
  4. Emit AnyMessage / FrameCursor when multiple templates exist.
  5. Format with prettyplease.

§Example

use ergo_sbe::{parse, Generator, GenerationConfig, Schema};

let ir = parse(r#"<?xml version="1.0"?>
<messageSchema package="ex" id="1" version="0" byteOrder="littleEndian">
  <types>
    <composite name="messageHeader">
      <type name="blockLength" primitiveType="uint16"/>
      <type name="templateId" primitiveType="uint16"/>
      <type name="schemaId" primitiveType="uint16"/>
      <type name="version" primitiveType="uint16"/>
    </composite>
  </types>
  <message name="Ping" id="1">
    <field name="seq" id="1" type="uint32" offset="0"/>
  </message>
</messageSchema>"#).unwrap();
let schema = Schema::from_ir(ir);
let set = Generator::new(GenerationConfig::new("ping"))
    .generate(&schema)
    .unwrap();
let src = &set.modules().next().unwrap().source;
assert!(src.contains("PingDecoder"));
assert!(src.contains("PingEncoder"));

See the crate root for how to use generated codecs (encode/decode, conversion styles, domain objects, metadata).

Structs§

GeneratedModule
One generated Rust source file.
GeneratedModuleSet
Set of modules from Generator::generate or Generator::generate_multi.
Generator
SBE → Rust codec generator.

Enums§

GenerateError
Errors returned by Generator::generate when the configuration is invalid for the given schema.