Current section
Files
Jump to
Current section
Files
src/butterbidi@script@types@primitive_protocol_value.erl
-module(butterbidi@script@types@primitive_protocol_value).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/butterbidi/script/types/primitive_protocol_value.gleam").
-export([number_to_json/1, primitive_protocol_value_to_json/1, number_to_string/1, to_string/1, to_bool/1, undefined/0, string/1, int/1, float/1, number_value_classifier/1, boolean/1]).
-export_type([primitive_protocol_value/0, undefined_value/0, null_value/0, string_value/0, special_number/0, number_/0, number_value/0, boolean_value/0, big_int_value/0]).
-type primitive_protocol_value() :: {undefined, undefined_value()} |
{null, null_value()} |
{string, string_value()} |
{number, number_value()} |
{boolean, boolean_value()} |
{big_int, big_int_value()}.
-type undefined_value() :: {undefined_value, binary()}.
-type null_value() :: {null_value, binary()}.
-type string_value() :: {string_value, binary(), binary()}.
-type special_number() :: na_n | negative_zero | infinity | negative_infinity.
-type number_() :: {int, integer()} |
{float, float()} |
{special, special_number()}.
-type number_value() :: {number_value, binary(), number_()}.
-type boolean_value() :: {boolean_value, binary(), boolean()}.
-type big_int_value() :: {big_int_value, binary(), binary()}.
-file("src/butterbidi/script/types/primitive_protocol_value.gleam", 113).
-spec special_number_to_string(special_number()) -> binary().
special_number_to_string(Special_number) ->
case Special_number of
na_n ->
<<"NaN"/utf8>>;
negative_zero ->
<<"-0"/utf8>>;
infinity ->
<<"Infinity"/utf8>>;
negative_infinity ->
<<"-Infinity"/utf8>>
end.
-file("src/butterbidi/script/types/primitive_protocol_value.gleam", 136).
-spec number_to_json(number_()) -> gleam@json:json().
number_to_json(Number) ->
case Number of
{int, Int} ->
gleam@json:int(Int);
{float, Float} ->
gleam@json:float(Float);
{special, Special_number} ->
gleam@json:string(special_number_to_string(Special_number))
end.
-file("src/butterbidi/script/types/primitive_protocol_value.gleam", 19).
-spec primitive_protocol_value_to_json(primitive_protocol_value()) -> gleam@json:json().
primitive_protocol_value_to_json(Value) ->
case Value of
{undefined, {undefined_value, Remote_type}} ->
gleam@json:object(
[{<<"type"/utf8>>, gleam@json:string(Remote_type)},
{<<"value"/utf8>>, gleam@json:string(<<"undefined"/utf8>>)}]
);
{null, {null_value, Remote_type@1}} ->
gleam@json:object(
[{<<"type"/utf8>>, gleam@json:string(Remote_type@1)},
{<<"value"/utf8>>, gleam@json:string(<<"null"/utf8>>)}]
);
{string, {string_value, Remote_type@2, Value@1}} ->
gleam@json:object(
[{<<"type"/utf8>>, gleam@json:string(Remote_type@2)},
{<<"value"/utf8>>, gleam@json:string(Value@1)}]
);
{number, {number_value, Remote_type@3, Value@2}} ->
gleam@json:object(
[{<<"type"/utf8>>, gleam@json:string(Remote_type@3)},
{<<"value"/utf8>>, number_to_json(Value@2)}]
);
{boolean, {boolean_value, Remote_type@4, Value@3}} ->
gleam@json:object(
[{<<"type"/utf8>>, gleam@json:string(Remote_type@4)},
{<<"value"/utf8>>, gleam@json:bool(Value@3)}]
);
{big_int, {big_int_value, Remote_type@5, Value@4}} ->
gleam@json:object(
[{<<"type"/utf8>>, gleam@json:string(Remote_type@5)},
{<<"value"/utf8>>, gleam@json:string(Value@4)}]
)
end.
-file("src/butterbidi/script/types/primitive_protocol_value.gleam", 128).
-spec number_to_string(number_()) -> binary().
number_to_string(Number) ->
case Number of
{int, Int} ->
erlang:integer_to_binary(Int);
{float, Float} ->
gleam_stdlib:float_to_string(Float);
{special, Special_number} ->
special_number_to_string(Special_number)
end.
-file("src/butterbidi/script/types/primitive_protocol_value.gleam", 54).
-spec to_string(primitive_protocol_value()) -> binary().
to_string(Value) ->
case Value of
{undefined, _} ->
<<"undefined"/utf8>>;
{null, _} ->
<<"null"/utf8>>;
{string, Value@1} ->
erlang:element(3, Value@1);
{number, Value@2} ->
number_to_string(erlang:element(3, Value@2));
{boolean, Value@3} ->
gleam@bool:to_string(erlang:element(3, Value@3));
{big_int, Value@4} ->
erlang:element(3, Value@4)
end.
-file("src/butterbidi/script/types/primitive_protocol_value.gleam", 65).
-spec to_bool(primitive_protocol_value()) -> boolean().
to_bool(Value) ->
case Value of
{boolean, Value@1} ->
erlang:element(3, Value@1);
_ ->
false
end.
-file("src/butterbidi/script/types/primitive_protocol_value.gleam", 76).
-spec undefined() -> primitive_protocol_value().
undefined() ->
{undefined, {undefined_value, <<"undefined"/utf8>>}}.
-file("src/butterbidi/script/types/primitive_protocol_value.gleam", 88).
-spec string(binary()) -> primitive_protocol_value().
string(Value) ->
{string, {string_value, <<"string"/utf8>>, Value}}.
-file("src/butterbidi/script/types/primitive_protocol_value.gleam", 99).
-spec string_to_special_number(binary()) -> special_number().
string_to_special_number(Variant) ->
case Variant of
<<"NaN"/utf8>> ->
na_n;
<<"-0"/utf8>> ->
negative_zero;
<<"Infinity"/utf8>> ->
infinity;
<<"-Infinity"/utf8>> ->
negative_infinity;
_ ->
_pipe = palabres:warning(<<"Unknown special number"/utf8>>),
palabres:string(_pipe, <<"variant"/utf8>>, Variant),
na_n
end.
-file("src/butterbidi/script/types/primitive_protocol_value.gleam", 145).
-spec int(integer()) -> primitive_protocol_value().
int(Int) ->
{number, {number_value, <<"number"/utf8>>, {int, Int}}}.
-file("src/butterbidi/script/types/primitive_protocol_value.gleam", 149).
-spec float(float()) -> primitive_protocol_value().
float(Float) ->
{number, {number_value, <<"number"/utf8>>, {float, Float}}}.
-file("src/butterbidi/script/types/primitive_protocol_value.gleam", 157).
-spec number_value_classifier(gleam@dynamic:dynamic_()) -> number_().
number_value_classifier(Value) ->
case gleam_stdlib:classify_dynamic(Value) of
<<"Int"/utf8>> ->
case gleam@dynamic@decode:run(
Value,
{decoder, fun gleam@dynamic@decode:decode_int/1}
) of
{ok, Int} ->
{int, Int};
{error, _} ->
_pipe = palabres:warning(<<"Failed to decode Int"/utf8>>),
_pipe@1 = palabres:string(
_pipe,
<<"value"/utf8>>,
gleam@string:inspect(Value)
),
palabres:log(_pipe@1),
{special, na_n}
end;
<<"Float"/utf8>> ->
case gleam@dynamic@decode:run(
Value,
{decoder, fun gleam@dynamic@decode:decode_float/1}
) of
{ok, Float} ->
{float, Float};
{error, _} ->
_pipe@2 = palabres:warning(
<<"Failed to decode Float"/utf8>>
),
_pipe@3 = palabres:string(
_pipe@2,
<<"value"/utf8>>,
gleam@string:inspect(Value)
),
palabres:log(_pipe@3),
{special, na_n}
end;
<<"String"/utf8>> ->
case gleam@dynamic@decode:run(
Value,
{decoder, fun gleam@dynamic@decode:decode_string/1}
) of
{ok, String} ->
{special, string_to_special_number(String)};
{error, _} ->
_pipe@4 = palabres:warning(
<<"Failed to decode String"/utf8>>
),
_pipe@5 = palabres:string(
_pipe@4,
<<"value"/utf8>>,
gleam@string:inspect(Value)
),
palabres:log(_pipe@5),
{special, na_n}
end;
_ ->
_pipe@6 = palabres:warning(<<"Unknown number type"/utf8>>),
_pipe@7 = palabres:string(
_pipe@6,
<<"type"/utf8>>,
gleam_stdlib:classify_dynamic(Value)
),
palabres:log(_pipe@7),
{special, na_n}
end.
-file("src/butterbidi/script/types/primitive_protocol_value.gleam", 202).
-spec boolean(boolean()) -> primitive_protocol_value().
boolean(Boolean) ->
{boolean, {boolean_value, <<"boolean"/utf8>>, Boolean}}.