thrifty/reader

Values

pub fn from_bit_array(data: BitArray) -> types.Reader

Helpers for reading Compact Protocol values with an immutable Reader.

Public API contract (high level):

  • from_bit_array/1: create a Reader positioned at the beginning of data.
  • read_i8/read_i16/read_i32/read_i64: read the corresponding signed integer and return Ok(#(value, reader)) or an Error(DecodeError).
  • read_double: read IEEE-754 little-endian double returning Ok(#(float, reader)).
  • read_binary/read_string: read a length-prefixed binary (varint length) and return Ok(#(bytes, reader)) or Ok(#(string, reader)) for strings.
  • read_struct: parse a struct returning the list of field headers and the reader positioned after the struct payload.
  • skip_value: skip a value of the given field type efficiently and return the advanced reader.

All public readers return Result(#(T, types.Reader), types.DecodeError) to make incremental parsing and error handling explicit. Examples and common error cases are documented on specific functions below.

pub fn position(reader: types.Reader) -> Int

Current byte offset inside the reader.

pub fn read_binary(
  reader: types.Reader,
) -> Result(#(BitArray, types.Reader), types.DecodeError)
pub fn read_bool_element(
  reader: types.Reader,
) -> Result(#(Bool, types.Reader), types.DecodeError)

Read a boolean element stored as a single byte (used for boolean values inside containers such as list / set / map values).

Encoding & canonical mapping:

  • Valid element bytes are 1 and 2. This library interprets them as 1 -> True and 2 -> False (matching the inline boolean type nibble mapping used in field headers).

Validation behaviour:

  • types.ReaderOptions.bool_element_policy controls validation. See thrifty/types.gleam for the available BoolElementPolicy values.

This function returns Ok(#(Bool, Reader)) on success, advancing the reader past the element byte, or Error(types.DecodeError) on invalid or truncated input.

pub fn read_double(
  reader: types.Reader,
) -> Result(#(Float, types.Reader), types.DecodeError)
pub fn read_i16(
  reader: types.Reader,
) -> Result(#(Int, types.Reader), types.DecodeError)

Read a zigzag-encoded i16/i32/i64 depending on the helper used. Returns Ok(#(value, reader)) on success or Error(types.DecodeError) on failure.

pub fn read_i32(
  reader: types.Reader,
) -> Result(#(Int, types.Reader), types.DecodeError)
pub fn read_i64(
  reader: types.Reader,
) -> Result(#(Int, types.Reader), types.DecodeError)
pub fn read_i8(
  reader: types.Reader,
) -> Result(#(Int, types.Reader), types.DecodeError)

Read an i8 value.

pub fn read_string(
  reader: types.Reader,
) -> Result(#(String, types.Reader), types.DecodeError)

Read a UTF-8 string. This uses read_binary/1 and then converts to a Gleam String. Returns Error(types.InvalidWireFormat) if bytes are not valid UTF-8. Example:

let r0 = reader.from_bit_array(writer.write_string(“hello”)) let Ok(#(s, r1)) = reader.read_string(r0) s |> should.equal(“hello”)

pub fn read_struct(
  reader: types.Reader,
) -> Result(
  #(List(types.FieldHeader), types.Reader),
  types.DecodeError,
)
pub fn read_varint(
  reader: types.Reader,
) -> Result(#(Int, types.Reader), types.DecodeError)
pub fn skip_value(
  reader: types.Reader,
  field_type: types.FieldType,
) -> Result(types.Reader, types.DecodeError)

Skip a value of the given field_type. Useful when parsing structs and encountering unknown/ignored fields. This function enforces depth/size limits configured in ReaderOptions and returns the advanced reader or an error. Example:

let r0 = reader.from_bit_array(some_encoded_list) let Ok(r1) = reader.skip_value(r0, types.List)

pub fn with_options(
  data: BitArray,
  options: types.ReaderOptions,
) -> types.Reader
Search Document