Current section

Files

Jump to
clad src clad.erl
Raw

src/clad.erl

-module(clad).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([flag/3, opt/4, list/1, positional_arguments/1, decode/2]).
-export_type([state/0]).
-type state() :: {state,
gleam@dict:dict(binary(), gleam@dynamic:dynamic_()),
gleam@dict:dict(binary(), list(gleam@dynamic:dynamic_())),
list(binary())}.
-file("/Users/ryan.miville/dev/clad/src/clad.gleam", 253).
-spec optional_field(
any(),
decode@zero:decoder(GZP),
fun((gleam@option:option(GZP)) -> decode@zero:decoder(GZS))
) -> decode@zero:decoder(GZS).
optional_field(Field_name, Field_decoder, Next) ->
decode@zero:optional_field(
Field_name,
none,
decode@zero:optional(Field_decoder),
Next
).
-file("/Users/ryan.miville/dev/clad/src/clad.gleam", 241).
-spec flag(binary(), binary(), fun((boolean()) -> decode@zero:decoder(GZL))) -> decode@zero:decoder(GZL).
flag(Long_name, Short_name, Next) ->
optional_field(
Long_name,
{decoder, fun decode@zero:decode_bool/1},
fun(Value) -> case Value of
{some, V} ->
Next(V);
none ->
decode@zero:optional_field(
Short_name,
false,
{decoder, fun decode@zero:decode_bool/1},
Next
)
end end
).
-file("/Users/ryan.miville/dev/clad/src/clad.gleam", 291).
-spec opt(
binary(),
binary(),
decode@zero:decoder(GZV),
fun((GZV) -> decode@zero:decoder(GZX))
) -> decode@zero:decoder(GZX).
opt(Long_name, Short_name, Field_decoder, Next) ->
optional_field(Long_name, Field_decoder, fun(Value) -> case Value of
{some, V} ->
Next(V);
none ->
decode@zero:field(Short_name, Field_decoder, Next)
end end).
-file("/Users/ryan.miville/dev/clad/src/clad.gleam", 315).
-spec list(decode@zero:decoder(HAA)) -> decode@zero:decoder(list(HAA)).
list(Inner) ->
Single = begin
_pipe = Inner,
decode@zero:map(_pipe, fun gleam@list:wrap/1)
end,
decode@zero:one_of(decode@zero:list(Inner), [Single]).
-file("/Users/ryan.miville/dev/clad/src/clad.gleam", 339).
-spec is_number(binary()) -> boolean().
is_number(Str) ->
case gleam@regexp:from_string(
<<"^[-+]?(?:\\d+(?:\\.\\d*)?|\\.\\d+)$"/utf8>>
) of
{ok, Re} ->
gleam@regexp:check(Re, Str);
{error, _} ->
false
end.
-file("/Users/ryan.miville/dev/clad/src/clad.gleam", 346).
-spec is_alpha(binary()) -> boolean().
is_alpha(Str) ->
case gleam@regexp:from_string(<<"^[a-zA-Z]+$"/utf8>>) of
{ok, Re} ->
gleam@regexp:check(Re, Str);
{error, _} ->
false
end.
-file("/Users/ryan.miville/dev/clad/src/clad.gleam", 509).
-spec append_positional(state(), binary()) -> state().
append_positional(State, Value) ->
Positional = [Value | erlang:element(4, State)],
erlang:setelement(4, State, Positional).
-file("/Users/ryan.miville/dev/clad/src/clad.gleam", 521).
-spec try_parse_float(binary()) -> {ok, gleam@dynamic:dynamic_()} | {error, nil}.
try_parse_float(Input) ->
_pipe = gleam_stdlib:parse_float(Input),
gleam@result:map(_pipe, fun gleam_stdlib:identity/1).
-file("/Users/ryan.miville/dev/clad/src/clad.gleam", 526).
-spec try_parse_int(binary()) -> {ok, gleam@dynamic:dynamic_()} | {error, nil}.
try_parse_int(Input) ->
_pipe = gleam_stdlib:parse_int(Input),
gleam@result:map(_pipe, fun gleam_stdlib:identity/1).
-file("/Users/ryan.miville/dev/clad/src/clad.gleam", 531).
-spec try_parse_bool(binary()) -> {ok, gleam@dynamic:dynamic_()} | {error, nil}.
try_parse_bool(Input) ->
case Input of
<<"true"/utf8>> ->
{ok, gleam_stdlib:identity(true)};
<<"True"/utf8>> ->
{ok, gleam_stdlib:identity(true)};
<<"false"/utf8>> ->
{ok, gleam_stdlib:identity(false)};
<<"False"/utf8>> ->
{ok, gleam_stdlib:identity(false)};
_ ->
{error, nil}
end.
-file("/Users/ryan.miville/dev/clad/src/clad.gleam", 514).
-spec parse_value(binary()) -> gleam@dynamic:dynamic_().
parse_value(Input) ->
_pipe = try_parse_float(Input),
_pipe@1 = gleam@result:'or'(_pipe, try_parse_int(Input)),
_pipe@2 = gleam@result:'or'(_pipe@1, try_parse_bool(Input)),
gleam@result:unwrap(_pipe@2, gleam_stdlib:identity(Input)).
-file("/Users/ryan.miville/dev/clad/src/clad.gleam", 433).
-spec set_arg(state(), binary(), binary()) -> state().
set_arg(State, Key, Value) ->
In_opt = gleam_stdlib:map_get(erlang:element(2, State), Key),
In_list = gleam_stdlib:map_get(erlang:element(3, State), Key),
case {In_opt, In_list} of
{{error, _}, {error, _}} ->
Opts = gleam@dict:insert(
erlang:element(2, State),
Key,
parse_value(Value)
),
erlang:setelement(2, State, Opts);
{{ok, V}, _} ->
Opts@1 = gleam@dict:delete(erlang:element(2, State), Key),
List_opts = gleam@dict:insert(
erlang:element(3, State),
Key,
[parse_value(Value), V]
),
erlang:setelement(3, erlang:setelement(2, State, Opts@1), List_opts);
{_, {ok, Values}} ->
List_opts@1 = gleam@dict:insert(
erlang:element(3, State),
Key,
[parse_value(Value) | Values]
),
erlang:setelement(3, State, List_opts@1)
end.
-file("/Users/ryan.miville/dev/clad/src/clad.gleam", 454).
-spec parse_short(binary(), state()) -> {state(), gleam@option:option(binary())}.
parse_short(Arg, State) ->
case gleam_stdlib:string_pop_grapheme(Arg) of
{ok, {H, <<""/utf8>>}} ->
{State, {some, H}};
{ok, {H@1, Rest}} ->
case {is_alpha(H@1), is_number(Rest)} of
{true, true} ->
{set_arg(State, H@1, Rest), none};
{_, _} ->
New_state = set_arg(State, H@1, <<"true"/utf8>>),
parse_short(Rest, New_state)
end;
_ ->
{State, none}
end.
-file("/Users/ryan.miville/dev/clad/src/clad.gleam", 363).
-spec parse_arg(binary(), list(binary()), state()) -> {state(), list(binary())}.
parse_arg(Arg, Rest, State) ->
case Arg of
<<"--"/utf8>> ->
Positional = begin
_pipe = [lists:reverse(Rest), erlang:element(4, State)],
gleam@list:flatten(_pipe)
end,
New_state = erlang:setelement(4, State, Positional),
{New_state, []};
<<"--"/utf8, Key/binary>> ->
case gleam@string:split(Key, <<"="/utf8>>) of
[Key@1, Value] ->
New_state@1 = set_arg(State, Key@1, Value),
{New_state@1, Rest};
_ ->
case Rest of
[] ->
New_state@2 = set_arg(State, Key, <<"true"/utf8>>),
{New_state@2, Rest};
[<<"-"/utf8, _/binary>> | _] ->
New_state@2 = set_arg(State, Key, <<"true"/utf8>>),
{New_state@2, Rest};
[Next | Rest@1] ->
New_state@3 = set_arg(State, Key, Next),
{New_state@3, Rest@1}
end
end;
<<"-"/utf8, Key@2/binary>> ->
case gleam@string:split(Key@2, <<"="/utf8>>) of
[Key@3, Value@1] ->
case gleam_stdlib:string_pop_grapheme(Key@3) of
{ok, {Key@4, _}} ->
New_state@4 = set_arg(State, Key@4, Value@1),
{New_state@4, Rest};
_ ->
{State, Rest}
end;
_ ->
case Rest of
[] ->
case parse_short(Key@2, State) of
{New_state@5, {some, K}} ->
{set_arg(New_state@5, K, <<"true"/utf8>>),
Rest};
{New_state@6, none} ->
{New_state@6, Rest}
end;
[<<"-"/utf8, _/binary>> | _] ->
case parse_short(Key@2, State) of
{New_state@5, {some, K}} ->
{set_arg(New_state@5, K, <<"true"/utf8>>),
Rest};
{New_state@6, none} ->
{New_state@6, Rest}
end;
[Next@1 | New_rest] ->
case parse_short(Key@2, State) of
{New_state@7, {some, K@1}} ->
{set_arg(New_state@7, K@1, Next@1),
New_rest};
{New_state@8, none} ->
{New_state@8, Rest}
end
end
end;
_ ->
New_state@9 = append_positional(State, Arg),
{New_state@9, Rest}
end.
-file("/Users/ryan.miville/dev/clad/src/clad.gleam", 353).
-spec parse_args(list(binary()), state()) -> state().
parse_args(Args, State) ->
case Args of
[] ->
State;
[Arg | Rest] ->
{New_state, Rest@1} = parse_arg(Arg, Rest, State),
parse_args(Rest@1, New_state)
end.
-file("/Users/ryan.miville/dev/clad/src/clad.gleam", 320).
-spec parse(list(binary())) -> state().
parse(Args) ->
State = {state, maps:new(), maps:new(), gleam@list:new()},
State@1 = parse_args(Args, State),
erlang:setelement(4, State@1, lists:reverse(erlang:element(4, State@1))).
-file("/Users/ryan.miville/dev/clad/src/clad.gleam", 219).
-spec positional_arguments(fun((list(binary())) -> decode@zero:decoder(GZI))) -> decode@zero:decoder(GZI).
positional_arguments(Next) ->
decode@zero:field(
<<"_"/utf8>>,
decode@zero:list({decoder, fun decode@zero:decode_string/1}),
fun(Args) -> Next(Args) end
).
-file("/Users/ryan.miville/dev/clad/src/clad.gleam", 327).
-spec to_dynamic(state()) -> gleam@dynamic:dynamic_().
to_dynamic(State) ->
List_opts = gleam@dict:map_values(
erlang:element(3, State),
fun(_, Values) -> _pipe = lists:reverse(Values),
gleam_stdlib:identity(_pipe) end
),
_pipe@1 = erlang:element(2, State),
_pipe@2 = maps:merge(_pipe@1, List_opts),
_pipe@3 = gleam@dict:insert(
_pipe@2,
<<"_"/utf8>>,
gleam_stdlib:identity(erlang:element(4, State))
),
gleam_stdlib:identity(_pipe@3).
-file("/Users/ryan.miville/dev/clad/src/clad.gleam", 193).
-spec decode(list(binary()), decode@zero:decoder(GZC)) -> {ok, GZC} |
{error, list(gleam@dynamic:decode_error())}.
decode(Args, Decoder) ->
_pipe = parse(Args),
_pipe@1 = to_dynamic(_pipe),
decode@zero:run(_pipe@1, Decoder).
-file("/Users/ryan.miville/dev/clad/src/clad.gleam", 487).
-spec parse_cluster(binary(), state(), gleam@option:option(binary())) -> {state(),
list(binary())}.
parse_cluster(Cluster, State, Next) ->
case gleam_stdlib:string_pop_grapheme(Cluster) of
{ok, {H, Rest}} ->
case {is_alpha(H), is_number(Rest)} of
{true, true} ->
{set_arg(State, H, Rest),
begin
_pipe = gleam@option:map(
Next,
fun gleam@list:wrap/1
),
gleam@option:unwrap(_pipe, [])
end};
{_, _} ->
New_state = set_arg(State, H, <<"true"/utf8>>),
parse_short_arg(Rest, New_state, Next)
end;
_ ->
{State,
begin
_pipe@1 = gleam@option:map(Next, fun gleam@list:wrap/1),
gleam@option:unwrap(_pipe@1, [])
end}
end.
-file("/Users/ryan.miville/dev/clad/src/clad.gleam", 470).
-spec parse_short_arg(binary(), state(), gleam@option:option(binary())) -> {state(),
list(binary())}.
parse_short_arg(Arg, State, Next) ->
case string:length(Arg) of
1 ->
Value = gleam@option:unwrap(Next, <<"true"/utf8>>),
New_state = set_arg(State, Arg, Value),
{New_state, []};
_ ->
parse_cluster(Arg, State, Next)
end.