Skip to main content

src/proute@names.erl

-module(proute@names).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/proute/names.gleam").
-export([snake_case/1, pascal_case/1, page_alias/1, helper_name/1, module_path_from_file/1, module_path_from_output_dir/1, is_valid_type_name/1, is_valid_label/1, is_valid_module_segment/1, is_valid_module_path/1, parse_type_reference/1]).
-export_type([type_reference/0]).

-type type_reference() :: {type_reference, binary(), binary()}.

-file("src/proute/names.gleam", 10).
-spec snake_case(binary()) -> binary().
snake_case(Value) ->
    justin:snake_case(Value).

-file("src/proute/names.gleam", 14).
-spec pascal_case(binary()) -> binary().
pascal_case(Value) ->
    _pipe = Value,
    _pipe@1 = gleam@string:replace(_pipe, <<"/"/utf8>>, <<"_"/utf8>>),
    justin:pascal_case(_pipe@1).

-file("src/proute/names.gleam", 20).
-spec page_alias(binary()) -> binary().
page_alias(Constructor) ->
    _pipe = Constructor,
    _pipe@1 = snake_case(_pipe),
    (fun(Name) -> <<Name/binary, "_page"/utf8>> end)(_pipe@1).

-file("src/proute/names.gleam", 26).
-spec helper_name(binary()) -> binary().
helper_name(Constructor) ->
    snake_case(Constructor).

-file("src/proute/names.gleam", 101).
-spec module_path_from_src_path(binary()) -> {ok, binary()} | {error, nil}.
module_path_from_src_path(Path) ->
    case gleam_stdlib:string_starts_with(Path, <<"src/"/utf8>>) of
        true ->
            {ok, gleam@string:drop_start(Path, 4)};

        false ->
            case begin
                _pipe = gleam@string:split(Path, <<"/src/"/utf8>>),
                gleam@list:last(_pipe)
            end of
                {ok, Module_path} when (Module_path =/= Path) andalso (Module_path =/= <<""/utf8>>) ->
                    {ok, Module_path};

                _ ->
                    {error, nil}
            end
    end.

-file("src/proute/names.gleam", 117).
-spec drop_suffix(binary(), binary()) -> binary().
drop_suffix(Value, Suffix) ->
    case gleam_stdlib:string_ends_with(Value, Suffix) of
        true ->
            gleam@string:drop_end(Value, string:length(Suffix));

        false ->
            Value
    end.

-file("src/proute/names.gleam", 113).
-spec normalize_path(binary()) -> binary().
normalize_path(Path) ->
    gleam@string:replace(Path, <<"\\"/utf8>>, <<"/"/utf8>>).

-file("src/proute/names.gleam", 30).
-spec module_path_from_file(binary()) -> {ok, binary()} | {error, nil}.
module_path_from_file(Path) ->
    _pipe = Path,
    _pipe@1 = normalize_path(_pipe),
    _pipe@2 = drop_suffix(_pipe@1, <<".gleam"/utf8>>),
    module_path_from_src_path(_pipe@2).

-file("src/proute/names.gleam", 37).
-spec module_path_from_output_dir(binary()) -> {ok, binary()} | {error, nil}.
module_path_from_output_dir(Path) ->
    _pipe = Path,
    _pipe@1 = normalize_path(_pipe),
    module_path_from_src_path(_pipe@1).

-file("src/proute/names.gleam", 136).
-spec is_reserved_word(binary()) -> boolean().
is_reserved_word(Name) ->
    _pipe = [<<"as"/utf8>>,
        <<"assert"/utf8>>,
        <<"case"/utf8>>,
        <<"const"/utf8>>,
        <<"external"/utf8>>,
        <<"fn"/utf8>>,
        <<"if"/utf8>>,
        <<"import"/utf8>>,
        <<"let"/utf8>>,
        <<"opaque"/utf8>>,
        <<"panic"/utf8>>,
        <<"pub"/utf8>>,
        <<"todo"/utf8>>,
        <<"type"/utf8>>,
        <<"use"/utf8>>],
    gleam@list:contains(_pipe, Name).

-file("src/proute/names.gleam", 132).
-spec is_digit(binary()) -> boolean().
is_digit(Char) ->
    gleam_stdlib:contains_string(<<"0123456789"/utf8>>, Char).

-file("src/proute/names.gleam", 128).
-spec is_uppercase_ascii(binary()) -> boolean().
is_uppercase_ascii(Char) ->
    gleam_stdlib:contains_string(<<"ABCDEFGHIJKLMNOPQRSTUVWXYZ"/utf8>>, Char).

-file("src/proute/names.gleam", 124).
-spec is_lowercase_ascii(binary()) -> boolean().
is_lowercase_ascii(Char) ->
    gleam_stdlib:contains_string(<<"abcdefghijklmnopqrstuvwxyz"/utf8>>, Char).

-file("src/proute/names.gleam", 84).
-spec is_valid_type_name(binary()) -> boolean().
is_valid_type_name(Name) ->
    Chars = gleam@string:to_graphemes(Name),
    case Chars of
        [] ->
            false;

        [First | Rest] ->
            (is_uppercase_ascii(First) andalso gleam@list:all(
                Rest,
                fun(Char) ->
                    ((is_lowercase_ascii(Char) orelse is_uppercase_ascii(Char))
                    orelse is_digit(Char))
                    orelse (Char =:= <<"_"/utf8>>)
                end
            ))
            andalso not is_reserved_word(Name)
    end.

-file("src/proute/names.gleam", 70).
-spec is_valid_label(binary()) -> boolean().
is_valid_label(Name) ->
    Chars = gleam@string:to_graphemes(Name),
    case Chars of
        [] ->
            false;

        [First | Rest] ->
            (is_lowercase_ascii(First) andalso gleam@list:all(
                Rest,
                fun(Char) ->
                    (is_lowercase_ascii(Char) orelse is_digit(Char)) orelse (Char
                    =:= <<"_"/utf8>>)
                end
            ))
            andalso not is_reserved_word(Name)
    end.

-file("src/proute/names.gleam", 66).
-spec is_valid_module_segment(binary()) -> boolean().
is_valid_module_segment(Segment) ->
    is_valid_label(Segment).

-file("src/proute/names.gleam", 57).
-spec is_valid_module_path(binary()) -> boolean().
is_valid_module_path(Path) ->
    (Path /= <<""/utf8>>) andalso begin
        _pipe = Path,
        _pipe@1 = gleam@string:split(_pipe, <<"/"/utf8>>),
        gleam@list:all(_pipe@1, fun is_valid_module_segment/1)
    end.

-file("src/proute/names.gleam", 43).
-spec parse_type_reference(binary()) -> {ok, type_reference()} | {error, nil}.
parse_type_reference(Value) ->
    gleam@result:'try'(case gleam@string:split(Value, <<"."/utf8>>) of
            [Module_path, Type_name] ->
                {ok, {Module_path, Type_name}};

            _ ->
                {error, nil}
        end, fun(Parts) ->
            {Module_path@1, Type_name@1} = Parts,
            case is_valid_module_path(Module_path@1) andalso is_valid_type_name(
                Type_name@1
            ) of
                true ->
                    {ok, {type_reference, Module_path@1, Type_name@1}};

                false ->
                    {error, nil}
            end
        end).