Current section
Files
Jump to
Current section
Files
src/radish@encoder.erl
-module(radish@encoder).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function]).
-export([encode/1]).
-spec null() -> binary().
null() ->
<<"_\r\n"/utf8>>.
-spec nan() -> binary().
nan() ->
<<",nan\r\n"/utf8>>.
-spec infinity() -> binary().
infinity() ->
<<",inf\r\n"/utf8>>.
-spec negative_infinity() -> binary().
negative_infinity() ->
<<",-inf\r\n"/utf8>>.
-spec integer(integer()) -> binary().
integer(Value) ->
<<<<":"/utf8, (gleam@int:to_string(Value))/binary>>/binary, "\r\n"/utf8>>.
-spec boolean(boolean()) -> binary().
boolean(Value) ->
case Value of
true ->
<<"#t\r\n"/utf8>>;
false ->
<<"#f\r\n"/utf8>>
end.
-spec simple_string(binary()) -> binary().
simple_string(Value) ->
<<<<"+"/utf8,
(begin
_pipe = Value,
_pipe@1 = gleam@string:replace(
_pipe,
<<"\r"/utf8>>,
<<""/utf8>>
),
gleam@string:replace(_pipe@1, <<"\n"/utf8>>, <<""/utf8>>)
end)/binary>>/binary,
"\r\n"/utf8>>.
-spec bulk_string(binary()) -> binary().
bulk_string(Value) ->
<<<<<<<<"$"/utf8,
(begin
_pipe = Value,
_pipe@1 = gleam@string:length(_pipe),
gleam@int:to_string(_pipe@1)
end)/binary>>/binary,
"\r\n"/utf8>>/binary,
Value/binary>>/binary,
"\r\n"/utf8>>.
-spec simple_error(binary()) -> binary().
simple_error(Value) ->
<<<<"-"/utf8,
(begin
_pipe = Value,
_pipe@1 = gleam@string:replace(
_pipe,
<<"\r"/utf8>>,
<<""/utf8>>
),
gleam@string:replace(_pipe@1, <<"\n"/utf8>>, <<""/utf8>>)
end)/binary>>/binary,
"\r\n"/utf8>>.
-spec bulk_error(binary()) -> binary().
bulk_error(Value) ->
<<<<<<<<"!"/utf8,
(begin
_pipe = Value,
_pipe@1 = gleam@string:length(_pipe),
gleam@int:to_string(_pipe@1)
end)/binary>>/binary,
"\r\n"/utf8>>/binary,
Value/binary>>/binary,
"\r\n"/utf8>>.
-spec big_number(integer()) -> binary().
big_number(Value) ->
<<<<"("/utf8, (gleam@int:to_string(Value))/binary>>/binary, "\r\n"/utf8>>.
-spec double(float()) -> binary().
double(Value) ->
<<<<","/utf8, (gleam@float:to_string(Value))/binary>>/binary, "\r\n"/utf8>>.
-spec integer_as_double(integer()) -> binary().
integer_as_double(Value) ->
<<<<","/utf8, (gleam@int:to_string(Value))/binary>>/binary, "\r\n"/utf8>>.
-spec array(list(radish@resp:value())) -> binary().
array(Value) ->
<<<<<<"*"/utf8,
(begin
_pipe = Value,
_pipe@1 = gleam@list:length(_pipe),
gleam@int:to_string(_pipe@1)
end)/binary>>/binary,
"\r\n"/utf8>>/binary,
(begin
_pipe@2 = gleam@list:map(Value, fun encode_internal/1),
gleam@string:join(_pipe@2, <<""/utf8>>)
end)/binary>>.
-spec encode_internal(radish@resp:value()) -> binary().
encode_internal(Value) ->
case Value of
nan ->
nan();
null ->
null();
infinity ->
infinity();
{set, Value@1} ->
set(Value@1);
{map, Value@2} ->
map(Value@2);
{push, Value@3} ->
push(Value@3);
{array, Value@4} ->
array(Value@4);
{double, Value@5} ->
double(Value@5);
{boolean, Value@6} ->
boolean(Value@6);
{integer, Value@7} ->
integer(Value@7);
{big_number, Value@8} ->
big_number(Value@8);
{bulk_error, Value@9} ->
bulk_error(Value@9);
{bulk_string, Value@10} ->
bulk_string(Value@10);
negative_infinity ->
negative_infinity();
{simple_error, Value@11} ->
simple_error(Value@11);
{simple_string, Value@12} ->
simple_string(Value@12);
{integer_as_double, Value@13} ->
integer_as_double(Value@13)
end.
-spec encode(radish@resp:value()) -> bitstring().
encode(Value) ->
_pipe = encode_internal(Value),
gleam_stdlib:identity(_pipe).
-spec map(gleam@dict:dict(radish@resp:value(), radish@resp:value())) -> binary().
map(Value) ->
<<<<<<"%"/utf8,
(begin
_pipe = Value,
_pipe@1 = gleam@dict:size(_pipe),
gleam@int:to_string(_pipe@1)
end)/binary>>/binary,
"\r\n"/utf8>>/binary,
(begin
_pipe@2 = Value,
_pipe@3 = gleam@dict:to_list(_pipe@2),
_pipe@4 = gleam@list:map(
_pipe@3,
fun(Item) ->
<<(encode_internal(erlang:element(1, Item)))/binary,
(encode_internal(erlang:element(2, Item)))/binary>>
end
),
gleam@string:join(_pipe@4, <<""/utf8>>)
end)/binary>>.
-spec push(list(radish@resp:value())) -> binary().
push(Value) ->
<<<<<<">"/utf8,
(begin
_pipe = Value,
_pipe@1 = gleam@list:length(_pipe),
gleam@int:to_string(_pipe@1)
end)/binary>>/binary,
"\r\n"/utf8>>/binary,
(begin
_pipe@2 = gleam@list:map(Value, fun encode_internal/1),
gleam@string:join(_pipe@2, <<""/utf8>>)
end)/binary>>.
-spec set(gleam@set:set(radish@resp:value())) -> binary().
set(Value) ->
Value@1 = gleam@set:to_list(Value),
<<<<<<"~"/utf8,
(begin
_pipe = Value@1,
_pipe@1 = gleam@list:length(_pipe),
gleam@int:to_string(_pipe@1)
end)/binary>>/binary,
"\r\n"/utf8>>/binary,
(begin
_pipe@2 = gleam@list:map(Value@1, fun encode_internal/1),
gleam@string:join(_pipe@2, <<""/utf8>>)
end)/binary>>.