Current section

Files

Jump to
gleam_bson src bson@object_id.erl
Raw

src/bson@object_id.erl

-module(bson@object_id).
-compile(no_auto_import).
-export([to_bit_string/1, to_string/1, from_string/1, from_bit_string/1]).
-export_type([object_id/0]).
-opaque object_id() :: {object_id, bitstring()}.
-spec to_bit_string(object_id()) -> bitstring().
to_bit_string(Id) ->
case Id of
{object_id, Value} ->
Value
end.
-spec to_string(object_id()) -> binary().
to_string(Id) ->
case Id of
{object_id, Value} ->
_pipe = Value,
to_string_internal(_pipe, <<""/utf8>>)
end.
-spec from_string(binary()) -> {ok, object_id()} | {error, nil}.
from_string(Id) ->
case begin
_pipe = Id,
gleam@string:length(_pipe)
end
=:= 24 of
true ->
Codes = begin
_pipe@1 = Id,
_pipe@2 = gleam@string:to_graphemes(_pipe@1),
_pipe@4 = gleam@list:map(
_pipe@2,
fun(Char) ->
<<Code>> = begin
_pipe@3 = Char,
gleam@bit_string:from_string(_pipe@3)
end,
Code
end
),
gleam@list:map(
_pipe@4,
fun(Code@1) ->
_pipe@5 = Code@1,
to_digit(_pipe@5)
end
)
end,
case begin
_pipe@6 = Codes,
gleam@list:any(
_pipe@6,
fun(Item) -> gleam@result:is_error(Item) end
)
end of
false ->
Value = begin
_pipe@7 = Codes,
_pipe@8 = gleam@list:map(
_pipe@7,
fun(Item@1) ->
{ok, Digit@1} = case Item@1 of
{ok, Digit} -> {ok, Digit};
_try ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try,
module => <<"bson/object_id"/utf8>>,
function => <<"from_string"/utf8>>,
line => 47})
end,
Digit@1
end
),
_pipe@9 = gleam@list:sized_chunk(_pipe@8, 2),
_pipe@10 = gleam@list:map(
_pipe@9,
fun(Pair) ->
[A, B] = Pair,
<<A:4, B:4>>
end
),
gleam@bit_string:concat(_pipe@10)
end,
{ok, {object_id, Value}};
true ->
{error, nil}
end;
false ->
{error, nil}
end.
-spec from_bit_string(bitstring()) -> {ok, object_id()} | {error, nil}.
from_bit_string(Id) ->
case gleam@bit_string:byte_size(Id) =:= 12 of
true ->
{ok, {object_id, Id}};
false ->
{error, nil}
end.
-spec to_string_internal(bitstring(), binary()) -> binary().
to_string_internal(Remaining, Storage) ->
<<Low:4, High:4, Remaining@1/binary>> = Remaining,
New_storage = begin
_pipe = Storage,
_pipe@1 = gleam@string:append(_pipe, to_char(Low)),
gleam@string:append(_pipe@1, to_char(High))
end,
case gleam@bit_string:byte_size(Remaining@1) =:= 0 of
true ->
New_storage;
false ->
to_string_internal(Remaining@1, New_storage)
end.
-spec to_digit(integer()) -> {ok, integer()} | {error, nil}.
to_digit(Char) ->
case (Char >= 48) andalso (Char =< 57) of
true ->
{ok, Char - 48};
false ->
case (Char >= 65) andalso (Char =< 70) of
true ->
{ok, Char - 55};
false ->
case (Char >= 97) andalso (Char =< 102) of
true ->
{ok, Char - 87};
false ->
{error, nil}
end
end
end.
-spec to_char(integer()) -> binary().
to_char(Digit) ->
Ch = case Digit < 10 of
true ->
Digit + 48;
false ->
Digit + 87
end,
{ok, Digit@2} = case gleam@bit_string:to_string(<<Ch>>) of
{ok, Digit@1} -> {ok, Digit@1};
_try ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try,
module => <<"bson/object_id"/utf8>>,
function => <<"to_char"/utf8>>,
line => 106})
end,
Digit@2.