Current section
Files
Jump to
Current section
Files
src/parsers@toml@printer.erl
-module(parsers@toml@printer).
-compile(no_auto_import).
-export([toml_to_json/1]).
-spec parse_toml(binary()) -> {ok, list({binary(), parsers@toml@model:node_()})} |
{error, parser_gleam@parse_result:parse_error(binary())}.
parse_toml(It) ->
case begin
_pipe = parsers@toml@parser:toml_doc_parser(),
(parser_gleam@string:run(It))(_pipe)
end of
{error, _try} -> {error, _try};
{ok, R} ->
{ok, erlang:element(2, R)}
end.
-spec with_type_info(binary(), binary()) -> gleam@json:json().
with_type_info(Type_, Value) ->
_pipe@2 = [{<<"type"/utf8>>,
begin
_pipe = Type_,
gleam@json:string(_pipe)
end},
{<<"value"/utf8>>,
begin
_pipe@1 = Value,
gleam@json:string(_pipe@1)
end}],
gleam@json:object(_pipe@2).
-spec table_to_json_object(list({binary(), parsers@toml@model:node_()})) -> gleam@json:json().
table_to_json_object(Tbl) ->
_pipe = Tbl,
_pipe@1 = gleam@list:map(
_pipe,
fun(It) ->
{Key, Node} = It,
{Key, node_to_json(Node)}
end
),
gleam@json:object(_pipe@1).
-spec node_to_json(parsers@toml@model:node_()) -> gleam@json:json().
node_to_json(Node) ->
case Node of
{v_table, It} ->
table_to_json_object(It);
{vt_array, It@1} ->
gleam@json:array(It@1, fun table_to_json_object/1);
{v_string, It@2} ->
with_type_info(<<"string"/utf8>>, It@2);
{v_integer, It@3} ->
with_type_info(
<<"integer"/utf8>>,
begin
_pipe = It@3,
gleam@int:to_string(_pipe)
end
);
{v_float, It@4} ->
with_type_info(<<"float"/utf8>>, case It@4 of
{float_numeric, It@5} ->
_pipe@1 = It@5,
_pipe@2 = gleam@json:float(_pipe@1),
gleam@json:to_string(_pipe@2);
{na_n, _@1} ->
<<"nan"/utf8>>;
{inf, Pos} ->
gleam@string:concat(
[parsers@toml@model:positiveness_to_string(Pos),
<<"inf"/utf8>>]
)
end);
{v_boolean, It@6} ->
with_type_info(
<<"bool"/utf8>>,
begin
_pipe@3 = It@6,
_pipe@4 = gleam@json:bool(_pipe@3),
gleam@json:to_string(_pipe@4)
end
);
{v_datetime, It@7} ->
case It@7 of
{rfc3339_datetime, _@2} ->
with_type_info(
<<"datetime"/utf8>>,
parsers@rfc_3339:print_rfc_3339(It@7)
);
{rfc3339_local_datetime, _@3} ->
with_type_info(
<<"datetime-local"/utf8>>,
parsers@rfc_3339:print_rfc_3339(It@7)
);
{rfc3339_local_date, _@4} ->
with_type_info(
<<"date-local"/utf8>>,
parsers@rfc_3339:print_rfc_3339(It@7)
);
{rfc3339_local_time, _@5} ->
with_type_info(
<<"time-local"/utf8>>,
parsers@rfc_3339:print_rfc_3339(It@7)
)
end;
{v_array, It@8} ->
gleam@json:array(It@8, fun node_to_json/1)
end.
-spec serialize_toml(list({binary(), parsers@toml@model:node_()})) -> binary().
serialize_toml(Toml) ->
_pipe = table_to_json_object(Toml),
gleam@json:to_string(_pipe).
-spec toml_to_json(binary()) -> {ok, binary()} |
{error, parser_gleam@parse_result:parse_error(binary())}.
toml_to_json(It) ->
case parse_toml(It) of
{error, _try} -> {error, _try};
{ok, Ast} ->
{ok, serialize_toml(Ast)}
end.