-module(roundabout@generate). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/roundabout/generate.gleam"). -export([get_function_arguments/3, get_function_name/2, get_type_name/2, generate_utils/0, generate_imports/0, generate_type/2, generate_type_rec/2, generate_segments_to_route/2, generate_segments_to_route_rec/2, generate_route_to_path/2, generate_route_to_path_rec/2, generate_helpers_rec/2]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. -file("src/roundabout/generate.gleam", 98). -spec generate_type_variant_param(roundabout@node:segment()) -> {ok, binary()} | {error, nil}. generate_type_variant_param(Segment) -> case Segment of {seg_param, Param} -> {ok, roundabout@parameter:full(Param)}; {seg_lit, _} -> {error, nil} end. -file("src/roundabout/generate.gleam", 461). ?DOC(false). -spec get_function_arguments( list(roundabout@node:info()), list(roundabout@node:segment()), roundabout@node:info() ) -> list(roundabout@node:segment()). get_function_arguments(Ancestors, Acc, Info) -> Current_segments = begin _pipe = Acc, gleam@list:filter_map(_pipe, fun(Segment) -> case Segment of {seg_lit, _} -> {error, <<""/utf8>>}; {seg_param, Param} -> New_name = <<<<(roundabout@type_name:snake( erlang:element(2, Info) ))/binary, "_"/utf8>>/binary, (roundabout@parameter:name(Param))/binary>>, gleam@result:'try'( roundabout@parameter:new( New_name, roundabout@parameter:kind(Param) ), fun(New_param) -> _pipe@1 = {seg_param, New_param}, {ok, _pipe@1} end ) end end) end, New_segments = begin _pipe@2 = erlang:element(3, Info), gleam@list:filter_map(_pipe@2, fun(Segment@1) -> case Segment@1 of {seg_lit, _} -> {error, nil}; {seg_param, Param@1} -> New_name@1 = (<<<<(roundabout@type_name:snake( erlang:element(2, Info) ))/binary, "_"/utf8>>/binary, (roundabout@parameter:name(Param@1))/binary>>), gleam@result:'try'( begin _pipe@3 = roundabout@parameter:new( New_name@1, roundabout@parameter:kind(Param@1) ), gleam@result:replace_error(_pipe@3, nil) end, fun(New_param@1) -> {ok, {seg_param, New_param@1}} end ) end end) end, Next_acc = lists:append(New_segments, Current_segments), case Ancestors of [Next_ancestor | Rest_ancestors] -> get_function_arguments(Rest_ancestors, Next_acc, Next_ancestor); _ -> Next_acc end. -file("src/roundabout/generate.gleam", 569). -spec get_function_name_do( list(binary()), list(roundabout@node:info()), roundabout@node:info() ) -> list(binary()). get_function_name_do(Collected, Ancestors, Info) -> Next = gleam@list:prepend( Collected, roundabout@type_name:snake(erlang:element(2, Info)) ), case Ancestors of [Next_ancestor | Rest_ancestors] -> get_function_name_do(Next, Rest_ancestors, Next_ancestor); _ -> Next end. -file("src/roundabout/generate.gleam", 563). ?DOC(false). -spec get_function_name(list(roundabout@node:info()), roundabout@node:info()) -> binary(). get_function_name(Ancestors, Info) -> _pipe = get_function_name_do([], Ancestors, Info), _pipe@1 = gleam@list:filter(_pipe, fun(Seg) -> Seg /= <<""/utf8>> end), gleam@string:join(_pipe@1, <<"_"/utf8>>). -file("src/roundabout/generate.gleam", 590). -spec get_type_name_do( list(binary()), list(roundabout@node:info()), roundabout@node:info() ) -> list(binary()). get_type_name_do(Collected, Ancestors, Info) -> Next = gleam@list:prepend( Collected, roundabout@type_name:name(erlang:element(2, Info)) ), case Ancestors of [Next_ancestor | Rest_ancestors] -> get_type_name_do(Next, Rest_ancestors, Next_ancestor); _ -> Next end. -file("src/roundabout/generate.gleam", 585). ?DOC(false). -spec get_type_name(list(roundabout@node:info()), roundabout@node:info()) -> binary(). get_type_name(Ancestors, Info) -> _pipe = get_type_name_do([], Ancestors, Info), gleam@string:join(_pipe, <<""/utf8>>). -file("src/roundabout/generate.gleam", 517). -spec generate_route_helper_body( list(roundabout@node:info()), list(binary()), roundabout@node:info() ) -> list(binary()). generate_route_helper_body(Ancestors, Acc, Info) -> Params = begin _pipe = erlang:element(3, Info), _pipe@2 = gleam@list:filter_map(_pipe, fun(Segment) -> case Segment of {seg_lit, _} -> {error, nil}; {seg_param, Param} -> _pipe@1 = (<<<<(roundabout@type_name:snake( erlang:element(2, Info) ))/binary, "_"/utf8>>/binary, (roundabout@parameter:name(Param))/binary>>), {ok, _pipe@1} end end), (fun(Entries) -> case gleam@list:is_empty(Acc) of true -> Entries; false -> lists:append(Entries, [<<"_"/utf8>>]) end end)(_pipe@2) end, Params@1 = case gleam@list:is_empty(Params) of true -> <<""/utf8>>; false -> <<<<"("/utf8, (gleam@string:join(Params, <<", "/utf8>>))/binary>>/binary, ")"/utf8>> end, Type_name = get_type_name(Ancestors, Info), New_line = <>, Next_acc = case Type_name of <<""/utf8>> -> Acc; _ -> lists:append(Acc, [New_line]) end, case Ancestors of [Next_ancestor | Rest_ancestors] -> generate_route_helper_body(Rest_ancestors, Next_acc, Next_ancestor); _ -> Next_acc end. -file("src/roundabout/generate.gleam", 601). -spec get_route_name(list(roundabout@node:info()), roundabout@node:info()) -> binary(). get_route_name(Ancestors, Info) -> <<(get_type_name(Ancestors, Info))/binary, "Route"/utf8>>. -file("src/roundabout/generate.gleam", 72). ?DOC( " Generate one type variant e.g.\n" "\n" " ```\n" " User(user_id: Int, sub: UserRoute)\n" " ```\n" ). -spec generate_type_variant( list(roundabout@node:info()), roundabout@node:node_() ) -> binary(). generate_type_variant(Ancestors, Node) -> Type_name = get_type_name(Ancestors, erlang:element(2, Node)), Sub = case gleam@list:is_empty(erlang:element(3, Node)) of true -> none; false -> {some, <<"sub: "/utf8, (get_route_name(Ancestors, erlang:element(2, Node)))/binary>>} end, Params = begin _pipe = erlang:element(3, erlang:element(2, Node)), _pipe@1 = gleam@list:filter_map( _pipe, fun generate_type_variant_param/1 ), (fun(Items) -> case Sub of none -> Items; {some, Sub@1} -> lists:append(Items, [Sub@1]) end end)(_pipe@1) end, Params@1 = case gleam@list:is_empty(Params) of true -> <<""/utf8>>; false -> <<<<"("/utf8, (gleam@string:join(Params, <<", "/utf8>>))/binary>>/binary, ")"/utf8>> end, <<<<" "/utf8, Type_name/binary>>/binary, Params@1/binary>>. -file("src/roundabout/generate.gleam", 620). ?DOC(false). -spec generate_utils() -> binary(). generate_utils() -> <<" fn with_int(str: String, fun) { int.parse(str) |> result.try(fun) } "/utf8>>. -file("src/roundabout/generate.gleam", 172). -spec generate_segments_to_route_case( list(roundabout@node:info()), roundabout@node:node_() ) -> binary(). generate_segments_to_route_case(Ancestors, Node) -> Matched_params = begin _pipe = erlang:element(3, erlang:element(2, Node)), _pipe@1 = gleam@list:map(_pipe, fun(Segment) -> case Segment of {seg_lit, Value} -> <<<<"\""/utf8, (roundabout@constant:value(Value))/binary>>/binary, "\""/utf8>>; {seg_param, Param} -> roundabout@parameter:name(Param) end end), gleam@string:join(_pipe@1, <<", "/utf8>>) end, Matched_params@1 = case gleam@list:is_empty(erlang:element(3, Node)) of true -> Matched_params; false -> <> end, Left = <<<<"["/utf8, Matched_params@1/binary>>/binary, "]"/utf8>>, Match_right_inner = begin _pipe@2 = erlang:element(3, erlang:element(2, Node)), _pipe@3 = gleam@list:filter_map(_pipe@2, fun(Seg) -> case Seg of {seg_lit, _} -> {error, nil}; {seg_param, Param@1} -> {ok, roundabout@parameter:name(Param@1)} end end), _pipe@4 = (fun(Params) -> case gleam@list:is_empty(erlang:element(3, Node)) of true -> Params; false -> lists:append(Params, [<<"sub"/utf8>>]) end end)(_pipe@3), gleam@string:join(_pipe@4, <<", "/utf8>>) end, Match_right_inner@2 = case Match_right_inner of <<""/utf8>> -> <<""/utf8>>; Match_right_inner@1 -> <<<<"("/utf8, Match_right_inner@1/binary>>/binary, ")"/utf8>> end, Right = case gleam@list:is_empty(erlang:element(3, Node)) of true -> <<<<(get_type_name(Ancestors, erlang:element(2, Node)))/binary, Match_right_inner@2/binary>>/binary, " |> Ok"/utf8>>; false -> Fn_name = <<(get_function_name(Ancestors, erlang:element(2, Node)))/binary, "_segments_to_route"/utf8>>, <<<<<<< result.map(fn(sub) { "/utf8>>/binary, (get_type_name(Ancestors, erlang:element(2, Node)))/binary>>/binary, Match_right_inner@2/binary>>/binary, " })"/utf8>> end, Right@1 = gleam@list:fold( erlang:element(3, erlang:element(2, Node)), Right, fun(Acc, Segment@1) -> case Segment@1 of {seg_lit, _} -> Acc; {seg_param, Param@2} -> case roundabout@parameter:kind(Param@2) of str -> Acc; int -> Name = roundabout@parameter:name(Param@2), <<<<<<<<<<<<"with_int("/utf8, Name/binary>>/binary, ", fn("/utf8>>/binary, Name/binary>>/binary, ") { "/utf8>>/binary, Acc/binary>>/binary, " })"/utf8>> end end end ), <<<<<<<<" "/utf8, " "/utf8>>/binary, Left/binary>>/binary, " -> "/utf8>>/binary, Right@1/binary>>. -file("src/roundabout/generate.gleam", 298). -spec generate_route_to_path_case( list(roundabout@node:info()), roundabout@node:node_() ) -> binary(). generate_route_to_path_case(Ancestors, Node) -> Variant_params = begin _pipe = erlang:element(3, erlang:element(2, Node)), _pipe@3 = gleam@list:filter_map(_pipe, fun(Seg) -> case Seg of {seg_lit, _} -> {error, nil}; {seg_param, Param} -> _pipe@1 = Param, _pipe@2 = roundabout@parameter:name(_pipe@1), {ok, _pipe@2} end end), (fun(Items) -> case gleam@list:is_empty(erlang:element(3, Node)) of true -> Items; false -> lists:append(Items, [<<"sub"/utf8>>]) end end)(_pipe@3) end, Variant_params_str = case gleam@list:is_empty(Variant_params) of true -> <<""/utf8>>; false -> <<<<"("/utf8, (gleam@string:join(Variant_params, <<", "/utf8>>))/binary>>/binary, ")"/utf8>> end, Path = begin _pipe@4 = erlang:element(3, erlang:element(2, Node)), _pipe@5 = gleam@list:map(_pipe@4, fun(Seg@1) -> case Seg@1 of {seg_lit, Value} -> <<<<"\""/utf8, (roundabout@constant:value(Value))/binary>>/binary, "/\""/utf8>>; {seg_param, Param@1} -> Name = roundabout@parameter:name(Param@1), case roundabout@parameter:kind(Param@1) of str -> Name; int -> <<<<"int.to_string("/utf8, Name/binary>>/binary, ")"/utf8>> end end end), gleam@string:join(_pipe@5, <<" <> "/utf8>>) end, Path@1 = case gleam@list:is_empty(erlang:element(3, Node)) of true -> Path; false -> <<<<< "/utf8>>/binary, (get_function_name(Ancestors, erlang:element(2, Node)))/binary>>/binary, "_route_to_path(sub)"/utf8>> end, Path@3 = case Path@1 of <<""/utf8>> -> <<"\"/\""/utf8>>; Path@2 -> <<"\"/\" <> "/utf8, Path@2/binary>> end, <<<<<<<<<<" "/utf8, " "/utf8>>/binary, (get_type_name(Ancestors, erlang:element(2, Node)))/binary>>/binary, Variant_params_str/binary>>/binary, " -> "/utf8>>/binary, Path@3/binary>>. -file("src/roundabout/generate.gleam", 16). ?DOC(false). -spec generate_imports() -> binary(). generate_imports() -> <<(begin _pipe = [<<"import gleam/int"/utf8>>, <<"import gleam/result"/utf8>>], gleam@string:join(_pipe, <<"\n"/utf8>>) end)/binary, "\n\n"/utf8>>. -file("src/roundabout/generate.gleam", 54). ?DOC(false). -spec generate_type(list(roundabout@node:info()), roundabout@node:node_()) -> binary(). generate_type(Ancestors, Node) -> Next_ancestors = gleam@list:prepend(Ancestors, erlang:element(2, Node)), Variants = begin _pipe = erlang:element(3, Node), _pipe@1 = gleam@list:map( _pipe, fun(_capture) -> generate_type_variant(Next_ancestors, _capture) end ), gleam@string:join(_pipe@1, <<"\n"/utf8>>) end, Route_name = get_route_name(Ancestors, erlang:element(2, Node)), <<<<<<<<<<"pub type "/utf8, Route_name/binary>>/binary, " {\n"/utf8>>/binary, Variants/binary>>/binary, "\n}"/utf8>>/binary, "\n\n"/utf8>>. -file("src/roundabout/generate.gleam", 35). ?DOC(false). -spec generate_type_rec(list(roundabout@node:info()), roundabout@node:node_()) -> {ok, binary()} | {error, nil}. generate_type_rec(Ancestors, Node) -> case gleam@list:is_empty(erlang:element(3, Node)) of true -> {error, nil}; false -> Next_ancestors = gleam@list:prepend( Ancestors, erlang:element(2, Node) ), Sub_types = begin _pipe = erlang:element(3, Node), _pipe@1 = gleam@list:filter_map( _pipe, fun(Node@1) -> generate_type_rec(Next_ancestors, Node@1) end ), gleam@string:join(_pipe@1, <<""/utf8>>) end, Out = <<(generate_type(Ancestors, Node))/binary, Sub_types/binary>>, {ok, Out} end. -file("src/roundabout/generate.gleam", 138). ?DOC(false). -spec generate_segments_to_route( list(roundabout@node:info()), roundabout@node:node_() ) -> binary(). generate_segments_to_route(Ancestors, Node) -> Next_ancestors = gleam@list:prepend(Ancestors, erlang:element(2, Node)), Segments_to_route_cases = begin _pipe = erlang:element(3, Node), _pipe@1 = gleam@list:map( _pipe, fun(_capture) -> generate_segments_to_route_case(Next_ancestors, _capture) end ), gleam@string:join(_pipe@1, <<"\n"/utf8>>) end, Function_name = begin _pipe@2 = [get_function_name(Ancestors, erlang:element(2, Node)), <<"segments_to_route"/utf8>>], _pipe@3 = gleam@list:filter( _pipe@2, fun(Name) -> not gleam@string:is_empty(Name) end ), gleam@string:join(_pipe@3, <<"_"/utf8>>) end, Type_name = get_type_name(Ancestors, erlang:element(2, Node)), Pub_prefix = case gleam@list:is_empty(Ancestors) of true -> <<"pub "/utf8>>; false -> <<""/utf8>> end, <<<<<<<<<<<<<<<<<<<<<>/binary, Function_name/binary>>/binary, "(segments: List(String)) -> Result("/utf8>>/binary, Type_name/binary>>/binary, "Route, Nil) {\n"/utf8>>/binary, " case segments {\n"/utf8>>/binary, Segments_to_route_cases/binary>>/binary, "\n _ -> Error(Nil)\n"/utf8>>/binary, " }\n"/utf8>>/binary, "}"/utf8>>/binary, "\n\n"/utf8>>. -file("src/roundabout/generate.gleam", 118). ?DOC(false). -spec generate_segments_to_route_rec( list(roundabout@node:info()), roundabout@node:node_() ) -> {ok, binary()} | {error, nil}. generate_segments_to_route_rec(Ancestors, Node) -> case gleam@list:is_empty(erlang:element(3, Node)) of true -> {error, nil}; false -> Next_ancestors = gleam@list:prepend( Ancestors, erlang:element(2, Node) ), Sub_types = begin _pipe = gleam@list:filter_map( erlang:element(3, Node), fun(Sub) -> generate_segments_to_route_rec(Next_ancestors, Sub) end ), gleam@string:join(_pipe, <<""/utf8>>) end, Out = <<(generate_segments_to_route(Ancestors, Node))/binary, Sub_types/binary>>, {ok, Out} end. -file("src/roundabout/generate.gleam", 266). ?DOC(false). -spec generate_route_to_path( list(roundabout@node:info()), roundabout@node:node_() ) -> binary(). generate_route_to_path(Ancestors, Node) -> Next_ancestors = gleam@list:prepend(Ancestors, erlang:element(2, Node)), Route_to_path_cases = begin _pipe = erlang:element(3, Node), _pipe@1 = gleam@list:map( _pipe, fun(_capture) -> generate_route_to_path_case(Next_ancestors, _capture) end ), gleam@string:join(_pipe@1, <<"\n"/utf8>>) end, Function_name = begin _pipe@2 = [get_function_name(Ancestors, erlang:element(2, Node)), <<"route_to_path"/utf8>>], _pipe@3 = gleam@list:filter( _pipe@2, fun(Name) -> not gleam@string:is_empty(Name) end ), gleam@string:join(_pipe@3, <<"_"/utf8>>) end, Pub_prefix = case gleam@list:is_empty(Ancestors) of true -> <<"pub "/utf8>>; false -> <<""/utf8>> end, <<<<<<<<<<<<<<<<<<<<<>/binary, Function_name/binary>>/binary, "(route: "/utf8>>/binary, (get_type_name( Ancestors, erlang:element(2, Node) ))/binary>>/binary, "Route) -> String {\n"/utf8>>/binary, " "/utf8>>/binary, "case route {\n"/utf8>>/binary, Route_to_path_cases/binary>>/binary, "\n }\n"/utf8>>/binary, "}"/utf8>>/binary, "\n\n"/utf8>>. -file("src/roundabout/generate.gleam", 248). ?DOC(false). -spec generate_route_to_path_rec( list(roundabout@node:info()), roundabout@node:node_() ) -> {ok, binary()} | {error, nil}. generate_route_to_path_rec(Ancestors, Node) -> case gleam@list:is_empty(erlang:element(3, Node)) of true -> {error, nil}; false -> Next_ancestors = gleam@list:prepend( Ancestors, erlang:element(2, Node) ), Sub_types = begin _pipe = gleam@list:filter_map( erlang:element(3, Node), fun(Node@1) -> generate_route_to_path_rec(Next_ancestors, Node@1) end ), gleam@string:join(_pipe, <<"\n"/utf8>>) end, _pipe@1 = (<<(generate_route_to_path(Ancestors, Node))/binary, Sub_types/binary>>), {ok, _pipe@1} end. -file("src/roundabout/generate.gleam", 385). -spec generate_route_helper( list(roundabout@node:info()), roundabout@node:node_() ) -> binary(). generate_route_helper(Ancestors, Cont) -> Function_name = <<(get_function_name(Ancestors, erlang:element(2, Cont)))/binary, "_route"/utf8>>, Function_arguments = begin _pipe = get_function_arguments(Ancestors, [], erlang:element(2, Cont)), _pipe@2 = gleam@list:filter_map(_pipe, fun(Segment) -> case Segment of {seg_lit, _} -> {error, nil}; {seg_param, Param} -> _pipe@1 = roundabout@parameter:full(Param), {ok, _pipe@1} end end), gleam@string:join(_pipe@2, <<", "/utf8>>) end, Body = <<" "/utf8, (begin _pipe@3 = generate_route_helper_body( Ancestors, [], erlang:element(2, Cont) ), gleam@string:join(_pipe@3, <<"\n |> "/utf8>>) end)/binary>>, <<<<<<<<<<<<<<<<"pub fn "/utf8, Function_name/binary>>/binary, "("/utf8>>/binary, Function_arguments/binary>>/binary, ") -> Route {\n"/utf8>>/binary, Body/binary>>/binary, "\n"/utf8>>/binary, "}"/utf8>>/binary, "\n\n"/utf8>>. -file("src/roundabout/generate.gleam", 414). -spec generate_path_helper( list(roundabout@node:info()), roundabout@node:node_() ) -> binary(). generate_path_helper(Ancestors, Cont) -> Function_name_prefix = get_function_name(Ancestors, erlang:element(2, Cont)), Route_function_name = <>, Path_function_name = <>, Function_arguments = get_function_arguments( Ancestors, [], erlang:element(2, Cont) ), This_function_arguments = begin _pipe = Function_arguments, _pipe@2 = gleam@list:filter_map(_pipe, fun(Segment) -> case Segment of {seg_lit, _} -> {error, nil}; {seg_param, Name} -> _pipe@1 = roundabout@parameter:full(Name), {ok, _pipe@1} end end), gleam@string:join(_pipe@2, <<", "/utf8>>) end, Callee_arguments = begin _pipe@3 = Function_arguments, _pipe@4 = gleam@list:filter_map( _pipe@3, fun(Segment@1) -> case Segment@1 of {seg_lit, _} -> {error, nil}; {seg_param, Name@1} -> {ok, roundabout@parameter:name(Name@1)} end end ), gleam@string:join(_pipe@4, <<", "/utf8>>) end, Body = <<<<<<<<<<" "/utf8, Route_function_name/binary>>/binary, "("/utf8>>/binary, Callee_arguments/binary>>/binary, ")\n"/utf8>>/binary, " |> route_to_path"/utf8>>, <<<<<<<<<<<<<<<<"pub fn "/utf8, Path_function_name/binary>>/binary, "("/utf8>>/binary, This_function_arguments/binary>>/binary, ") -> String {\n"/utf8>>/binary, Body/binary>>/binary, "\n"/utf8>>/binary, "}"/utf8>>/binary, "\n\n"/utf8>>. -file("src/roundabout/generate.gleam", 380). ?DOC(" helpers\n"). -spec generate_helpers(list(roundabout@node:info()), roundabout@node:node_()) -> binary(). generate_helpers(Ancestors, Node) -> <<(generate_route_helper(Ancestors, Node))/binary, (generate_path_helper(Ancestors, Node))/binary>>. -file("src/roundabout/generate.gleam", 363). ?DOC(false). -spec generate_helpers_rec( list(roundabout@node:info()), roundabout@node:node_() ) -> binary(). generate_helpers_rec(Ancestors, Node) -> case gleam@list:is_empty(erlang:element(3, Node)) of true -> generate_helpers(Ancestors, Node); false -> Next_ancestors = gleam@list:prepend( Ancestors, erlang:element(2, Node) ), _pipe = gleam@list:map( erlang:element(3, Node), fun(Node@1) -> generate_helpers_rec(Next_ancestors, Node@1) end ), gleam@string:join(_pipe, <<""/utf8>>) end.