-module(bison@encoder). -compile([no_auto_import, nowarn_unused_vars]). -export([encode/1]). -spec null() -> {bison@kind:kind(), bitstring()}. null() -> {{kind, <<16#0A>>}, <<>>}. -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 = gleam@bit_string:byte_size(<>) + 1, {{kind, <<16#0D>>}, <>}. -spec string(binary()) -> {bison@kind:kind(), bitstring()}. string(Value) -> Length = gleam@bit_string: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:date_time()) -> {bison@kind:kind(), bitstring()}. datetime(Value) -> {duration, Value@1} = birl@time:difference( Value, {date_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_string(Value)}. -spec timestamp(integer()) -> {bison@kind:kind(), bitstring()}. timestamp(Value) -> {{kind, <<16#11>>}, <>}. -spec md5(bison@md5:md5()) -> {bison@kind:kind(), bitstring()}. md5(Value) -> Value@1 = bison@md5:to_bit_string(Value), Length = gleam@bit_string:byte_size(Value@1), {{kind, <<16#05>>}, begin _pipe = [<>, erlang:element(2, {sub_kind, <<16#5>>}), Value@1], gleam@bit_string:concat(_pipe) end}. -spec uuid(bison@uuid:uuid()) -> {bison@kind:kind(), bitstring()}. uuid(Value) -> Value@1 = bison@uuid:to_bit_string(Value), Length = gleam@bit_string:byte_size(Value@1), {{kind, <<16#05>>}, begin _pipe = [<>, erlang:element(2, {sub_kind, <<16#4>>}), Value@1], gleam@bit_string:concat(_pipe) end}. -spec custom(bison@custom:custom()) -> {bison@kind:kind(), bitstring()}. custom(Value) -> {Code, Value@1} = bison@custom:to_bit_string_with_code(Value), Length = gleam@bit_string:byte_size(Value@1), {{kind, <<16#05>>}, begin _pipe = [<>, <>, Value@1], gleam@bit_string:concat(_pipe) end}. -spec generic(bison@generic:generic()) -> {bison@kind:kind(), bitstring()}. generic(Value) -> Value@1 = bison@generic:to_bit_string(Value), Length = gleam@bit_string:byte_size(Value@1), {{kind, <<16#05>>}, begin _pipe = [<>, erlang:element(2, {sub_kind, <<16#0>>}), Value@1], gleam@bit_string:concat(_pipe) end}. -spec regex(binary(), binary()) -> {bison@kind:kind(), bitstring()}. regex(Pattern, Options) -> {{kind, <<16#0B>>}, begin _pipe = [<>], gleam@bit_string:concat(_pipe) end}. -spec encode_kv({binary(), bison@bson:value()}) -> bitstring(). encode_kv(Pair) -> Key = <<(erlang:element(1, Pair))/binary, 0>>, Value@15 = case erlang:element(2, Pair) of null -> null(); min -> min(); max -> max(); {js, Value} -> js(Value); {str, Value@1} -> string(Value@1); {array, Value@2} -> array(Value@2); {int32, Value@3} -> int32(Value@3); {int64, Value@4} -> int64(Value@4); {double, Value@5} -> double(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); {timestamp, Value@10} -> timestamp(Value@10); {binary, {md5, Value@11}} -> md5(Value@11); {binary, {uuid, Value@12}} -> uuid(Value@12); {binary, {custom, Value@13}} -> custom(Value@13); {binary, {generic, Value@14}} -> generic(Value@14); {regex, {Pattern, Options}} -> regex(Pattern, Options) end, case Value@15 of {Kind, Value@16} -> _pipe = [erlang:element(2, Kind), Key, Value@16], gleam@bit_string:concat(_pipe) end. -spec document(list({binary(), bison@bson:value()})) -> {bison@kind:kind(), bitstring()}. document(Doc) -> Doc@1 = begin _pipe = Doc, _pipe@1 = gleam@list:map(_pipe, fun encode_kv/1), gleam@bit_string:concat(_pipe@1) end, Size = gleam@bit_string:byte_size(Doc@1) + 5, {{kind, <<16#03>>}, begin _pipe@2 = [<>, Doc@1, <<0>>], gleam@bit_string:concat(_pipe@2) end}. -spec encode(list({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(Index, Item) -> {gleam@int:to_string(Index), Item} end ), document(_pipe) end of {_, Value@1} -> {{kind, <<16#04>>}, Value@1} end.