Packages

MessagePack (msgpack) implementation for Gleam

Retired package: Release invalid - Not stable

Current section

Files

Jump to
glepack src glepack.erl
Raw

src/glepack.erl

-module(glepack).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([pack/1, pack_strict/1, unpack/1, unpack_strict/1, unpack_exact/1, unpack_exact_strict/1]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(" This Module provides functions to encode and decode MessagePack data\n").
-file("src/glepack.gleam", 25).
?DOC(
" Convert a Gleam value to MessagePack format.\n"
" \n"
" This function takes any value that can be represented by `data.Value` and \n"
" converts it to MessagePack binary format.\n"
" \n"
" ## Examples\n"
" \n"
" ```gleam\n"
" pack(data.Integer(42))\n"
" // -> Ok(<<42>>)\n"
" \n"
" let map = dict.new() |> dict.insert(data.String(\"key\"), data.String(\"value\"))\n"
" pack(data.Map(map))\n"
" // -> Ok binary data representing the map\n"
" ```\n"
).
-spec pack(glepack@data:value()) -> {ok, bitstring()} | {error, nil}.
pack(Value) ->
glepack@encode:value(Value).
-file("src/glepack.gleam", 40).
?DOC(
" Convert a Gleam value to MessagePack format, raising an error if conversion fails.\n"
" \n"
" This is similar to `pack` but returns the binary data directly instead of a Result.\n"
" If encoding fails, this function will panic.\n"
" \n"
" ## Examples\n"
" \n"
" ```gleam\n"
" pack_strict(data.Integer(42))\n"
" // -> <<42>>\n"
" ```\n"
).
-spec pack_strict(glepack@data:value()) -> bitstring().
pack_strict(Value) ->
case pack(Value) of
{ok, Binary} ->
Binary;
{error, _} ->
erlang:error(#{gleam_error => panic,
message => <<"Failed to encode value to MessagePack format"/utf8>>,
module => <<"glepack"/utf8>>,
function => <<"pack_strict"/utf8>>,
line => 43})
end.
-file("src/glepack.gleam", 59).
?DOC(
" Decode a MessagePack binary into a Gleam value.\n"
" \n"
" This function takes MessagePack formatted binary data and converts it\n"
" to a `data.Value` type. It also returns the unconsumed portion of the input\n"
" if there are remaining bytes after decoding.\n"
" \n"
" ## Examples\n"
" \n"
" ```gleam\n"
" unpack(<<163, 102, 111, 111>>)\n"
" // -> Ok(#(data.String(\"foo\"), <<>>))\n"
" ```\n"
).
-spec unpack(bitstring()) -> {ok, {glepack@data:value(), bitstring()}} |
{error, glepack@error:decode_error()}.
unpack(Input) ->
glepack@decode:value(Input).
-file("src/glepack.gleam", 76).
?DOC(
" Decode a MessagePack binary into a Gleam value, raising an error if decoding fails.\n"
" \n"
" This is similar to `unpack` but returns the value directly instead of a Result.\n"
" If decoding fails, this function will panic.\n"
" \n"
" ## Examples\n"
" \n"
" ```gleam\n"
" unpack_strict(<<163, 102, 111, 111>>)\n"
" // -> #(data.String(\"foo\"), <<>>)\n"
" ```\n"
).
-spec unpack_strict(bitstring()) -> {glepack@data:value(), bitstring()}.
unpack_strict(Input) ->
case unpack(Input) of
{ok, Result} ->
Result;
{error, _} ->
erlang:error(#{gleam_error => panic,
message => <<"Failed to decode MessagePack data"/utf8>>,
module => <<"glepack"/utf8>>,
function => <<"unpack_strict"/utf8>>,
line => 79})
end.
-file("src/glepack.gleam", 98).
?DOC(
" Decode a complete MessagePack binary into a Gleam value.\n"
" \n"
" Unlike `unpack`, this function expects the input to contain exactly one\n"
" MessagePack value with no trailing data. If there are remaining bytes after\n"
" decoding, an error is returned.\n"
" \n"
" ## Examples\n"
" \n"
" ```gleam\n"
" unpack_exact(<<163, 102, 111, 111>>)\n"
" // -> Ok(data.String(\"foo\"))\n"
" \n"
" unpack_exact(<<163, 102, 111, 111, 192>>)\n"
" // -> Error(Nil) because there is a remaining byte\n"
" ```\n"
).
-spec unpack_exact(bitstring()) -> {ok, glepack@data:value()} |
{error, glepack@error:decode_error()}.
unpack_exact(Input) ->
case unpack(Input) of
{ok, {Value, Rest}} ->
case erlang:byte_size(Rest) of
0 ->
{ok, Value};
_ ->
{error, incomplete_data}
end;
{error, E} ->
{error, E}
end.
-file("src/glepack.gleam", 121).
?DOC(
" Decode a complete MessagePack binary, raising an error if decoding fails or if \n"
" there is trailing data.\n"
" \n"
" This is similar to `unpack_exact` but returns the value directly instead of a Result.\n"
" If decoding fails or there is trailing data, this function will panic.\n"
" \n"
" ## Examples\n"
" \n"
" ```gleam\n"
" unpack_exact_strict(<<163, 102, 111, 111>>)\n"
" // -> data.String(\"foo\")\n"
" ```\n"
).
-spec unpack_exact_strict(bitstring()) -> glepack@data:value().
unpack_exact_strict(Input) ->
case unpack_exact(Input) of
{ok, Value} ->
Value;
{error, _} ->
erlang:error(#{gleam_error => panic,
message => <<"Failed to decode MessagePack data or found trailing bytes"/utf8>>,
module => <<"glepack"/utf8>>,
function => <<"unpack_exact_strict"/utf8>>,
line => 125})
end.