Current section

Files

Jump to
glint src glint@flag.erl
Raw

src/glint@flag.erl

-module(glint@flag).
-compile(no_auto_import).
-export([int/2, int_list/2, string/2, string_list/2, bool/2, build_map/1, update_flags/2]).
-export_type([flag_value/0, flag/0]).
-type flag_value() :: {bool_flag, boolean()} |
{int_flag, integer()} |
{int_list_flag, list(integer())} |
{string_flag, binary()} |
{string_list_flag, list(binary())}.
-type flag() :: {flag, binary(), flag_value()}.
-spec int(binary(), integer()) -> flag().
int(Name, Value) ->
{flag, Name, {int_flag, Value}}.
-spec int_list(binary(), list(integer())) -> flag().
int_list(Name, Value) ->
{flag, Name, {int_list_flag, Value}}.
-spec string(binary(), binary()) -> flag().
string(Name, Value) ->
{flag, Name, {string_flag, Value}}.
-spec string_list(binary(), list(binary())) -> flag().
string_list(Name, Value) ->
{flag, Name, {string_list_flag, Value}}.
-spec bool(binary(), boolean()) -> flag().
bool(Name, Value) ->
{flag, Name, {bool_flag, Value}}.
-spec build_map(list(flag())) -> gleam@map:map_(binary(), flag_value()).
build_map(Flags) ->
gleam@list:fold(
Flags,
gleam@map:new(),
fun(M, Flag) ->
gleam@map:insert(
M,
erlang:element(2, Flag),
erlang:element(3, Flag)
)
end
).
-spec update_flags(gleam@map:map_(binary(), flag_value()), binary()) -> {ok,
gleam@map:map_(binary(), flag_value())} |
{error, snag:snag()}.
update_flags(Flags, Flag_input) ->
case begin
_pipe = Flag_input,
_pipe@1 = gleam@string:split_once(_pipe, <<"="/utf8>>),
gleam@result:replace_error(_pipe@1, no_value_flag_err(Flag_input))
end of
{error, _try} -> {error, _try};
{ok, {Key, Value}} ->
case begin
_pipe@2 = gleam@map:get(Flags, Key),
gleam@result:replace_error(_pipe@2, undefined_flag_err(Key))
end of
{error, _try@1} -> {error, _try@1};
{ok, Default} ->
case case Default of
{int_flag, _@1} ->
_pipe@3 = Value,
_pipe@4 = gleam@int:parse(_pipe@3),
_pipe@5 = gleam@result:replace_error(
_pipe@4,
int_flag_err(Key, Value)
),
gleam@result:map(
_pipe@5,
fun(A) -> {int_flag, A} end
);
{int_list_flag, _@2} ->
_pipe@6 = Value,
_pipe@7 = gleam@string:split(_pipe@6, <<","/utf8>>),
_pipe@8 = gleam@list:try_map(
_pipe@7,
fun gleam@int:parse/1
),
_pipe@9 = gleam@result:replace_error(
_pipe@8,
int_list_flag_err(Key, Value)
),
gleam@result:map(
_pipe@9,
fun(A) -> {int_list_flag, A} end
);
{string_flag, _@3} ->
{ok, {string_flag, Value}};
{string_list_flag, _@4} ->
_pipe@10 = Value,
_pipe@11 = gleam@string:split(
_pipe@10,
<<","/utf8>>
),
_pipe@12 = {string_list_flag, _pipe@11},
{ok, _pipe@12};
{bool_flag, _@5} ->
case Value of
<<"true"/utf8>> ->
{ok, {bool_flag, true}};
<<"false"/utf8>> ->
{ok, {bool_flag, false}};
_@6 ->
{error, bool_flag_err(Key, Value)}
end
end of
{error, _try@2} -> {error, _try@2};
{ok, New_value} ->
{ok, gleam@map:insert(Flags, Key, New_value)}
end
end
end.
-spec layer_invalid_flag(snag:snag(), binary()) -> snag:snag().
layer_invalid_flag(Err, Flag) ->
_pipe = [<<"invalid flag '"/utf8>>, Flag, <<"'"/utf8>>],
_pipe@1 = gleam@string:concat(_pipe),
snag:layer(Err, _pipe@1).
-spec no_value_flag_err(binary()) -> snag:snag().
no_value_flag_err(Flag_input) ->
_pipe = [<<"flag '"/utf8>>, Flag_input, <<"' has no assigned value"/utf8>>],
_pipe@1 = gleam@string:concat(_pipe),
_pipe@2 = snag:new(_pipe@1),
layer_invalid_flag(_pipe@2, Flag_input).
-spec undefined_flag_err(binary()) -> snag:snag().
undefined_flag_err(Key) ->
_pipe = <<"flag provided but not defined"/utf8>>,
_pipe@1 = snag:new(_pipe),
layer_invalid_flag(_pipe@1, Key).
-spec int_flag_err(binary(), binary()) -> snag:snag().
int_flag_err(Key, Value) ->
_pipe = [<<"cannot parse flag '"/utf8>>,
Key,
<<"' value '"/utf8>>,
Value,
<<"' as int"/utf8>>],
_pipe@1 = gleam@string:concat(_pipe),
_pipe@2 = snag:new(_pipe@1),
layer_invalid_flag(_pipe@2, Key).
-spec int_list_flag_err(binary(), binary()) -> snag:snag().
int_list_flag_err(Key, Value) ->
_pipe = [<<"cannot parse flag '"/utf8>>,
Key,
<<"' value '"/utf8>>,
Value,
<<"' as int list"/utf8>>],
_pipe@1 = gleam@string:concat(_pipe),
_pipe@2 = snag:new(_pipe@1),
layer_invalid_flag(_pipe@2, Key).
-spec bool_flag_err(binary(), binary()) -> snag:snag().
bool_flag_err(Key, Value) ->
_pipe = [<<"cannot parse flag '"/utf8>>,
Key,
<<"' value '"/utf8>>,
Value,
<<"' as boolean"/utf8>>],
_pipe@1 = gleam@string:concat(_pipe),
_pipe@2 = snag:new(_pipe@1),
layer_invalid_flag(_pipe@2, Key).