src/gleedoc@scan.erl

-module(gleedoc@scan).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/gleedoc/scan.gleam").
-export([public_names/2, module_imports/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/gleedoc/scan.gleam", 84).
-spec if_public(glance:publicity(), binary()) -> {ok, binary()} | {error, nil}.
if_public(Publicity, Name) ->
    case Publicity of
        public ->
            {ok, Name};

        _ ->
            {error, nil}
    end.

-file("src/gleedoc/scan.gleam", 73).
-spec parse_module(binary(), binary()) -> {ok, glance:module_()} |
    {error, snag:snag()}.
parse_module(File_path, Source) ->
    _pipe = Source,
    _pipe@1 = glance:module(_pipe),
    gleam@result:map_error(
        _pipe@1,
        fun(Err) ->
            snag:new(
                <<<<<<"Failed to parse "/utf8, File_path/binary>>/binary,
                        ": "/utf8>>/binary,
                    (gleam@string:inspect(Err))/binary>>
            )
        end
    ).

-file("src/gleedoc/scan.gleam", 13).
?DOC(" Scan a Gleam source file and extract all public definition names.\n").
-spec public_names(binary(), binary()) -> {ok, list(binary())} |
    {error, snag:snag()}.
public_names(File_path, Source) ->
    gleam@result:'try'(
        parse_module(File_path, Source),
        fun(Module) ->
            Pub_functions = begin
                _pipe = erlang:element(6, Module),
                gleam@list:filter_map(
                    _pipe,
                    fun(D) ->
                        if_public(
                            erlang:element(4, erlang:element(3, D)),
                            erlang:element(3, erlang:element(3, D))
                        )
                    end
                )
            end,
            Pub_types = gleam@list:filter_map(
                erlang:element(3, Module),
                fun(D@1) ->
                    if_public(
                        erlang:element(4, erlang:element(3, D@1)),
                        erlang:element(3, erlang:element(3, D@1))
                    )
                end
            ),
            Pub_constants = gleam@list:filter_map(
                erlang:element(5, Module),
                fun(D@2) ->
                    if_public(
                        erlang:element(4, erlang:element(3, D@2)),
                        erlang:element(3, erlang:element(3, D@2))
                    )
                end
            ),
            Pub_names = lists:append([Pub_functions, Pub_types, Pub_constants]),
            {ok, Pub_names}
        end
    ).

-file("src/gleedoc/scan.gleam", 91).
-spec format_unqualified(glance:unqualified_import(), boolean()) -> binary().
format_unqualified(U, Is_type) ->
    Prefix = case Is_type of
        true ->
            <<"type "/utf8>>;

        false ->
            <<""/utf8>>
    end,
    Alias = case erlang:element(3, U) of
        {some, A} ->
            <<" as "/utf8, A/binary>>;

        none ->
            <<""/utf8>>
    end,
    <<<<Prefix/binary, (erlang:element(2, U))/binary>>/binary, Alias/binary>>.

-file("src/gleedoc/scan.gleam", 42).
?DOC(
    " Scan a Gleam source file and return its top-level import statements\n"
    " reconstructed as import strings (e.g. `\"import gleam/order\"`).\n"
).
-spec module_imports(binary(), binary()) -> {ok, list(binary())} |
    {error, snag:snag()}.
module_imports(File_path, Source) ->
    gleam@result:'try'(
        parse_module(File_path, Source),
        fun(Module) ->
            Imports = begin
                _pipe = erlang:element(2, Module),
                gleam@list:map(
                    _pipe,
                    fun(Def) ->
                        Imp = erlang:element(3, Def),
                        Base = <<"import "/utf8,
                            (erlang:element(3, Imp))/binary>>,
                        Unqualified = lists:append(
                            [begin
                                    _pipe@1 = erlang:element(5, Imp),
                                    gleam@list:map(
                                        _pipe@1,
                                        fun(U) ->
                                            format_unqualified(U, true)
                                        end
                                    )
                                end,
                                begin
                                    _pipe@2 = erlang:element(6, Imp),
                                    gleam@list:map(
                                        _pipe@2,
                                        fun(U@1) ->
                                            format_unqualified(U@1, false)
                                        end
                                    )
                                end]
                        ),
                        case Unqualified of
                            [] ->
                                Base;

                            Names ->
                                <<<<<<Base/binary, ".{"/utf8>>/binary,
                                        (gleam@string:join(Names, <<", "/utf8>>))/binary>>/binary,
                                    "}"/utf8>>
                        end
                    end
                )
            end,
            {ok, Imports}
        end
    ).