desert-codegen

    Code generator for vigoo/desert: derives BinaryCodec implementations from #desert(binary_codec) annotations

    codegen
    serialization
    moonbit
    Download zip
    Author
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    5 hours ago
    Downloads
    2

    #desert-codegen

    Code generator for vigoo/desert: derives BinarySerializer / BinaryDeserializer implementations for MoonBit structs and enums annotated with #desert(binary_codec).

    #Usage

    From a clone of the desert-moonbit repository (a MoonBit workspace containing this tool):

    moon run codegen -- <absolute path to your package directory>

    The generator scans *.mbt files in the given package, finds annotated types, and writes a desert_derive.mbt file next to them with the codec implementations (formatted with the official MoonBit formatter). It also adds the vigoo/desert import to the package's moon.pkg if missing. Re-run it after changing annotated types; the output is deterministic and idempotent.

    See the main library docs for the full annotation reference: docs/getting_started.md.

    #Supported annotations

    • #desert(binary_codec) — derive a codec for a struct or enum
    • evolution(FieldAdded("f", default), FieldMadeOptional("f"), FieldRemoved("f"), FieldMadeTransient("f")) — schema history for structs and individual enum cases
    • type_id = "pkg.TypeName" — also derive RegisteredValue for the type registry
    • sorted_constructors (enums) — assign constructor ids in lexical order
    • transparent (single-field structs/cases) — encode the inner value directly
    • #transient(expr) on fields — skip on wire, use expr when reading
    • #desert(transient) on enum cases — the case cannot be serialized and consumes no constructor id

    Not supported: container-level evolution on enums (put steps on cases), custom wrappers, tuple structs.

    #Development

    moon check # type check moon test # unit tests for the parser/emitter pipeline moon fmt

    CodegenError

    pub suberror CodegenError {
    CodegenError(String)
    }

    Error raised for invalid derive annotations or inputs.

    CaseIR

    pub(all) struct CaseIR {
    name : String
    arity : Int
    option_flags : Array[Bool]
    steps : Array[EvolutionIR]
    transient : Bool
    transparent : Bool
    }

    DeriveOptions

    pub(all) struct DeriveOptions {
    transparent : Bool
    sorted_constructors : Bool
    type_id : String?
    steps : Array[EvolutionIR]
    }

    DeriveOptions::empty

    EnumIR

    pub(all) struct EnumIR {
    name : String
    cases : Array[CaseIR]
    sorted_constructors : Bool
    type_id : String?
    }

    Internal representation of one derived enum (with per-case data).

    EvolutionIR

    pub(all) enum EvolutionIR {
    FieldAddedIR(String, String)
    FieldMadeOptionalIR(String)
    FieldRemovedIR(String)
    FieldMadeTransientIR(String)
    }

    FieldIR

    pub(all) struct FieldIR {
    name : String
    is_option : Bool
    transient_default : String?
    added_default : String?
    }

    StructIR

    pub(all) struct StructIR {
    name : String
    fields : Array[FieldIR]
    steps : Array[EvolutionIR]
    transparent : Bool
    type_id : String?
    }

    Internal representation of one derived struct.

    emit_enum

    fn emit_enum(en : EnumIR) -> String

    emit_struct

    fn emit_struct(st : StructIR) -> String

    is_binary_codec_attr

    fn is_binary_codec_attr(raw : String) -> Bool

    True when the raw attribute text is our derive marker.

    lowercase_first

    fn lowercase_first(s : String) -> String

    Top-level let names must start lowercase.

    metadata_ref

    fn metadata_ref(type_name : String) -> String

    parse_between_parens_public

    fn parse_between_parens_public(s : String) -> String raise CodegenError

    Public helper used by the field-attribute reader.

    parse_derive_args

    fn parse_derive_args(body : String) -> DeriveOptions raise CodegenError

    Parse the argument list body (the text between the outer parentheses).

    scan_impls

    Walk one parsed source file, returning the derived structs and enums.

    split_top_level

    fn split_top_level(s : String) -> Array[String]

    Split on top-level commas (outside parentheses and string literals).