aiken_flat/encoder

Types

Opaque type for encoding given values and collecting them in a buffer.

Function type shared by all encoders.

Alias

EncoderFn<a> = fn(Encoder, a) -> Encoder

Constants

identity_encoder_fn_selector: fn(Int) -> EncoderFn<Data>

Identity encoder selector against field indices (helpful for defining empty record type encoders).

identity_encoder_fn_selector_factory: fn(Int) -> fn(Int) -> EncoderFn<Data>

Identity encoder selector factory against tag indices (helpful for defining “empty sum type” encoders, i.e. sum types that none of their constructors carry values).

new: Encoder

Initiate an encoder.

Functions

Basics

contramap(encoder_fn: EncoderFn<a>, mapping_fn: fn(b) -> a) -> EncoderFn<b>

Runners

unwrap(self: Encoder) -> ByteArray

Returns the encoded bytes so far, ignoring the current byte.

Essential Encoders

unsafe_u8(self: Encoder, u8: ByteArray) -> Encoder

Assumes given ByteArray is a single byte.

bool(self: Encoder, b: Bool) -> Encoder

bytes(self: Encoder, arr: ByteArray) -> Encoder

byte_array(self: Encoder, arr: ByteArray) -> Encoder

integer(self: Encoder, x: Int) -> Encoder

word(self: Encoder, x: Int) -> Encoder

encode_list_with(
  self: Encoder,
  list: List<a>,
  with: fn(Encoder, a) -> Encoder,
) -> Encoder

bits(self: Encoder, bits: Bits) -> Encoder

filler(self: Encoder) -> Encoder

Custom Type Encoders

make_rec_2(
  f0_encoder: EncoderFn<Data>,
  f1_encoder: EncoderFn<Data>,
) -> EncoderFn<List<Data>>

Helper for making encoders for record types with 2 fields. More performant than using make_rec.

make_rec_3(
  f0_encoder: EncoderFn<Data>,
  f1_encoder: EncoderFn<Data>,
  f2_encoder: EncoderFn<Data>,
) -> EncoderFn<List<Data>>

Helper for making encoders for record types with 3 fields. More performant than using make_rec.

make_rec_4(
  f0_encoder: EncoderFn<Data>,
  f1_encoder: EncoderFn<Data>,
  f2_encoder: EncoderFn<Data>,
  f3_encoder: EncoderFn<Data>,
) -> EncoderFn<List<Data>>

Helper for making encoders for record types with 4 fields. More performant than using make_rec.

make_rec(
  encoder_fn_selector: fn(Int) -> EncoderFn<Data>,
) -> EncoderFn<List<Data>>

Helper for creating record type encoders. The resulting encoder traverses a list of Data values and uses their indices to find their corresponding encoders from the provided selector function.

make_sum_2(
  c0_encoders: fn(Int) -> EncoderFn<Data>,
  c1_encoders: fn(Int) -> EncoderFn<Data>,
) -> EncoderFn<Data>

Helper for making encoders for sum types with 2 data constructors, and arbitrary fields in each. More performant than using make_sum. The Data in return type is assumed to be a Constr.

make_sum_3(
  c0_encoders: fn(Int) -> EncoderFn<Data>,
  c1_encoders: fn(Int) -> EncoderFn<Data>,
  c2_encoders: fn(Int) -> EncoderFn<Data>,
) -> EncoderFn<Data>

Helper for making encoders for sum types with 3 data constructors, and arbitrary fields in each. More performant than using make_sum. The Data in return type is assumed to be a Constr.

make_sum_4(
  c0_encoders: fn(Int) -> EncoderFn<Data>,
  c1_encoders: fn(Int) -> EncoderFn<Data>,
  c2_encoders: fn(Int) -> EncoderFn<Data>,
  c3_encoders: fn(Int) -> EncoderFn<Data>,
) -> EncoderFn<Data>

Helper for making encoders for sum types with 4 data constructors, and arbitrary fields in each. More performant than using make_sum. The Data in return type is assumed to be a Constr.

make_sum(
  constr_count: Int,
  encoder_fn_selector_factory: fn(Int) -> fn(Int) -> EncoderFn<Data>,
) -> EncoderFn<Data>

Helper function for generating encoders for sum types. The factory is expected to be a function that takes an index, which is the index of the constructor in the subject sum type, and returns another function that given the index of a field from the record, returns an encoder function.

An example can be:

pub type Color {
  Abyss
  BlackAndWhite {
    white: Int,
  }
  Colored {
    red: Int,
    green: Int,
    blue: Int,
  }
}

The factory function for this datatype should be:

let color_encoder_factory =
  fn(tag: Int) {
    if tag == 0 {
      identity_encoder_fn_selector
    } else if tag == 1 {
      fn(f_index: Int) {
        if f_index == 0 {
          integer |> contramap(builtin.un_i_data)
        } else {
          fail
        }
      }
    } else if tag == 2 {
      fn(f_index: Int) {
        if f_index <= 2 {
          integer |> contramap(builtin.un_i_data)
        } else {
          fail
        }
      }
    } else {
      fail
    }
  }

In this case, it can be much simpler if we forgo the strictness:

let color_encoder_factory =
  fn(_) {
    fn(_) {
      integer |> contramap(builtin.un_i_data)
    }
  }

And the encoder itself will be:

let color_encoder = make_sum(3, color_encoder_factory)
Search Document