-module(ids@base32). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([encode/2, decode/2]). -spec encode_bytes(bitstring(), list({integer(), binary()})) -> binary(). encode_bytes(Binary, Alphabet) -> case Binary of <> -> _pipe = Alphabet, _pipe@1 = gleam@list:key_find(_pipe, Index), _pipe@2 = gleam@result:unwrap(_pipe@1, <<"0"/utf8>>), gleam@string:append(_pipe@2, encode_bytes(Rest, Alphabet)); _ -> <<""/utf8>> end. -spec encode(bitstring(), binary()) -> binary(). encode(Bytes, Alphabet) -> Alphabet_with_index = begin _pipe = Alphabet, _pipe@1 = gleam@string:to_graphemes(_pipe), gleam@list:index_map(_pipe@1, fun(X, I) -> {I, X} end) end, encode_bytes(<<0:2, Bytes/bitstring>>, Alphabet_with_index). -spec decode(binary(), binary()) -> {ok, bitstring()} | {error, nil}. decode(Binary, Alphabet) -> Alphabet_with_index = begin _pipe = Alphabet, _pipe@1 = gleam@string:to_graphemes(_pipe), gleam@list:index_map(_pipe@1, fun(X, I) -> {X, I} end) end, Bits = begin _pipe@2 = Binary, _pipe@3 = gleam@string:to_graphemes(_pipe@2), gleam@list:fold( _pipe@3, <<>>, fun(Acc, C) -> Index = begin _pipe@4 = Alphabet_with_index, _pipe@5 = gleam@list:key_find(_pipe@4, C), gleam@result:unwrap(_pipe@5, 0) end, <> end ) end, case Bits of <<0:2, Res/bitstring>> -> {ok, Res}; _ -> {error, nil} end.