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_string/1, to_int_list/1, to_bit_string/1, from_string/1, from_int_list/1, from_bit_string/1]).
-export_type([object_id/0]).
-opaque object_id() :: {object_id, bitstring()}.
-spec to_string(object_id()) -> binary().
to_string(Id) ->
case Id of
{object_id, Value} ->
_pipe = Value,
to_string_internal(_pipe, <<""/utf8>>)
end.
-spec to_int_list(object_id()) -> list(integer()).
to_int_list(Id) ->
case Id of
{object_id, Value} ->
_pipe = Value,
to_int_list_internal(_pipe, [])
end.
-spec to_bit_string(object_id()) -> bitstring().
to_bit_string(Id) ->
case Id of
{object_id, Value} ->
Value
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 ->
case begin
_pipe@1 = Id,
_pipe@2 = gleam@string:to_graphemes(_pipe@1),
gleam@list:try_map(_pipe@2, fun to_digit/1)
end of
{ok, Codes} ->
{ok,
{object_id,
begin
_pipe@3 = Codes,
_pipe@4 = gleam@list:sized_chunk(_pipe@3, 2),
_pipe@5 = gleam@list:map(
_pipe@4,
fun(Pair) ->
[High, Low] = Pair,
<<High:4, Low:4>>
end
),
gleam@bit_string:concat(_pipe@5)
end}};
{error, nil} ->
{error, nil}
end;
false ->
{error, nil}
end.
-spec from_int_list(list(integer())) -> {ok, object_id()} | {error, nil}.
from_int_list(Id) ->
case begin
_pipe = Id,
gleam@list:length(_pipe)
end of
12 ->
case begin
_pipe@1 = Id,
gleam@list:try_fold(
_pipe@1,
<<>>,
fun(Acc, Code) -> case (Code >= 0) andalso (Code =< 255) of
true ->
{ok,
begin
_pipe@2 = Acc,
gleam@bit_string:append(_pipe@2, <<Code>>)
end};
false ->
{error, nil}
end end
)
end of
{ok, Id@1} ->
{ok, {object_id, Id@1}};
{error, nil} ->
{error, nil}
end;
24 ->
case begin
_pipe@3 = Id,
gleam@list:try_map(
_pipe@3,
fun(Code@1) -> case (Code@1 >= 0) andalso (Code@1 =< 15) of
true ->
{ok, Code@1};
false ->
{error, nil}
end end
)
end of
{ok, Codes} ->
{ok,
{object_id,
begin
_pipe@4 = Codes,
_pipe@5 = gleam@list:sized_chunk(_pipe@4, 2),
_pipe@6 = gleam@list:map(
_pipe@5,
fun(Pair) ->
[High, Low] = Pair,
<<Num:8>> = <<High:4, Low:4>>,
Num
end
),
gleam@list:fold(
_pipe@6,
<<>>,
fun(Acc@1, Code@2) ->
_pipe@7 = Acc@1,
gleam@bit_string:append(_pipe@7, <<Code@2>>)
end
)
end}};
{error, nil} ->
{error, nil}
end;
_@1 ->
{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) ->
<<High:4, Low:4, Remaining@1/binary>> = Remaining,
New_storage = begin
_pipe = Storage,
_pipe@1 = gleam@string:append(_pipe, to_char(High)),
gleam@string:append(_pipe@1, to_char(Low))
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_int_list_internal(bitstring(), list(integer())) -> list(integer()).
to_int_list_internal(Remaining, Storage) ->
<<Num:8, Remaining@1/binary>> = Remaining,
New_storage = begin
_pipe = Storage,
gleam@list:append(_pipe, [Num])
end,
case gleam@bit_string:byte_size(Remaining@1) =:= 0 of
true ->
New_storage;
false ->
to_int_list_internal(Remaining@1, New_storage)
end.
-spec to_digit(binary()) -> {ok, integer()} | {error, nil}.
to_digit(Char) ->
<<Code>> = begin
_pipe = Char,
gleam@bit_string:from_string(_pipe)
end,
case Code of
Code@1 when (Code@1 >= 48) andalso (Code@1 =< 57) ->
<<_@1:4, Num:4>> = begin
_pipe@1 = Char,
gleam@bit_string:from_string(_pipe@1)
end,
{ok, Num};
Code@2 when ((Code@2 >= 65) andalso (Code@2 =< 70)) orelse ((Code@2 >= 97) andalso (Code@2 =< 102)) ->
<<_@2:5, Additive:3>> = begin
_pipe@2 = Char,
gleam@bit_string:from_string(_pipe@2)
end,
{ok, 9 + Additive};
_@3 ->
{error, nil}
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 => 171})
end,
Digit@2.