Skip to main content

src/iconify_lustre@add.erl

-module(iconify_lustre@add).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/iconify_lustre/add.gleam").
-export([main/0]).

-file("src/iconify_lustre/add.gleam", 73).
-spec process(list(binary()), internal@config:configuration()) -> nil.
process(Arguments, Config) ->
    case Arguments of
        [<<"-m"/utf8>>, Module | Rest] ->
            process(
                Rest,
                {configuration,
                    erlang:element(2, Config),
                    erlang:element(3, Config),
                    Module}
            );

        [<<"-s"/utf8>>, Iconset | Rest@1] ->
            process(
                Rest@1,
                {configuration,
                    erlang:element(2, Config),
                    Iconset,
                    erlang:element(4, Config)}
            );

        [Icon | Rest@2] ->
            internal@iconify:add(Icon, Config),
            process(Rest@2, Config);

        [] ->
            nil
    end.

-file("src/iconify_lustre/add.gleam", 60).
-spec invalid_configuration(internal@config:configuration_error()) -> nil.
invalid_configuration(Error) ->
    Description = case Error of
        {file_error, File_error} ->
            simplifile:describe_error(File_error);

        {toml_error, {unexpected, Got, Expected}} ->
            <<<<<<<<"Expected: '"/utf8, Expected/binary>>/binary,
                        "', but got:'"/utf8>>/binary,
                    Got/binary>>/binary,
                "'"/utf8>>;

        {toml_error, {key_already_in_use, Key}} ->
            <<<<"Key '"/utf8, (gleam@string:join(Key, <<"."/utf8>>))/binary>>/binary,
                "' already set"/utf8>>
    end,
    gleam_stdlib:println(<<"Error found in your gleam.toml file:"/utf8>>),
    gleam_stdlib:println(<<"  "/utf8, Description/binary>>).

-file("src/iconify_lustre/add.gleam", 29).
-spec help() -> nil.
help() ->
    _pipe = [<<"usage: gleam run -m iconify_lustre/add -- [-m module]... [-s set]... <[set/]name>>..."/utf8>>,
        <<""/utf8>>,
        <<<<"  -m module    Specifies the module in which the icon should be added. Defaults to '"/utf8,
                "*"/utf8>>/binary,
            "'."/utf8>>,
        <<"                  If the module ends in a slash (/) it is considered a directory"/utf8>>,
        <<"                  and the actual will have the name of the icon."/utf8>>,
        <<"                  If the module is an asterisk (*) is replaced with the name of"/utf8>>,
        <<"                  the iconset."/utf8>>,
        <<""/utf8>>,
        <<"                  E.G. 'iconify/*/ for icon lucide:test will result in iconify/lucide/test.gleam"/utf8>>,
        <<"                                   with a function test."/utf8>>,
        <<"                  E.G. 'iconify/*  for icon lucide:test will result in iconify/lucide.gleam"/utf8>>,
        <<"                                   with a function test."/utf8>>,
        <<""/utf8>>,
        <<<<"  -s set       Specifies the iconset of the icon. Defaults to '"/utf8,
                "lucide"/utf8>>/binary,
            "'."/utf8>>,
        <<""/utf8>>,
        <<"  [set/]name   The icon with 'name' from the 'set' will be installed. 'set' defaults to 'lucide'."/utf8>>,
        <<""/utf8>>,
        <<"The defaults of 'module' and 'set' can be given in gleam.toml as well:"/utf8>>,
        <<"  [tools.iconify_lustre]"/utf8>>,
        <<"  iconset = \"tabler\""/utf8>>,
        <<"  module = \"iconify/*/\""/utf8>>],
    gleam@list:each(_pipe, fun gleam_stdlib:println/1).

-file("src/iconify_lustre/add.gleam", 25).
-spec is_valid_argument(binary()) -> boolean().
is_valid_argument(Argument) ->
    ((Argument =:= <<"-m"/utf8>>) orelse (Argument =:= <<"-s"/utf8>>)) orelse not gleam_stdlib:string_starts_with(
        Argument,
        <<"-"/utf8>>
    ).

-file("src/iconify_lustre/add.gleam", 14).
-spec main() -> nil.
main() ->
    Config = internal@config:get(<<"lucide"/utf8>>, <<"*"/utf8>>),
    Args = erlang:element(4, argv:load()),
    case {gleam@list:all(Args, fun is_valid_argument/1), Config} of
        {false, _} ->
            help();

        {true, {error, Error}} ->
            invalid_configuration(Error);

        {true, {ok, Config@1}} ->
            process(Args, Config@1)
    end.