-module(trove@codec). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/trove/codec.gleam"). -export([int/0, bit_array/0, string/0]). -export_type([codec/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( " Codecs for serializing keys and values to and from `BitArray`.\n" "\n" " A `Codec(a)` pairs an encode function with a decode function. Built-in\n" " codecs cover strings, integers, and raw bytes. For custom types, construct\n" " a `Codec` directly with your own encode/decode functions.\n" "\n" " Custom codecs must satisfy `decode(encode(v)) == Ok(v)` and produce\n" " deterministic output (same value always encodes to the same bytes).\n" ). -type codec(HBO) :: {codec, fun((HBO) -> bitstring()), fun((bitstring()) -> {ok, HBO} | {error, nil})}. -file("src/trove/codec.gleam", 57). ?DOC( " 64-bit big-endian signed integer codec (two's complement).\n" " Values outside the signed 64-bit range (`-2^63` to `2^63 - 1`) silently\n" " wrap on encode.\n" "\n" " **Note:** This means `decode(encode(v))` may not equal `Ok(v)` for values\n" " outside the signed 64-bit range. If you need arbitrary-precision integers,\n" " provide a custom codec.\n" "\n" " ```gleam\n" " let c = codec.int()\n" " let bits = c.encode(42)\n" " let assert Ok(42) = c.decode(bits)\n" " ```\n" ). -spec int() -> codec(integer()). int() -> {codec, fun(N) -> <> end, fun(Bits) -> case Bits of <> -> {ok, N@1}; _ -> {error, nil} end end}. -file("src/trove/codec.gleam", 73). ?DOC( " Identity codec for raw bytes. Encodes and decodes as-is.\n" "\n" " ```gleam\n" " let c = codec.bit_array()\n" " let bits = c.encode(<<1, 2, 3>>)\n" " let assert Ok(<<1, 2, 3>>) = c.decode(bits)\n" " ```\n" ). -spec bit_array() -> codec(bitstring()). bit_array() -> {codec, fun gleam@function:identity/1, fun(Field@0) -> {ok, Field@0} end}. -file("src/trove/codec.gleam", 40). ?DOC( " UTF-8 string codec.\n" "\n" " ```gleam\n" " let c = codec.string()\n" " let bits = c.encode(\"hello\")\n" " let assert Ok(\"hello\") = c.decode(bits)\n" " ```\n" ). -spec string() -> codec(binary()). string() -> {codec, fun gleam_stdlib:identity/1, fun gleam@bit_array:to_string/1}.