src/glipt@internal@project.erl

-module(glipt@internal@project).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/glipt/internal/project.gleam").
-export([find_project_root/1, resolve_project_path/2, read_project_name/1]).

-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.

?MODULEDOC(false).

-file("src/glipt/internal/project.gleam", 5).
?DOC(false).
-spec find_project_root(binary()) -> {ok, binary()} | {error, nil}.
find_project_root(From) ->
    Toml_path = <<From/binary, "/gleam.toml"/utf8>>,
    case simplifile_erl:is_file(Toml_path) of
        {ok, true} ->
            {ok, From};

        _ ->
            Parent = glipt@internal@path:dirname(From),
            case (Parent =:= From) orelse (Parent =:= <<"."/utf8>>) of
                true ->
                    {error, nil};

                false ->
                    find_project_root(Parent)
            end
    end.

-file("src/glipt/internal/project.gleam", 19).
?DOC(false).
-spec resolve_project_path(binary(), binary()) -> {ok, binary()} | {error, nil}.
resolve_project_path(Script_dir, Relative_path) ->
    Absolute = case gleam_stdlib:string_starts_with(Relative_path, <<"/"/utf8>>) of
        true ->
            Relative_path;

        false ->
            <<<<Script_dir/binary, "/"/utf8>>/binary, Relative_path/binary>>
    end,
    find_project_root(Absolute).

-file("src/glipt/internal/project.gleam", 43).
?DOC(false).
-spec find_name_line(list(binary())) -> {ok, binary()} | {error, nil}.
find_name_line(Lines) ->
    case Lines of
        [] ->
            {error, nil};

        [Line | Rest] ->
            Trimmed = gleam@string:trim(Line),
            case gleam_stdlib:string_starts_with(Trimmed, <<"name"/utf8>>) of
                true ->
                    case gleam@string:split_once(Trimmed, <<"="/utf8>>) of
                        {ok, {_, Value}} ->
                            {ok,
                                begin
                                    _pipe = Value,
                                    _pipe@1 = gleam@string:trim(_pipe),
                                    gleam@string:replace(
                                        _pipe@1,
                                        <<"\""/utf8>>,
                                        <<""/utf8>>
                                    )
                                end};

                        {error, nil} ->
                            find_name_line(Rest)
                    end;

                false ->
                    find_name_line(Rest)
            end
    end.

-file("src/glipt/internal/project.gleam", 38).
?DOC(false).
-spec parse_name_from_toml(binary()) -> {ok, binary()} | {error, nil}.
parse_name_from_toml(Content) ->
    Lines = gleam@string:split(Content, <<"\n"/utf8>>),
    find_name_line(Lines).

-file("src/glipt/internal/project.gleam", 30).
?DOC(false).
-spec read_project_name(binary()) -> {ok, binary()} | {error, nil}.
read_project_name(Project_root) ->
    Toml_path = <<Project_root/binary, "/gleam.toml"/utf8>>,
    case simplifile:read(Toml_path) of
        {ok, Content} ->
            parse_name_from_toml(Content);

        {error, _} ->
            {error, nil}
    end.