Current section
Files
Jump to
Current section
Files
src/ids@base32.erl
-module(ids@base32).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([encode/2, decode/2]).
-file("/Users/rvcas/thelab/me/ids/src/ids/base32.gleam", 21).
-spec encode_bytes(bitstring(), list({integer(), binary()})) -> binary().
encode_bytes(Binary, Alphabet) ->
case Binary of
<<Index:5/unsigned, Rest/bitstring>> ->
_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.
-file("/Users/rvcas/thelab/me/ids/src/ids/base32.gleam", 10).
-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).
-file("/Users/rvcas/thelab/me/ids/src/ids/base32.gleam", 36).
-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,
<<Acc/bitstring, Index:5>>
end
)
end,
case Bits of
<<0:2, Res/bitstring>> ->
{ok, Res};
_ ->
{error, nil}
end.