glepack

This Module provides functions to encode and decode MessagePack data

Functions

pub fn pack(value: Value) -> Result(BitArray, Nil)

Convert a Gleam value to MessagePack format.

This function takes any value that can be represented by data.Value and converts it to MessagePack binary format.

Examples

pack(data.Integer(42))
// -> Ok(<<42>>)

let map = dict.new() |> dict.insert(data.String("key"), data.String("value"))
pack(data.Map(map))
// -> Ok binary data representing the map
pub fn pack_strict(value: Value) -> BitArray

Convert a Gleam value to MessagePack format, raising an error if conversion fails.

This is similar to pack but returns the binary data directly instead of a Result. If encoding fails, this function will panic.

Examples

pack_strict(data.Integer(42))
// -> <<42>>
pub fn unpack(
  input: BitArray,
) -> Result(#(Value, BitArray), DecodeError)

Decode a MessagePack binary into a Gleam value.

This function takes MessagePack formatted binary data and converts it to a data.Value type. It also returns the unconsumed portion of the input if there are remaining bytes after decoding.

Examples

unpack(<<163, 102, 111, 111>>)
// -> Ok(#(data.String("foo"), <<>>))
pub fn unpack_exact(
  input: BitArray,
) -> Result(Value, DecodeError)

Decode a complete MessagePack binary into a Gleam value.

Unlike unpack, this function expects the input to contain exactly one MessagePack value with no trailing data. If there are remaining bytes after decoding, an error is returned.

Examples

unpack_exact(<<163, 102, 111, 111>>)
// -> Ok(data.String("foo"))

unpack_exact(<<163, 102, 111, 111, 192>>)
// -> Error(Nil) because there is a remaining byte
pub fn unpack_exact_strict(input: BitArray) -> Value

Decode a complete MessagePack binary, raising an error if decoding fails or if there is trailing data.

This is similar to unpack_exact but returns the value directly instead of a Result. If decoding fails or there is trailing data, this function will panic.

Examples

unpack_exact_strict(<<163, 102, 111, 111>>)
// -> data.String("foo")
pub fn unpack_strict(input: BitArray) -> #(Value, BitArray)

Decode a MessagePack binary into a Gleam value, raising an error if decoding fails.

This is similar to unpack but returns the value directly instead of a Result. If decoding fails, this function will panic.

Examples

unpack_strict(<<163, 102, 111, 111>>)
// -> #(data.String("foo"), <<>>)
Search Document