gargamelle/codec_record

Shared static field declarations for record definitions and encoders. Produces the existing encoding.Codec, with a structural Contract. Each field consumes one constructor argument and retains a getter from the whole record.

Child codecs must have no semantic requirements, including optional and defaulted children. Requirements are rejected, never silently discarded. Attach semantic requirements to the completed record’s Contract separately. Only completed definitions and encoders are supported, not recursion plans.

Operations preserve the first construction error and skip subsequent builder work. Gleam still evaluates arguments eagerly after an earlier error. Construction runs no constructors, getters or decoding conversions.

Types

Construction failures are distinct from input and runtime encoding failures. Field failures retain the declared name and original error without rewriting it.

pub type BuildError {
  Definition(cause: gargamelle.DefinitionError)
  FieldDefinition(
    name: String,
    cause: gargamelle.DefinitionError,
  )
  FieldEncoding(name: String, cause: encoding.EncodingError)
  SemanticRequirements(name: String, descriptions: List(String))
}

Constructors

Fields

opaque

A partially applied decoding constructor and an encoder for the whole record.

pub opaque type Fields(remaining, whole)

Values

pub fn defaulted(
  fields: Result(Fields(fn(a) -> b, whole), BuildError),
  name: String,
  child: encoding.Codec(a),
  default: gargamelle.Json,
  get: fn(whole) -> a,
) -> Result(Fields(b, whole), BuildError)

Validate the default during construction without decoding. Absence decodes that default; present invalid input fails. Encoding always writes the getter’s actual value, including when equal to the default. Child semantic requirements are rejected even when the default satisfies the structural definition.

pub fn finish(
  fields: Result(Fields(a, a), BuildError),
  policy: gargamelle.RecordPolicy,
) -> Result(encoding.Codec(a), BuildError)

Finish when the constructor output and encoder input have the same type. Strict rejects unknown input fields. Open accepts and discards them during decoding; encoding writes declared fields only. Neither policy proves that constructors and getters are inverses.

pub fn optional(
  fields: Result(
    Fields(fn(option.Option(a)) -> b, whole),
    BuildError,
  ),
  name: String,
  child: encoding.Codec(a),
  get: fn(whole) -> option.Option(a),
) -> Result(Fields(b, whole), BuildError)

Absence decodes as None. On encoding None omits the key; Some uses the child encoder. JSON null is distinct from absence and must be supported by the child. Child semantic requirements are rejected even if the field could be absent.

pub fn required(
  fields: Result(Fields(fn(a) -> b, whole), BuildError),
  name: String,
  child: encoding.Codec(a),
  get: fn(whole) -> a,
) -> Result(Fields(b, whole), BuildError)

Declare a required field once for decoding, schemas and encoding. Reject child semantic requirements before adding either field declaration.

pub fn start(
  builder: Result(
    gargamelle.Record(a, Nil),
    gargamelle.DefinitionError,
  ),
) -> Result(Fields(a, whole), BuildError)

Start from record.start or an ordinary-constructor adapter such as start2. An existing definition-construction error is preserved as Definition(error).

✨ Search Document