Skip to main content

src/tomlet@key.erl

-module(tomlet@key).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/tomlet/key.gleam").
-export([to_strings/1, starts_with/2, conflicts/2, is_bare_key/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/tomlet/key.gleam", 7).
?DOC(false).
-spec to_strings(tomlet@ast:key()) -> list(binary()).
to_strings(Key) ->
    {key, Segments} = Key,
    gleam@list:map(Segments, fun(Segment) -> case Segment of
                {bare_key_segment, Text} ->
                    Text;

                {quoted_key_segment, Value, _} ->
                    Value
            end end).

-file("src/tomlet/key.gleam", 17).
?DOC(false).
-spec starts_with(list(binary()), list(binary())) -> boolean().
starts_with(Path, Prefix) ->
    case {Path, Prefix} of
        {_, []} ->
            true;

        {[Value | Rest_values], [Prefix_value | Rest_prefix]} ->
            (Value =:= Prefix_value) andalso starts_with(
                Rest_values,
                Rest_prefix
            );

        {_, _} ->
            false
    end.

-file("src/tomlet/key.gleam", 26).
?DOC(false).
-spec conflicts(list(binary()), list(binary())) -> boolean().
conflicts(Existing, Target) ->
    starts_with(Target, Existing) orelse starts_with(Existing, Target).

-file("src/tomlet/key.gleam", 35).
?DOC(false).
-spec is_bare_key_codepoint(integer()) -> boolean().
is_bare_key_codepoint(Codepoint) ->
    (((((Codepoint >= 65) andalso (Codepoint =< 90)) orelse ((Codepoint >= 97)
    andalso (Codepoint =< 122)))
    orelse ((Codepoint >= 48) andalso (Codepoint =< 57)))
    orelse (Codepoint =:= 45))
    orelse (Codepoint =:= 95).

-file("src/tomlet/key.gleam", 43).
?DOC(false).
-spec all_bare_key_codepoints(list(integer())) -> boolean().
all_bare_key_codepoints(Codepoints) ->
    case Codepoints of
        [] ->
            true;

        [Codepoint | Rest] ->
            is_bare_key_codepoint(gleam_stdlib:identity(Codepoint)) andalso all_bare_key_codepoints(
                Rest
            )
    end.

-file("src/tomlet/key.gleam", 30).
?DOC(false).
-spec is_bare_key(binary()) -> boolean().
is_bare_key(Segment) ->
    (string:length(Segment) > 0) andalso all_bare_key_codepoints(
        gleam@string:to_utf_codepoints(Segment)
    ).