Skip to main content

src/internal@iconify.erl

-module(internal@iconify).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/internal/iconify.gleam").
-export([add/2]).
-export_type([iconify_error/0]).

-type iconify_error() :: {http_error, binary(), gleam@httpc:http_error()} |
    {invalid_response, binary()} |
    {conversion_error, binary()} |
    {module_error, internal@module:module_error()}.

-file("src/internal/iconify.gleam", 77).
-spec adjust(binary(), binary(), binary()) -> {ok,
        internal@module:module_change()} |
    {error, iconify_error()}.
adjust(Filename, Fn_name, Fn_body) ->
    _pipe = internal@module:adjust(Filename, Fn_name, Fn_body),
    gleam@result:map_error(_pipe, fun(Field@0) -> {module_error, Field@0} end).

-file("src/internal/iconify.gleam", 71).
-spec convert(binary()) -> {ok, binary()} | {error, iconify_error()}.
convert(Svg) ->
    _pipe = Svg,
    _pipe@1 = internal@converter:to_lustre(_pipe),
    gleam@result:map_error(
        _pipe@1,
        fun(Field@0) -> {conversion_error, Field@0} end
    ).

-file("src/internal/iconify.gleam", 56).
-spec download(binary(), binary()) -> {ok, binary()} | {error, iconify_error()}.
download(Set, Name) ->
    Url = <<<<<<<<"https://api.iconify.design/"/utf8, Set/binary>>/binary,
                "/"/utf8>>/binary,
            Name/binary>>/binary,
        ".svg"/utf8>>,
    Request@1 = case gleam@http@request:to(Url) of
        {ok, Request} -> Request;
        _assert_fail ->
            erlang:error(#{gleam_error => let_assert,
                        message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
                        file => <<?FILEPATH/utf8>>,
                        module => <<"internal/iconify"/utf8>>,
                        function => <<"download"/utf8>>,
                        line => 58,
                        value => _assert_fail,
                        start => 1868,
                        'end' => 1908,
                        pattern_start => 1879,
                        pattern_end => 1890})
    end,
    _pipe = Request@1,
    _pipe@1 = gleam@httpc:send(_pipe),
    _pipe@2 = gleam@result:map_error(
        _pipe@1,
        fun(_capture) -> {http_error, Url, _capture} end
    ),
    gleam@result:'try'(
        _pipe@2,
        fun(Response) -> case erlang:element(2, Response) of
                200 ->
                    {ok, erlang:element(4, Response)};

                _ ->
                    {error, {invalid_response, Url}}
            end end
    ).

-file("src/internal/iconify.gleam", 46).
-spec do_add(binary(), binary(), binary()) -> {ok,
        internal@module:module_change()} |
    {error, iconify_error()}.
do_add(Set, Name, Filename) ->
    _pipe = download(Set, Name),
    _pipe@1 = gleam@result:'try'(_pipe, fun convert/1),
    gleam@result:'try'(
        _pipe@1,
        fun(_capture) -> adjust(Filename, Name, _capture) end
    ).

-file("src/internal/iconify.gleam", 86).
-spec normalize(binary()) -> binary().
normalize(Filename) ->
    _pipe = Filename,
    _pipe@1 = string:lowercase(_pipe),
    _pipe@2 = gleam@string:split(_pipe@1, <<""/utf8>>),
    _pipe@3 = gleam@list:map(_pipe@2, fun(Char) -> case Char of
                <<"a"/utf8>> ->
                    Char;

                <<"b"/utf8>> ->
                    Char;

                <<"c"/utf8>> ->
                    Char;

                <<"d"/utf8>> ->
                    Char;

                <<"e"/utf8>> ->
                    Char;

                <<"f"/utf8>> ->
                    Char;

                <<"g"/utf8>> ->
                    Char;

                <<"h"/utf8>> ->
                    Char;

                <<"i"/utf8>> ->
                    Char;

                <<"j"/utf8>> ->
                    Char;

                <<"k"/utf8>> ->
                    Char;

                <<"l"/utf8>> ->
                    Char;

                <<"m"/utf8>> ->
                    Char;

                <<"n"/utf8>> ->
                    Char;

                <<"o"/utf8>> ->
                    Char;

                <<"p"/utf8>> ->
                    Char;

                <<"q"/utf8>> ->
                    Char;

                <<"r"/utf8>> ->
                    Char;

                <<"s"/utf8>> ->
                    Char;

                <<"t"/utf8>> ->
                    Char;

                <<"u"/utf8>> ->
                    Char;

                <<"v"/utf8>> ->
                    Char;

                <<"w"/utf8>> ->
                    Char;

                <<"x"/utf8>> ->
                    Char;

                <<"y"/utf8>> ->
                    Char;

                <<"z"/utf8>> ->
                    Char;

                _ ->
                    <<"_"/utf8>>
            end end),
    gleam@string:join(_pipe@3, <<""/utf8>>).

-file("src/internal/iconify.gleam", 20).
-spec add(binary(), internal@config:configuration()) -> nil.
add(Icon, Config) ->
    {Set, Name} = begin
        _pipe = gleam@string:split_once(Icon, <<"/"/utf8>>),
        gleam@result:unwrap(_pipe, {erlang:element(3, Config), Icon})
    end,
    Module = gleam@string:replace(
        erlang:element(4, Config),
        <<"*"/utf8>>,
        normalize(Set)
    ),
    Module@1 = case gleam_stdlib:string_ends_with(Module, <<"/"/utf8>>) of
        false ->
            Module;

        true ->
            <<Module/binary, (normalize(Name))/binary>>
    end,
    Filename = <<<<<<(erlang:element(2, Config))/binary, "src/"/utf8>>/binary,
            Module@1/binary>>/binary,
        ".gleam"/utf8>>,
    gleam_stdlib:print(
        <<<<<<<<"- "/utf8, Set/binary>>/binary, "/"/utf8>>/binary, Name/binary>>/binary,
            ": "/utf8>>
    ),
    case do_add(Set, Name, Filename) of
        {ok, Change} ->
            gleam_stdlib:println(internal@module:change_to_string(Change));

        {error, {http_error, Url, invalid_utf8_response}} ->
            gleam_stdlib:println(<<"Invalid response from "/utf8, Url/binary>>);

        {error, {http_error, Url@1, {failed_to_connect, _, _}}} ->
            gleam_stdlib:println(<<"Failed to connect to "/utf8, Url@1/binary>>);

        {error, {http_error, Url@2, response_timeout}} ->
            gleam_stdlib:println(<<"Timeout from "/utf8, Url@2/binary>>);

        {error, {invalid_response, Url@3}} ->
            gleam_stdlib:println(
                <<"Invalid response from "/utf8, Url@3/binary>>
            );

        {error, {conversion_error, _}} ->
            gleam_stdlib:println(<<"Couldn't convert SVG to lustre"/utf8>>);

        {error, {module_error, Error}} ->
            gleam_stdlib:println(internal@module:error_to_string(Error))
    end.