Skip to main content

src/proute@generate@page_input.erl

-module(proute@generate@page_input).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/proute/generate/page_input.gleam").
-export([generate/1]).

-file("src/proute/generate/page_input.gleam", 97).
-spec route_fields(list(proute@discover:route_param())) -> binary().
route_fields(Params) ->
    _pipe = Params,
    _pipe@1 = gleam@list:map(
        _pipe,
        fun(Param) ->
            <<<<(erlang:element(2, Param))/binary, ": "/utf8>>/binary,
                (erlang:element(3, Param))/binary>>
        end
    ),
    gleam@string:join(_pipe@1, <<", "/utf8>>).

-file("src/proute/generate/page_input.gleam", 90).
-spec source_file_label(binary()) -> binary().
source_file_label(Source_file) ->
    case gleam@string:split(Source_file, <<"/pages/"/utf8>>) of
        [_, Relative] ->
            <<"pages/"/utf8, Relative/binary>>;

        _ ->
            Source_file
    end.

-file("src/proute/generate/page_input.gleam", 84).
-spec route_page_label(proute@discover:page_route()) -> binary().
route_page_label(Route) ->
    _pipe = erlang:element(7, Route),
    _pipe@1 = gleam@string:replace(_pipe, <<"\\"/utf8>>, <<"/"/utf8>>),
    source_file_label(_pipe@1).

-file("src/proute/generate/page_input.gleam", 69).
-spec route_params_record(proute@discover:page_route()) -> binary().
route_params_record(Route) ->
    <<<<<<<<<<<<<<<<<<<<<<"/// Route parameters for `"/utf8,
                                                (route_page_label(Route))/binary>>/binary,
                                            "`.\n"/utf8>>/binary,
                                        "pub type "/utf8>>/binary,
                                    (erlang:element(3, Route))/binary>>/binary,
                                "RouteParams {\n"/utf8>>/binary,
                            "  "/utf8>>/binary,
                        (erlang:element(3, Route))/binary>>/binary,
                    "RouteParams("/utf8>>/binary,
                (route_fields(erlang:element(6, Route)))/binary>>/binary,
            ")\n"/utf8>>/binary,
        "}\n"/utf8>>.

-file("src/proute/generate/page_input.gleam", 63).
-spec route_params(list(proute@discover:page_route())) -> binary().
route_params(Routes) ->
    _pipe = Routes,
    _pipe@1 = gleam@list:map(_pipe, fun route_params_record/1),
    gleam@string:join(_pipe@1, <<"\n"/utf8>>).

-file("src/proute/generate/page_input.gleam", 49).
-spec query_params() -> binary().
query_params() ->
    <<<<<<<<<<<<<<<<<<<<"/// Query parameters parsed from the current URL.\n"/utf8,
                                            "///\n"/utf8>>/binary,
                                        "/// Query strings are open-ended and can repeat keys, so generated code stores\n"/utf8>>/binary,
                                    "/// the decoded pairs instead of forcing a fixed record shape.\n"/utf8>>/binary,
                                "pub type QueryParams {\n"/utf8>>/binary,
                            "  QueryParams(values: List(#(String, String)))\n"/utf8>>/binary,
                        "}\n\n"/utf8>>/binary,
                    "/// Build an empty query param bag.\n"/utf8>>/binary,
                "pub fn empty_query_params() -> QueryParams {\n"/utf8>>/binary,
            "  QueryParams([])\n"/utf8>>/binary,
        "}\n"/utf8>>.

-file("src/proute/generate/page_input.gleam", 31).
-spec header_body(binary(), list(proute@discover:page_route())) -> binary().
header_body(Mount_name, Dynamic_routes) ->
    case Dynamic_routes of
        [] ->
            <<<<<<<<"//// The "/utf8, Mount_name/binary>>/binary,
                        " routes currently have no dynamic route params, but page dispatch\n"/utf8>>/binary,
                    "//// still receives query params through the same generated input convention as\n"/utf8>>/binary,
                "//// other mounts.\n"/utf8>>;

        _ ->
            <<<<"//// Pages receive route parameters and query parameters as named values instead\n"/utf8,
                    "//// of a growing list of positional arguments. Nested dynamic routes can add\n"/utf8>>/binary,
                "//// fields to the route params record without changing the meaning of call sites.\n"/utf8>>
    end.

-file("src/proute/generate/page_input.gleam", 18).
-spec header(binary(), list(proute@discover:page_route())) -> binary().
header(Mount_name, Dynamic_routes) ->
    <<<<<<<<<<<<"//// Generated page input types. Do not edit.\n"/utf8,
                            "////\n"/utf8>>/binary,
                        "//// mount: "/utf8>>/binary,
                    Mount_name/binary>>/binary,
                "\n"/utf8>>/binary,
            "////\n"/utf8>>/binary,
        (header_body(Mount_name, Dynamic_routes))/binary>>.

-file("src/proute/generate/page_input.gleam", 5).
-spec generate(proute@discover:mount_routes()) -> binary().
generate(Mount_routes) ->
    {mount_routes, Mount, Routes} = Mount_routes,
    Dynamic_routes = begin
        _pipe = Routes,
        gleam@list:filter(
            _pipe,
            fun(Route) -> erlang:element(6, Route) /= [] end
        )
    end,
    _pipe@1 = [header(erlang:element(2, Mount), Dynamic_routes),
        query_params(),
        route_params(Dynamic_routes)],
    _pipe@2 = gleam@list:filter(
        _pipe@1,
        fun(Section) -> Section /= <<""/utf8>> end
    ),
    gleam@string:join(_pipe@2, <<"\n"/utf8>>).