-module(bison@encoder). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([encode/1]). -spec null() -> {bison@kind:kind(), bitstring()}. null() -> {{kind, <<16#0A>>}, <<>>}. -spec nan() -> {bison@kind:kind(), bitstring()}. nan() -> {{kind, <<16#01>>}, <<"NaN"/utf8>>}. -spec infinity() -> {bison@kind:kind(), bitstring()}. infinity() -> {{kind, <<16#01>>}, <<"Infinity"/utf8>>}. -spec negative_infinity() -> {bison@kind:kind(), bitstring()}. negative_infinity() -> {{kind, <<16#01>>}, <<"-Infinity"/utf8>>}. -spec min() -> {bison@kind:kind(), bitstring()}. min() -> {{kind, <<16#FF>>}, <<>>}. -spec max() -> {bison@kind:kind(), bitstring()}. max() -> {{kind, <<16#7F>>}, <<>>}. -spec js(binary()) -> {bison@kind:kind(), bitstring()}. js(Value) -> Length = erlang:byte_size(<>) + 1, {{kind, <<16#0D>>}, <>}. -spec string(binary()) -> {bison@kind:kind(), bitstring()}. string(Value) -> Length = erlang:byte_size(<>) + 1, {{kind, <<16#02>>}, <>}. -spec double(float()) -> {bison@kind:kind(), bitstring()}. double(Value) -> {{kind, <<16#01>>}, <>}. -spec boolean(boolean()) -> {bison@kind:kind(), bitstring()}. boolean(Value) -> case Value of true -> {{kind, <<16#08>>}, <<1>>}; false -> {{kind, <<16#08>>}, <<0>>} end. -spec int32(integer()) -> {bison@kind:kind(), bitstring()}. int32(Value) -> {{kind, <<16#10>>}, <>}. -spec int64(integer()) -> {bison@kind:kind(), bitstring()}. int64(Value) -> {{kind, <<16#12>>}, <>}. -spec datetime(birl:time()) -> {bison@kind:kind(), bitstring()}. datetime(Value) -> {duration, Value@1} = birl:difference(Value, {time, 0, 0, none, none}), Value@2 = Value@1 div 1000, {{kind, <<16#09>>}, <>}. -spec object_id(bison@object_id:object_id()) -> {bison@kind:kind(), bitstring()}. object_id(Value) -> {{kind, <<16#07>>}, bison@object_id:to_bit_array(Value)}. -spec timestamp(integer(), integer()) -> {bison@kind:kind(), bitstring()}. timestamp(Stamp, Counter) -> {{kind, <<16#11>>}, <>}. -spec md5(bison@md5:md5()) -> {bison@kind:kind(), bitstring()}. md5(Value) -> Value@1 = bison@md5:to_bit_array(Value), Length = erlang:byte_size(Value@1), {{kind, <<16#05>>}, begin _pipe = [<>, erlang:element(2, {sub_kind, <<16#5>>}), Value@1], gleam_stdlib:bit_array_concat(_pipe) end}. -spec uuid(bison@uuid:uuid()) -> {bison@kind:kind(), bitstring()}. uuid(Value) -> Value@1 = bison@uuid:to_bit_array(Value), Length = erlang:byte_size(Value@1), {{kind, <<16#05>>}, begin _pipe = [<>, erlang:element(2, {sub_kind, <<16#4>>}), Value@1], gleam_stdlib:bit_array_concat(_pipe) end}. -spec custom(bison@custom:custom()) -> {bison@kind:kind(), bitstring()}. custom(Value) -> {Code, Value@1} = bison@custom:to_bit_array_with_code(Value), Length = erlang:byte_size(Value@1), {{kind, <<16#05>>}, begin _pipe = [<>, <>, Value@1], gleam_stdlib:bit_array_concat(_pipe) end}. -spec generic(bison@generic:generic()) -> {bison@kind:kind(), bitstring()}. generic(Value) -> Value@1 = bison@generic:to_bit_array(Value), Length = erlang:byte_size(Value@1), {{kind, <<16#05>>}, begin _pipe = [<>, erlang:element(2, {sub_kind, <<16#0>>}), Value@1], gleam_stdlib:bit_array_concat(_pipe) end}. -spec regex(binary(), binary()) -> {bison@kind:kind(), bitstring()}. regex(Pattern, Options) -> {{kind, <<16#0B>>}, begin _pipe = [<>], gleam_stdlib:bit_array_concat(_pipe) end}. -spec encode_kv({binary(), bison@bson:value()}) -> bitstring(). encode_kv(Pair) -> Key = <<(erlang:element(1, Pair))/binary, 0>>, Value@14 = case erlang:element(2, Pair) of na_n -> nan(); min -> min(); max -> max(); null -> null(); infinity -> infinity(); {js, Value} -> js(Value); {array, Value@1} -> array(Value@1); {int32, Value@2} -> int32(Value@2); {int64, Value@3} -> int64(Value@3); {double, Value@4} -> double(Value@4); {string, Value@5} -> string(Value@5); {boolean, Value@6} -> boolean(Value@6); {document, Value@7} -> document(Value@7); {date_time, Value@8} -> datetime(Value@8); {object_id, Value@9} -> object_id(Value@9); {binary, {md5, Value@10}} -> md5(Value@10); negative_infinity -> negative_infinity(); {binary, {uuid, Value@11}} -> uuid(Value@11); {binary, {custom, Value@12}} -> custom(Value@12); {binary, {generic, Value@13}} -> generic(Value@13); {regex, Pattern, Options} -> regex(Pattern, Options); {timestamp, Stamp, Counter} -> timestamp(Stamp, Counter) end, case Value@14 of {Kind, Value@15} -> _pipe = [erlang:element(2, Kind), Key, Value@15], gleam_stdlib:bit_array_concat(_pipe) end. -spec document(gleam@dict:dict(binary(), bison@bson:value())) -> {bison@kind:kind(), bitstring()}. document(Doc) -> Doc@1 = gleam@dict:fold( Doc, <<>>, fun(Acc, Key, Value) -> gleam@bit_array:append(Acc, encode_kv({Key, Value})) end ), Size = erlang:byte_size(Doc@1) + 5, {{kind, <<16#03>>}, begin _pipe = [<>, Doc@1, <<0>>], gleam_stdlib:bit_array_concat(_pipe) end}. -spec encode(gleam@dict:dict(binary(), bison@bson:value())) -> bitstring(). encode(Doc) -> case document(Doc) of {_, Value} -> Value end. -spec array(list(bison@bson:value())) -> {bison@kind:kind(), bitstring()}. array(Value) -> case begin _pipe = gleam@list:index_map( Value, fun(Item, Index) -> {gleam@int:to_string(Index), Item} end ), _pipe@1 = maps:from_list(_pipe), document(_pipe@1) end of {_, Value@1} -> {{kind, <<16#04>>}, Value@1} end.