Packages

A convention-first web framework for Gleam

Current section

Files

Jump to
refrakt src refrakt@cli@routes.erl
Raw

src/refrakt@cli@routes.erl

-module(refrakt@cli@routes).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/refrakt/cli/routes.gleam").
-export([run/0]).
-file("src/refrakt/cli/routes.gleam", 58).
-spec extract_method(binary()) -> binary().
extract_method(Pattern) ->
case gleam_stdlib:contains_string(Pattern, <<"http.Get"/utf8>>) of
true ->
<<"GET"/utf8>>;
false ->
case gleam_stdlib:contains_string(Pattern, <<"http.Post"/utf8>>) of
true ->
<<"POST"/utf8>>;
false ->
case gleam_stdlib:contains_string(
Pattern,
<<"http.Put"/utf8>>
) of
true ->
<<"PUT"/utf8>>;
false ->
case gleam_stdlib:contains_string(
Pattern,
<<"http.Delete"/utf8>>
) of
true ->
<<"DELETE"/utf8>>;
false ->
case gleam_stdlib:contains_string(
Pattern,
<<"http.Patch"/utf8>>
) of
true ->
<<"PATCH"/utf8>>;
false ->
<<"???"/utf8>>
end
end
end
end
end.
-file("src/refrakt/cli/routes.gleam", 81).
-spec extract_path(binary()) -> binary().
extract_path(Pattern) ->
case gleam@string:split_once(Pattern, <<"],"/utf8>>) of
{error, _} ->
<<"/"/utf8>>;
{ok, {List_part, _}} ->
Trimmed = gleam@string:trim(List_part),
case Trimmed of
<<"[]"/utf8>> ->
<<"/"/utf8>>;
_ ->
Inner = begin
_pipe = Trimmed,
gleam@string:drop_start(_pipe, 1)
end,
Segments = begin
_pipe@1 = Inner,
_pipe@2 = gleam@string:split(_pipe@1, <<","/utf8>>),
gleam@list:map(
_pipe@2,
fun(S) ->
S@1 = gleam@string:trim(S),
case gleam_stdlib:string_starts_with(
S@1,
<<"\""/utf8>>
) of
true ->
_pipe@3 = S@1,
gleam@string:replace(
_pipe@3,
<<"\""/utf8>>,
<<""/utf8>>
);
false ->
<<":"/utf8, S@1/binary>>
end
end
)
end,
<<"/"/utf8,
(gleam@string:join(Segments, <<"/"/utf8>>))/binary>>
end
end.
-file("src/refrakt/cli/routes.gleam", 113).
-spec extract_handler_name(binary()) -> binary().
extract_handler_name(Handler) ->
case gleam@string:split_once(Handler, <<"("/utf8>>) of
{ok, {Name, _}} ->
gleam@string:trim(Name);
{error, _} ->
gleam@string:trim(Handler)
end.
-file("src/refrakt/cli/routes.gleam", 121).
-spec pad_right(binary(), integer()) -> binary().
pad_right(S, Width) ->
Padding = Width - string:length(S),
case Padding > 0 of
true ->
<<S/binary, (gleam@string:repeat(<<" "/utf8>>, Padding))/binary>>;
false ->
S
end.
-file("src/refrakt/cli/routes.gleam", 39).
-spec parse_route_line(binary()) -> {ok, binary()} | {error, nil}.
parse_route_line(Line) ->
case gleam@string:split_once(Line, <<" -> "/utf8>>) of
{error, _} ->
{error, nil};
{ok, {Pattern, Handler}} ->
case gleam_stdlib:contains_string(Pattern, <<"http."/utf8>>) of
false ->
{error, nil};
true ->
Method = extract_method(Pattern),
Path = extract_path(Pattern),
Handler_name = extract_handler_name(Handler),
{ok,
<<<<(pad_right(Method, 8))/binary,
(pad_right(Path, 24))/binary>>/binary,
Handler_name/binary>>}
end
end.
-file("src/refrakt/cli/routes.gleam", 27).
-spec extract_routes(binary()) -> list(binary()).
extract_routes(Content) ->
_pipe = Content,
_pipe@1 = gleam@string:split(_pipe, <<"\n"/utf8>>),
gleam@list:filter_map(
_pipe@1,
fun(Line) ->
Trimmed = gleam@string:trim(Line),
case parse_route_line(Trimmed) of
{ok, Route} ->
{ok, Route};
{error, _} ->
{error, nil}
end
end
).
-file("src/refrakt/cli/routes.gleam", 9).
-spec run() -> nil.
run() ->
App = refrakt@cli@project:app_name(),
Router_path = <<<<"src/"/utf8, App/binary>>/binary, "/router.gleam"/utf8>>,
case simplifile:read(Router_path) of
{error, _} ->
gleam_stdlib:println(<<"Could not read "/utf8, Router_path/binary>>);
{ok, Content} ->
Routes = extract_routes(Content),
case Routes of
[] ->
gleam_stdlib:println(<<"No routes found."/utf8>>);
_ ->
gleam@list:each(
Routes,
fun(Route) -> gleam_stdlib:println(Route) end
)
end
end.