Current section

Files

Jump to
distribute src distribute@codec@composite.erl
Raw

src/distribute@codec@composite.erl

-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 → `<<0>>`, Some(v) → `<<1, encoded_v>>`.\n").
-spec option_encoder(
fun((GYF) -> {ok, bitstring()} | {error, distribute@codec:encode_error()})
) -> fun((gleam@option:option(GYF)) -> {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 → `<<0, encoded>>`, Error → `<<1, encoded>>`.\n").
-spec result_encoder(
fun((GYR) -> {ok, bitstring()} | {error, distribute@codec:encode_error()}),
fun((GYT) -> {ok, bitstring()} | {error, distribute@codec:encode_error()})
) -> fun(({ok, GYR} | {error, GYT}) -> {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, {GYY, bitstring()}} |
{error, distribute@codec:decode_error()}),
fun((bitstring()) -> {ok, {GZA, bitstring()}} |
{error, distribute@codec:decode_error()})
) -> fun((bitstring()) -> {ok, {{ok, GYY} | {error, GZA}, 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, GZF} | {error, distribute@codec:decode_error()}),
fun((bitstring()) -> {ok, GZH} | {error, distribute@codec:decode_error()})
) -> fun((bitstring()) -> {ok, {ok, GZF} | {error, GZH}} |
{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((GZM) -> {ok, bitstring()} | {error, distribute@codec:encode_error()}),
fun((GZO) -> {ok, bitstring()} | {error, distribute@codec:encode_error()})
) -> fun(({GZM, GZO}) -> {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(
[<<Len_a:32>>, Encoded_a, Encoded_b]
)}
end
)
end
)
end.
-file("src/distribute/codec/composite.gleam", 128).
-spec tuple2_sized_decoder(
fun((bitstring()) -> {ok, {GZR, bitstring()}} |
{error, distribute@codec:decode_error()}),
fun((bitstring()) -> {ok, {GZT, bitstring()}} |
{error, distribute@codec:decode_error()})
) -> fun((bitstring()) -> {ok, {{GZR, GZT}, bitstring()}} |
{error, distribute@codec:decode_error()}).
tuple2_sized_decoder(First, Second) ->
fun(Data) -> case Data of
<<Len_a:32, Rest/binary>> ->
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, {GZW, bitstring()}} |
{error, distribute@codec:decode_error()}),
fun((bitstring()) -> {ok, {GZY, bitstring()}} |
{error, distribute@codec:decode_error()})
) -> fun((bitstring()) -> {ok, {GZW, GZY}} |
{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((HAB) -> {ok, bitstring()} | {error, distribute@codec:encode_error()}),
fun((HAD) -> {ok, bitstring()} | {error, distribute@codec:encode_error()}),
fun((HAF) -> {ok, bitstring()} | {error, distribute@codec:encode_error()})
) -> fun(({HAB, HAD, HAF}) -> {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(
[<<Len_a:32>>,
Encoded_a,
<<Len_b:32>>,
Encoded_b,
Encoded_c]
)}
end
)
end
)
end
)
end.
-file("src/distribute/codec/composite.gleam", 189).
-spec tuple3_sized_decoder(
fun((bitstring()) -> {ok, {HAI, bitstring()}} |
{error, distribute@codec:decode_error()}),
fun((bitstring()) -> {ok, {HAK, bitstring()}} |
{error, distribute@codec:decode_error()}),
fun((bitstring()) -> {ok, {HAM, bitstring()}} |
{error, distribute@codec:decode_error()})
) -> fun((bitstring()) -> {ok, {{HAI, HAK, HAM}, bitstring()}} |
{error, distribute@codec:decode_error()}).
tuple3_sized_decoder(First, Second, Third) ->
fun(Data) -> case Data of
<<Len_a:32, Rest/binary>> ->
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
<<Len_b:32, Rest2/binary>> ->
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, {HAP, bitstring()}} |
{error, distribute@codec:decode_error()}),
fun((bitstring()) -> {ok, {HAR, bitstring()}} |
{error, distribute@codec:decode_error()}),
fun((bitstring()) -> {ok, {HAT, bitstring()}} |
{error, distribute@codec:decode_error()})
) -> fun((bitstring()) -> {ok, {HAP, HAR, HAT}} |
{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(HBA), distribute@codec:codec(HBC)) -> distribute@codec:codec({HBA,
HBC}).
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(HBF),
distribute@codec:codec(HBH),
distribute@codec:codec(HBJ)
) -> distribute@codec:codec({HBF, HBH, HBJ}).
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(HAW)) -> distribute@codec:codec(gleam@option:option(HAW)).
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, {GYJ, bitstring()}} |
{error, distribute@codec:decode_error()})
) -> fun((bitstring()) -> {ok, {gleam@option:option(GYJ), 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, GYN} | {error, distribute@codec:decode_error()})
) -> fun((bitstring()) -> {ok, gleam@option:option(GYN)} |
{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.