Packages

A pure Gleam implementation of Google Protobuf.

Current section

Files

Jump to
acrostic src acrostic@encoding.erl
Raw

src/acrostic@encoding.erl

-module(acrostic@encoding).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([encode_i32/1, encode_i64/1, encode_string/1, encode_varint/1, encode_bool/1, encode_field/3, encode_repeated_field/3]).
-export_type([wire_type/0, field_encoder/1]).
-type wire_type() :: wire_var_int_ty | wire_i64_ty | wire_len_ty | wire_i32_ty.
-type field_encoder(MOU) :: {field_encoder,
wire_type(),
fun((MOU) -> bitstring())}.
-file("/Users/windy/study/gleam/acrostic/src/acrostic/encoding.gleam", 33).
-spec encode_i32(float()) -> bitstring().
encode_i32(N) ->
<<N:32/float-little>>.
-file("/Users/windy/study/gleam/acrostic/src/acrostic/encoding.gleam", 37).
-spec encode_i64(float()) -> bitstring().
encode_i64(N) ->
<<N:64/float-little>>.
-file("/Users/windy/study/gleam/acrostic/src/acrostic/encoding.gleam", 41).
-spec encode_string(binary()) -> bitstring().
encode_string(S) ->
<<S/binary>>.
-file("/Users/windy/study/gleam/acrostic/src/acrostic/encoding.gleam", 53).
-spec wire_to_int(wire_type()) -> integer().
wire_to_int(Wire) ->
case Wire of
wire_var_int_ty ->
0;
wire_i64_ty ->
1;
wire_len_ty ->
2;
wire_i32_ty ->
5
end.
-file("/Users/windy/study/gleam/acrostic/src/acrostic/encoding.gleam", 12).
-spec encode_varint(integer()) -> bitstring().
encode_varint(N) ->
case erlang:'band'(N, 16#FFFFFFFFFFFFFFFF) of
X when X >= 16#80 ->
gleam@bit_array:append(
<<(begin
_pipe = N,
_pipe@1 = erlang:'band'(_pipe, 16#7F),
erlang:'bor'(_pipe@1, 16#80)
end)>>,
encode_varint(acrostic@helper:lsr(X, 7))
);
X@1 ->
<<(erlang:'band'(X@1, 16#7F))>>
end.
-file("/Users/windy/study/gleam/acrostic/src/acrostic/encoding.gleam", 24).
-spec encode_bool(boolean()) -> bitstring().
encode_bool(B) ->
encode_varint((case B of
true ->
1;
false ->
0
end)).
-file("/Users/windy/study/gleam/acrostic/src/acrostic/encoding.gleam", 111).
-spec encode_key(integer(), integer()) -> bitstring().
encode_key(Field_number, Wire_type) ->
Key = begin
_pipe = Field_number,
_pipe@1 = erlang:'bsl'(_pipe, 3),
erlang:'bor'(_pipe@1, Wire_type)
end,
encode_varint(Key).
-file("/Users/windy/study/gleam/acrostic/src/acrostic/encoding.gleam", 66).
-spec encode_field(integer(), MPA, field_encoder(MPA)) -> bitstring().
encode_field(Field_num, Value, Encoder) ->
Key = encode_key(
Field_num,
begin
_pipe = erlang:element(2, Encoder),
wire_to_int(_pipe)
end
),
Body = (erlang:element(3, Encoder))(Value),
Length = case erlang:element(2, Encoder) =:= wire_len_ty of
true ->
encode_varint(erlang:byte_size(Body));
false ->
<<>>
end,
_pipe@1 = Key,
_pipe@2 = gleam@bit_array:append(_pipe@1, Length),
gleam@bit_array:append(_pipe@2, Body).
-file("/Users/windy/study/gleam/acrostic/src/acrostic/encoding.gleam", 81).
-spec encode_repeated_field(integer(), list(MPC), field_encoder(MPC)) -> bitstring().
encode_repeated_field(Field_num, List, Encoder) ->
case List of
[] ->
<<>>;
_ ->
Packed = erlang:element(2, Encoder) /= wire_len_ty,
case Packed of
true ->
Key = encode_key(
Field_num,
begin
_pipe = wire_len_ty,
wire_to_int(_pipe)
end
),
Body = begin
_pipe@1 = List,
_pipe@2 = gleam@list:map(
_pipe@1,
fun(V) -> (erlang:element(3, Encoder))(V) end
),
gleam@list:fold(
_pipe@2,
<<>>,
fun gleam@bit_array:append/2
)
end,
Length = encode_varint(erlang:byte_size(Body)),
_pipe@3 = Key,
_pipe@4 = gleam@bit_array:append(_pipe@3, Length),
gleam@bit_array:append(_pipe@4, Body);
false ->
_pipe@5 = List,
_pipe@6 = gleam@list:map(
_pipe@5,
fun(V@1) -> encode_field(Field_num, V@1, Encoder) end
),
gleam@list:fold(_pipe@6, <<>>, fun gleam@bit_array:append/2)
end
end.