Skip to main content

src/rocksky@internal@query.erl

-module(rocksky@internal@query).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/rocksky/internal/query.gleam").
-export([string_param/2, int_param/2, bool_param/2, maybe_string/3, maybe_int/3, maybe_bool/3, repeated_string/3, percent_encode/1, encode/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/rocksky/internal/query.gleam", 10).
?DOC(false).
-spec string_param(binary(), binary()) -> {binary(), binary()}.
string_param(Name, Value) ->
    {Name, Value}.

-file("src/rocksky/internal/query.gleam", 14).
?DOC(false).
-spec int_param(binary(), integer()) -> {binary(), binary()}.
int_param(Name, Value) ->
    {Name, erlang:integer_to_binary(Value)}.

-file("src/rocksky/internal/query.gleam", 22).
?DOC(false).
-spec bool_to_string(boolean()) -> binary().
bool_to_string(Value) ->
    case Value of
        true ->
            <<"true"/utf8>>;

        false ->
            <<"false"/utf8>>
    end.

-file("src/rocksky/internal/query.gleam", 18).
?DOC(false).
-spec bool_param(binary(), boolean()) -> {binary(), binary()}.
bool_param(Name, Value) ->
    {Name, bool_to_string(Value)}.

-file("src/rocksky/internal/query.gleam", 30).
?DOC(false).
-spec maybe_string(
    list({binary(), binary()}),
    binary(),
    gleam@option:option(binary())
) -> list({binary(), binary()}).
maybe_string(Params, Name, Value) ->
    case Value of
        {some, V} ->
            [{Name, V} | Params];

        none ->
            Params
    end.

-file("src/rocksky/internal/query.gleam", 42).
?DOC(false).
-spec maybe_int(
    list({binary(), binary()}),
    binary(),
    gleam@option:option(integer())
) -> list({binary(), binary()}).
maybe_int(Params, Name, Value) ->
    case Value of
        {some, V} ->
            [{Name, erlang:integer_to_binary(V)} | Params];

        none ->
            Params
    end.

-file("src/rocksky/internal/query.gleam", 54).
?DOC(false).
-spec maybe_bool(
    list({binary(), binary()}),
    binary(),
    gleam@option:option(boolean())
) -> list({binary(), binary()}).
maybe_bool(Params, Name, Value) ->
    case Value of
        {some, V} ->
            [{Name, bool_to_string(V)} | Params];

        none ->
            Params
    end.

-file("src/rocksky/internal/query.gleam", 66).
?DOC(false).
-spec repeated_string(list({binary(), binary()}), binary(), list(binary())) -> list({binary(),
    binary()}).
repeated_string(Params, Name, Values) ->
    gleam@list:fold(Values, Params, fun(Acc, V) -> [{Name, V} | Acc] end).

-file("src/rocksky/internal/query.gleam", 153).
?DOC(false).
-spec hex_nibble(integer()) -> binary().
hex_nibble(N) ->
    case N of
        0 ->
            <<"0"/utf8>>;

        1 ->
            <<"1"/utf8>>;

        2 ->
            <<"2"/utf8>>;

        3 ->
            <<"3"/utf8>>;

        4 ->
            <<"4"/utf8>>;

        5 ->
            <<"5"/utf8>>;

        6 ->
            <<"6"/utf8>>;

        7 ->
            <<"7"/utf8>>;

        8 ->
            <<"8"/utf8>>;

        9 ->
            <<"9"/utf8>>;

        10 ->
            <<"A"/utf8>>;

        11 ->
            <<"B"/utf8>>;

        12 ->
            <<"C"/utf8>>;

        13 ->
            <<"D"/utf8>>;

        14 ->
            <<"E"/utf8>>;

        _ ->
            <<"F"/utf8>>
    end.

-file("src/rocksky/internal/query.gleam", 149).
?DOC(false).
-spec hex_byte(integer()) -> binary().
hex_byte(Byte) ->
    <<<<"%"/utf8, (hex_nibble(Byte div 16))/binary>>/binary,
        (hex_nibble(Byte rem 16))/binary>>.

-file("src/rocksky/internal/query.gleam", 125).
?DOC(false).
-spec encode_to_bytes(integer()) -> binary().
encode_to_bytes(Code) ->
    case Code of
        C when C < 16#80 ->
            hex_byte(C);

        C@1 when C@1 < 16#800 ->
            B1 = 16#C0 + (C@1 div 64),
            B2 = 16#80 + (C@1 rem 64),
            <<(hex_byte(B1))/binary, (hex_byte(B2))/binary>>;

        C@2 when C@2 < 16#10000 ->
            B1@1 = 16#E0 + (C@2 div 4096),
            B2@1 = 16#80 + ((C@2 div 64) rem 64),
            B3 = 16#80 + (C@2 rem 64),
            <<<<(hex_byte(B1@1))/binary, (hex_byte(B2@1))/binary>>/binary,
                (hex_byte(B3))/binary>>;

        C@3 ->
            B1@2 = 16#F0 + (C@3 div 262144),
            B2@2 = 16#80 + ((C@3 div 4096) rem 64),
            B3@1 = 16#80 + ((C@3 div 64) rem 64),
            B4 = 16#80 + (C@3 rem 64),
            <<<<<<(hex_byte(B1@2))/binary, (hex_byte(B2@2))/binary>>/binary,
                    (hex_byte(B3@1))/binary>>/binary,
                (hex_byte(B4))/binary>>
    end.

-file("src/rocksky/internal/query.gleam", 110).
?DOC(false).
-spec codepoint_to_string(integer()) -> binary().
codepoint_to_string(Cp) ->
    gleam_stdlib:utf_codepoint_list_to_string([Cp]).

-file("src/rocksky/internal/query.gleam", 114).
?DOC(false).
-spec is_unreserved(integer()) -> boolean().
is_unreserved(Code) ->
    (((((((Code >= 16#41) andalso (Code =< 16#5A)) orelse ((Code >= 16#61)
    andalso (Code =< 16#7A)))
    orelse ((Code >= 16#30) andalso (Code =< 16#39)))
    orelse (Code =:= 16#2D))
    orelse (Code =:= 16#2E))
    orelse (Code =:= 16#5F))
    orelse (Code =:= 16#7E).

-file("src/rocksky/internal/query.gleam", 102).
?DOC(false).
-spec encode_codepoint(integer()) -> binary().
encode_codepoint(Cp) ->
    Code = gleam_stdlib:identity(Cp),
    case is_unreserved(Code) of
        true ->
            codepoint_to_string(Cp);

        false ->
            encode_to_bytes(Code)
    end.

-file("src/rocksky/internal/query.gleam", 95).
?DOC(false).
-spec percent_encode(binary()) -> binary().
percent_encode(Input) ->
    _pipe = Input,
    _pipe@1 = gleam@string:to_utf_codepoints(_pipe),
    _pipe@2 = gleam@list:map(_pipe@1, fun encode_codepoint/1),
    erlang:list_to_binary(_pipe@2).

-file("src/rocksky/internal/query.gleam", 76).
?DOC(false).
-spec encode(list({binary(), binary()})) -> binary().
encode(Params) ->
    case Params of
        [] ->
            <<""/utf8>>;

        _ ->
            Pairs = begin
                _pipe = Params,
                _pipe@1 = lists:reverse(_pipe),
                gleam@list:map(
                    _pipe@1,
                    fun(P) ->
                        {K, V} = P,
                        <<<<(percent_encode(K))/binary, "="/utf8>>/binary,
                            (percent_encode(V))/binary>>
                    end
                )
            end,
            <<"?"/utf8, (gleam@string:join(Pairs, <<"&"/utf8>>))/binary>>
    end.