Current section

Files

Jump to
json_canvas src json_canvas@encode@node.erl
Raw

src/json_canvas@encode@node.erl

-module(json_canvas@encode@node).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([encode_file_path/1, encode_subpath/1, encode_url/1, encode_group_label/1, encode_group_background/1, encode_group_background_style/1, encode_node/1]).
-spec encode_file_path(json_canvas@types:file_path()) -> gleam@json:json().
encode_file_path(File_path) ->
{file_path, File_path@1} = File_path,
gleam@json:string(File_path@1).
-spec encode_subpath(json_canvas@types:subpath()) -> gleam@json:json().
encode_subpath(Subpath) ->
{subpath, Subpath@1} = Subpath,
gleam@json:string(Subpath@1).
-spec encode_url(json_canvas@types:url()) -> gleam@json:json().
encode_url(Url) ->
{url, Url@1} = Url,
gleam@json:string(Url@1).
-spec encode_group_label(json_canvas@types:group_label()) -> gleam@json:json().
encode_group_label(Group_label) ->
{group_label, Group_label@1} = Group_label,
gleam@json:string(Group_label@1).
-spec encode_group_background(json_canvas@types:group_background()) -> gleam@json:json().
encode_group_background(Group_background) ->
{group_background, Group_background@1} = Group_background,
gleam@json:string(Group_background@1).
-spec encode_group_background_style(json_canvas@types:group_background_style()) -> gleam@json:json().
encode_group_background_style(Group_background_style) ->
_pipe = case Group_background_style of
cover ->
<<"cover"/utf8>>;
ratio ->
<<"ratio"/utf8>>;
repeat ->
<<"repeat"/utf8>>
end,
gleam@json:string(_pipe).
-spec filter_entry(list({binary(), gleam@option:option(gleam@json:json())})) -> list({binary(),
gleam@json:json()}).
filter_entry(List) ->
_pipe = List,
gleam@list:filter_map(
_pipe,
fun(Entry) ->
{Key, Value} = Entry,
gleam@result:'try'(
begin
_pipe@1 = Value,
gleam@option:to_result(_pipe@1, nil)
end,
fun(Value@1) -> {ok, {Key, Value@1}} end
)
end
).
-spec encode_node(json_canvas@types:node_()) -> gleam@json:json().
encode_node(Node) ->
case Node of
{text_node, Id, X, Y, Width, Height, Color, Text} ->
Required_entries = [{<<"id"/utf8>>,
json_canvas@encode@node_id:encode_node_id(Id)},
{<<"x"/utf8>>, gleam@json:int(X)},
{<<"y"/utf8>>, gleam@json:int(Y)},
{<<"width"/utf8>>, gleam@json:int(Width)},
{<<"height"/utf8>>, gleam@json:int(Height)},
{<<"text"/utf8>>, gleam@json:string(Text)}],
Optional_entries = begin
_pipe = [{<<"color"/utf8>>,
gleam@option:map(
Color,
fun json_canvas@encode@color:encode_color/1
)}],
filter_entry(_pipe)
end,
gleam@json:object(lists:append(Required_entries, Optional_entries));
{file_node, Id@1, X@1, Y@1, Width@1, Height@1, Color@1, Path, Subpath} ->
Required_entries@1 = [{<<"id"/utf8>>,
json_canvas@encode@node_id:encode_node_id(Id@1)},
{<<"x"/utf8>>, gleam@json:int(X@1)},
{<<"y"/utf8>>, gleam@json:int(Y@1)},
{<<"width"/utf8>>, gleam@json:int(Width@1)},
{<<"height"/utf8>>, gleam@json:int(Height@1)},
{<<"path"/utf8>>, encode_file_path(Path)}],
Optional_entries@1 = begin
_pipe@1 = [{<<"color"/utf8>>,
gleam@option:map(
Color@1,
fun json_canvas@encode@color:encode_color/1
)},
{<<"subpath"/utf8>>,
gleam@option:map(Subpath, fun encode_subpath/1)}],
filter_entry(_pipe@1)
end,
gleam@json:object(
lists:append(Required_entries@1, Optional_entries@1)
);
{link_node, Id@2, X@2, Y@2, Width@2, Height@2, Color@2, Url} ->
Required_entries@2 = [{<<"id"/utf8>>,
json_canvas@encode@node_id:encode_node_id(Id@2)},
{<<"x"/utf8>>, gleam@json:int(X@2)},
{<<"y"/utf8>>, gleam@json:int(Y@2)},
{<<"width"/utf8>>, gleam@json:int(Width@2)},
{<<"height"/utf8>>, gleam@json:int(Height@2)},
{<<"url"/utf8>>, encode_url(Url)}],
Optional_entries@2 = begin
_pipe@2 = [{<<"color"/utf8>>,
gleam@option:map(
Color@2,
fun json_canvas@encode@color:encode_color/1
)}],
filter_entry(_pipe@2)
end,
gleam@json:object(
lists:append(Required_entries@2, Optional_entries@2)
);
{group_node,
Id@3,
X@3,
Y@3,
Width@3,
Height@3,
Color@3,
Label,
Background,
Background_style} ->
Required_entries@3 = [{<<"id"/utf8>>,
json_canvas@encode@node_id:encode_node_id(Id@3)},
{<<"x"/utf8>>, gleam@json:int(X@3)},
{<<"y"/utf8>>, gleam@json:int(Y@3)},
{<<"width"/utf8>>, gleam@json:int(Width@3)},
{<<"height"/utf8>>, gleam@json:int(Height@3)}],
Optional_entries@3 = begin
_pipe@3 = [{<<"color"/utf8>>,
gleam@option:map(
Color@3,
fun json_canvas@encode@color:encode_color/1
)},
{<<"label"/utf8>>,
gleam@option:map(Label, fun encode_group_label/1)},
{<<"background"/utf8>>,
gleam@option:map(
Background,
fun encode_group_background/1
)},
{<<"backgroundStyle"/utf8>>,
gleam@option:map(
Background_style,
fun encode_group_background_style/1
)}],
filter_entry(_pipe@3)
end,
gleam@json:object(
lists:append(Required_entries@3, Optional_entries@3)
)
end.