-module(distribute@codec@composite). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/distribute/codec/composite.gleam"). -export([option_encoder/1, result_encoder/2, result_sized_decoder/2, result_decoder/2, tuple2_encoder/2, tuple2_sized_decoder/2, tuple2_decoder/2, tuple3_encoder/3, tuple3_sized_decoder/3, tuple3_decoder/3, tuple2/2, tuple3/3, option/1, option_sized_decoder/1, option_decoder/1]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. -file("src/distribute/codec/composite.gleam", 14). ?DOC(" Encoder for Option(a). None is 0x00, Some(v) is 0x01 + encoded value.\n"). -spec option_encoder( fun((HCD) -> {ok, bitstring()} | {error, distribute@codec:encode_error()}) ) -> fun((gleam@option:option(HCD)) -> {ok, bitstring()} | {error, distribute@codec:encode_error()}). option_encoder(Inner) -> fun(Opt) -> case Opt of none -> {ok, <<0>>}; {some, Value} -> gleam@result:'try'( Inner(Value), fun(Encoded) -> {ok, gleam@bit_array:append(<<1>>, Encoded)} end ) end end. -file("src/distribute/codec/composite.gleam", 57). ?DOC(" Encoder for Result(a, e). Ok is 0x00 + encoded, Error is 0x01 + encoded.\n"). -spec result_encoder( fun((HCP) -> {ok, bitstring()} | {error, distribute@codec:encode_error()}), fun((HCR) -> {ok, bitstring()} | {error, distribute@codec:encode_error()}) ) -> fun(({ok, HCP} | {error, HCR}) -> {ok, bitstring()} | {error, distribute@codec:encode_error()}). result_encoder(Ok_encoder, Error_encoder) -> fun(Res) -> case Res of {ok, Value} -> gleam@result:'try'( Ok_encoder(Value), fun(Encoded) -> {ok, gleam@bit_array:append(<<0>>, Encoded)} end ); {error, Err} -> gleam@result:'try'( Error_encoder(Err), fun(Encoded@1) -> {ok, gleam@bit_array:append(<<1>>, Encoded@1)} end ) end end. -file("src/distribute/codec/composite.gleam", 75). -spec result_sized_decoder( fun((bitstring()) -> {ok, {HCW, bitstring()}} | {error, distribute@codec:decode_error()}), fun((bitstring()) -> {ok, {HCY, bitstring()}} | {error, distribute@codec:decode_error()}) ) -> fun((bitstring()) -> {ok, {{ok, HCW} | {error, HCY}, bitstring()}} | {error, distribute@codec:decode_error()}). result_sized_decoder(Ok_decoder, Error_decoder) -> fun(Data) -> case Data of <<0, Rest/binary>> -> gleam@result:'try'( Ok_decoder(Rest), fun(_use0) -> {Value, Remaining} = _use0, {ok, {{ok, Value}, Remaining}} end ); <<1, Rest@1/binary>> -> gleam@result:'try'( Error_decoder(Rest@1), fun(_use0@1) -> {Err, Remaining@1} = _use0@1, {ok, {{error, Err}, Remaining@1}} end ); _ -> {error, {invalid_binary, <<"invalid result tag"/utf8>>}} end end. -file("src/distribute/codec/composite.gleam", 94). -spec result_decoder( fun((bitstring()) -> {ok, HDD} | {error, distribute@codec:decode_error()}), fun((bitstring()) -> {ok, HDF} | {error, distribute@codec:decode_error()}) ) -> fun((bitstring()) -> {ok, {ok, HDD} | {error, HDF}} | {error, distribute@codec:decode_error()}). result_decoder(Ok_decoder, Error_decoder) -> fun(Data) -> case Data of <<0, Rest/binary>> -> gleam@result:'try'( Ok_decoder(Rest), fun(Value) -> {ok, {ok, Value}} end ); <<1, Rest@1/binary>> -> gleam@result:'try'( Error_decoder(Rest@1), fun(Err) -> {ok, {error, Err}} end ); _ -> {error, {invalid_binary, <<"invalid result tag"/utf8>>}} end end. -file("src/distribute/codec/composite.gleam", 118). ?DOC(" Encoder for #(a, b). First element is length-prefixed (32-bit).\n"). -spec tuple2_encoder( fun((HDK) -> {ok, bitstring()} | {error, distribute@codec:encode_error()}), fun((HDM) -> {ok, bitstring()} | {error, distribute@codec:encode_error()}) ) -> fun(({HDK, HDM}) -> {ok, bitstring()} | {error, distribute@codec:encode_error()}). tuple2_encoder(First, Second) -> fun(Tuple) -> {A, B} = Tuple, gleam@result:'try'( First(A), fun(Encoded_a) -> gleam@result:'try'( Second(B), fun(Encoded_b) -> Len_a = erlang:byte_size(Encoded_a), {ok, gleam_stdlib:bit_array_concat( [<>, Encoded_a, Encoded_b] )} end ) end ) end. -file("src/distribute/codec/composite.gleam", 128). -spec tuple2_sized_decoder( fun((bitstring()) -> {ok, {HDP, bitstring()}} | {error, distribute@codec:decode_error()}), fun((bitstring()) -> {ok, {HDR, bitstring()}} | {error, distribute@codec:decode_error()}) ) -> fun((bitstring()) -> {ok, {{HDP, HDR}, bitstring()}} | {error, distribute@codec:decode_error()}). tuple2_sized_decoder(First, Second) -> fun(Data) -> case Data of <> -> Rest_size = erlang:byte_size(Rest), gleam@result:'try'( begin _pipe = gleam_stdlib:bit_array_slice(Rest, 0, Len_a), gleam@result:replace_error( _pipe, {insufficient_data, <<"tuple2 first"/utf8>>} ) end, fun(First_data) -> gleam@result:'try'( First(First_data), fun(_use0) -> {A, _} = _use0, gleam@result:'try'( begin _pipe@1 = gleam_stdlib:bit_array_slice( Rest, Len_a, Rest_size - Len_a ), gleam@result:replace_error( _pipe@1, {insufficient_data, <<"tuple2 second"/utf8>>} ) end, fun(After_first) -> gleam@result:'try'( Second(After_first), fun(_use0@1) -> {B, Remaining} = _use0@1, {ok, {{A, B}, Remaining}} end ) end ) end ) end ); _ -> {error, {invalid_binary, <<"missing tuple2 length prefix"/utf8>>}} end end. -file("src/distribute/codec/composite.gleam", 153). -spec tuple2_decoder( fun((bitstring()) -> {ok, {HDU, bitstring()}} | {error, distribute@codec:decode_error()}), fun((bitstring()) -> {ok, {HDW, bitstring()}} | {error, distribute@codec:decode_error()}) ) -> fun((bitstring()) -> {ok, {HDU, HDW}} | {error, distribute@codec:decode_error()}). tuple2_decoder(First, Second) -> distribute@codec:to_decoder(tuple2_sized_decoder(First, Second)). -file("src/distribute/codec/composite.gleam", 165). ?DOC(" Encoder for #(a, b, c). First two elements are length-prefixed (32-bit).\n"). -spec tuple3_encoder( fun((HDZ) -> {ok, bitstring()} | {error, distribute@codec:encode_error()}), fun((HEB) -> {ok, bitstring()} | {error, distribute@codec:encode_error()}), fun((HED) -> {ok, bitstring()} | {error, distribute@codec:encode_error()}) ) -> fun(({HDZ, HEB, HED}) -> {ok, bitstring()} | {error, distribute@codec:encode_error()}). tuple3_encoder(First, Second, Third) -> fun(Tuple) -> {A, B, C} = Tuple, gleam@result:'try'( First(A), fun(Encoded_a) -> gleam@result:'try'( Second(B), fun(Encoded_b) -> gleam@result:'try'( Third(C), fun(Encoded_c) -> Len_a = erlang:byte_size(Encoded_a), Len_b = erlang:byte_size(Encoded_b), {ok, gleam_stdlib:bit_array_concat( [<>, Encoded_a, <>, Encoded_b, Encoded_c] )} end ) end ) end ) end. -file("src/distribute/codec/composite.gleam", 189). -spec tuple3_sized_decoder( fun((bitstring()) -> {ok, {HEG, bitstring()}} | {error, distribute@codec:decode_error()}), fun((bitstring()) -> {ok, {HEI, bitstring()}} | {error, distribute@codec:decode_error()}), fun((bitstring()) -> {ok, {HEK, bitstring()}} | {error, distribute@codec:decode_error()}) ) -> fun((bitstring()) -> {ok, {{HEG, HEI, HEK}, bitstring()}} | {error, distribute@codec:decode_error()}). tuple3_sized_decoder(First, Second, Third) -> fun(Data) -> case Data of <> -> Rest_size = erlang:byte_size(Rest), gleam@result:'try'( begin _pipe = gleam_stdlib:bit_array_slice(Rest, 0, Len_a), gleam@result:replace_error( _pipe, {insufficient_data, <<"tuple3 first"/utf8>>} ) end, fun(First_data) -> gleam@result:'try'( First(First_data), fun(_use0) -> {A, _} = _use0, gleam@result:'try'( begin _pipe@1 = gleam_stdlib:bit_array_slice( Rest, Len_a, Rest_size - Len_a ), gleam@result:replace_error( _pipe@1, {insufficient_data, <<"tuple3 after first"/utf8>>} ) end, fun(After_first) -> case After_first of <> -> Rest2_size = erlang:byte_size( Rest2 ), gleam@result:'try'( begin _pipe@2 = gleam_stdlib:bit_array_slice( Rest2, 0, Len_b ), gleam@result:replace_error( _pipe@2, {insufficient_data, <<"tuple3 second"/utf8>>} ) end, fun(Second_data) -> gleam@result:'try'( Second(Second_data), fun(_use0@1) -> {B, _} = _use0@1, gleam@result:'try'( begin _pipe@3 = gleam_stdlib:bit_array_slice( Rest2, Len_b, Rest2_size - Len_b ), gleam@result:replace_error( _pipe@3, {insufficient_data, <<"tuple3 third"/utf8>>} ) end, fun( Third_data ) -> gleam@result:'try'( Third( Third_data ), fun( _use0@2 ) -> {C, Remaining} = _use0@2, {ok, {{A, B, C}, Remaining}} end ) end ) end ) end ); _ -> {error, {invalid_binary, <<"tuple3 missing second length"/utf8>>}} end end ) end ) end ); _ -> {error, {invalid_binary, <<"tuple3 missing length"/utf8>>}} end end. -file("src/distribute/codec/composite.gleam", 230). -spec tuple3_decoder( fun((bitstring()) -> {ok, {HEN, bitstring()}} | {error, distribute@codec:decode_error()}), fun((bitstring()) -> {ok, {HEP, bitstring()}} | {error, distribute@codec:decode_error()}), fun((bitstring()) -> {ok, {HER, bitstring()}} | {error, distribute@codec:decode_error()}) ) -> fun((bitstring()) -> {ok, {HEN, HEP, HER}} | {error, distribute@codec:decode_error()}). tuple3_decoder(First, Second, Third) -> distribute@codec:to_decoder(tuple3_sized_decoder(First, Second, Third)). -file("src/distribute/codec/composite.gleam", 250). -spec tuple2(distribute@codec:codec(HEY), distribute@codec:codec(HFA)) -> distribute@codec:codec({HEY, HFA}). tuple2(First, Second) -> {codec, tuple2_encoder(erlang:element(2, First), erlang:element(2, Second)), tuple2_decoder(erlang:element(4, First), erlang:element(4, Second)), tuple2_sized_decoder( erlang:element(4, First), erlang:element(4, Second) )}. -file("src/distribute/codec/composite.gleam", 264). -spec tuple3( distribute@codec:codec(HFD), distribute@codec:codec(HFF), distribute@codec:codec(HFH) ) -> distribute@codec:codec({HFD, HFF, HFH}). tuple3(First, Second, Third) -> {codec, tuple3_encoder( erlang:element(2, First), erlang:element(2, Second), erlang:element(2, Third) ), tuple3_decoder( erlang:element(4, First), erlang:element(4, Second), erlang:element(4, Third) ), tuple3_sized_decoder( erlang:element(4, First), erlang:element(4, Second), erlang:element(4, Third) )}. -file("src/distribute/codec/composite.gleam", 242). -spec option(distribute@codec:codec(HEU)) -> distribute@codec:codec(gleam@option:option(HEU)). option(Inner) -> {codec, option_encoder(erlang:element(2, Inner)), option_decoder(erlang:element(3, Inner)), option_sized_decoder(erlang:element(4, Inner))}. -file("src/distribute/codec/composite.gleam", 26). -spec option_sized_decoder( fun((bitstring()) -> {ok, {HCH, bitstring()}} | {error, distribute@codec:decode_error()}) ) -> fun((bitstring()) -> {ok, {gleam@option:option(HCH), bitstring()}} | {error, distribute@codec:decode_error()}). option_sized_decoder(Inner) -> fun(Data) -> case Data of <<0, Rest/binary>> -> {ok, {none, Rest}}; <<1, Rest@1/binary>> -> gleam@result:'try'( Inner(Rest@1), fun(_use0) -> {Value, Remaining} = _use0, {ok, {{some, Value}, Remaining}} end ); _ -> {error, {invalid_binary, <<"invalid option tag"/utf8>>}} end end. -file("src/distribute/codec/composite.gleam", 39). -spec option_decoder( fun((bitstring()) -> {ok, HCL} | {error, distribute@codec:decode_error()}) ) -> fun((bitstring()) -> {ok, gleam@option:option(HCL)} | {error, distribute@codec:decode_error()}). option_decoder(Inner) -> fun(Data) -> case Data of <<0, _/binary>> -> {ok, none}; <<1, Rest/binary>> -> gleam@result:'try'( Inner(Rest), fun(Value) -> {ok, {some, Value}} end ); _ -> {error, {invalid_binary, <<"invalid option tag"/utf8>>}} end end.