Current section

Files

Jump to
tale src tale@util.erl
Raw

src/tale@util.erl

-module(tale@util).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/tale/util.gleam").
-export([get_string_or/3, get_bool_or/3, optional_string/1, optional_int/1, get_string_list_or/3, describe_toml_error/1, describe_tokenizer_error/1, describe_runtime_error/1, slugify/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.
-file("src/tale/util.gleam", 10).
?DOC(" get string or\n").
-spec get_string_or(
gleam@dict:dict(binary(), tom:toml()),
list(binary()),
binary()
) -> binary().
get_string_or(Doc, Key, Fallback) ->
case tom:get_string(Doc, Key) of
{ok, Value} ->
Value;
{error, _} ->
Fallback
end.
-file("src/tale/util.gleam", 22).
?DOC(" get bool or\n").
-spec get_bool_or(
gleam@dict:dict(binary(), tom:toml()),
list(binary()),
boolean()
) -> boolean().
get_bool_or(Doc, Key, Fallback) ->
case tom:get_bool(Doc, Key) of
{ok, Value} ->
Value;
{error, _} ->
Fallback
end.
-file("src/tale/util.gleam", 50).
?DOC(" Optional string\n").
-spec optional_string({ok, binary()} | {error, tom:get_error()}) -> gleam@option:option(binary()).
optional_string(Result) ->
case Result of
{ok, Value} ->
{some, Value};
{error, _} ->
none
end.
-file("src/tale/util.gleam", 59).
-spec optional_int({ok, integer()} | {error, tom:get_error()}) -> gleam@option:option(integer()).
optional_int(Result) ->
case Result of
{ok, Value} ->
{some, Value};
{error, _} ->
none
end.
-file("src/tale/util.gleam", 67).
?DOC(" Toml array to strings\n").
-spec toml_array_to_strings(list(tom:toml()), list(binary())) -> gleam@option:option(list(binary())).
toml_array_to_strings(Values, Acc) ->
case Values of
[] ->
{some, lists:reverse(Acc)};
[{string, Value} | Rest] ->
toml_array_to_strings(Rest, [Value | Acc]);
[_ | _] ->
none
end.
-file("src/tale/util.gleam", 34).
?DOC(" Get string list\n").
-spec get_string_list_or(
gleam@dict:dict(binary(), tom:toml()),
list(binary()),
list(binary())
) -> list(binary()).
get_string_list_or(Doc, Key, Fallback) ->
case tom:get_array(Doc, Key) of
{ok, Values} ->
case toml_array_to_strings(Values, []) of
{some, Strings} ->
Strings;
none ->
Fallback
end;
{error, _} ->
Fallback
end.
-file("src/tale/util.gleam", 79).
?DOC(" TOML Errors description\n").
-spec describe_toml_error(tom:parse_error()) -> binary().
describe_toml_error(Err) ->
case Err of
{unexpected, Got, Expected} ->
<<<<<<"Unexpected \""/utf8, Got/binary>>/binary,
"\", expected "/utf8>>/binary,
Expected/binary>>;
{key_already_in_use, Key} ->
<<<<"Duplicate key \""/utf8,
(gleam@string:join(Key, <<"."/utf8>>))/binary>>/binary,
"\""/utf8>>
end.
-file("src/tale/util.gleam", 88).
-spec describe_tokenizer_error(handles@error:tokenizer_error()) -> binary().
describe_tokenizer_error(Err) ->
case Err of
{unbalanced_tag, Index} ->
<<"Unbalanced tag near character "/utf8,
(erlang:integer_to_binary(Index))/binary>>;
{unbalanced_block, Index@1} ->
<<"Unbalanced block near character "/utf8,
(erlang:integer_to_binary(Index@1))/binary>>;
{missing_argument, Index@2} ->
<<"Missing argument near character "/utf8,
(erlang:integer_to_binary(Index@2))/binary>>;
{missing_block_kind, Index@3} ->
<<"Missing block kind near character "/utf8,
(erlang:integer_to_binary(Index@3))/binary>>;
{missing_partial_id, Index@4} ->
<<"Missing partial id near character "/utf8,
(erlang:integer_to_binary(Index@4))/binary>>;
{unexpected_multiple_arguments, Index@5} ->
<<"Too many arguments near character "/utf8,
(erlang:integer_to_binary(Index@5))/binary>>;
{unexpected_argument, Index@6} ->
<<"Unexpected argument near character "/utf8,
(erlang:integer_to_binary(Index@6))/binary>>;
{unexpected_block_kind, Index@7} ->
<<"Unexpected block kind near character "/utf8,
(erlang:integer_to_binary(Index@7))/binary>>;
{unexpected_block_end, Index@8} ->
<<"Unexpected block end near character "/utf8,
(erlang:integer_to_binary(Index@8))/binary>>
end.
-file("src/tale/util.gleam", 131).
-spec describe_path(list(binary())) -> binary().
describe_path(Path) ->
case Path of
[] ->
<<"(root)"/utf8>>;
Props ->
gleam@string:join(Props, <<"."/utf8>>)
end.
-file("src/tale/util.gleam", 111).
-spec describe_runtime_error(handles@error:runtime_error()) -> binary().
describe_runtime_error(Err) ->
case Err of
{unexpected_type, Index, Path, Got, Expected} ->
<<<<<<<<<<<<<<"Unexpected value at "/utf8,
(describe_path(Path))/binary>>/binary,
" (near character "/utf8>>/binary,
(erlang:integer_to_binary(Index))/binary>>/binary,
"): got "/utf8>>/binary,
Got/binary>>/binary,
", expected one of "/utf8>>/binary,
(gleam@string:join(Expected, <<", "/utf8>>))/binary>>;
{unknown_property, Index@1, Path@1} ->
<<<<<<"Unknown property "/utf8, (describe_path(Path@1))/binary>>/binary,
" near character "/utf8>>/binary,
(erlang:integer_to_binary(Index@1))/binary>>;
{unknown_partial, _, Id} ->
<<<<"Unknown partial \""/utf8, Id/binary>>/binary, "\""/utf8>>
end.
-file("src/tale/util.gleam", 165).
-spec is_alphanumeric(binary()) -> boolean().
is_alphanumeric(Char) ->
case gleam@string:to_utf_codepoints(Char) of
[Codepoint | _] ->
Value = gleam_stdlib:identity(Codepoint),
Is_digit = (Value >= 48) andalso (Value =< 57),
Is_lower = (Value >= 97) andalso (Value =< 122),
Is_upper = (Value >= 65) andalso (Value =< 90),
(Is_digit orelse Is_lower) orelse Is_upper;
_ ->
false
end.
-file("src/tale/util.gleam", 158).
-spec is_allowed_slug_char(binary()) -> boolean().
is_allowed_slug_char(Char) ->
case Char of
<<"-"/utf8>> ->
true;
Other ->
is_alphanumeric(Other)
end.
-file("src/tale/util.gleam", 145).
-spec replace_for_slug(binary()) -> binary().
replace_for_slug(Value) ->
_pipe = Value,
_pipe@1 = gleam@string:to_graphemes(_pipe),
_pipe@2 = gleam@list:fold(
_pipe@1,
[],
fun(Acc, Char) -> case is_allowed_slug_char(Char) of
true ->
[Char | Acc];
false ->
[<<"-"/utf8>> | Acc]
end end
),
_pipe@3 = lists:reverse(_pipe@2),
erlang:list_to_binary(_pipe@3).
-file("src/tale/util.gleam", 138).
-spec slugify(binary()) -> binary().
slugify(Value) ->
_pipe = Value,
_pipe@1 = string:lowercase(_pipe),
_pipe@2 = replace_for_slug(_pipe@1),
gleam@string:replace(_pipe@2, <<"--"/utf8>>, <<"-"/utf8>>).