Current section
Files
Jump to
Current section
Files
src/clad.erl
-module(clad).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([flag/0, opt/4, positional_arguments/1, decode/2]).
-export_type([state/0]).
-type state() :: {state,
gleam@dict:dict(binary(), gleam@dynamic:dynamic_()),
list(binary())}.
-file("/Users/ryan.miville/dev/clad/src/clad.gleam", 195).
-spec flag() -> decode@zero:decoder(boolean()).
flag() ->
_pipe = {decoder, fun decode@zero:decode_bool/1},
_pipe@1 = decode@zero:optional(_pipe),
decode@zero:map(
_pipe@1,
fun(_capture) -> gleam@option:unwrap(_capture, false) end
).
-file("/Users/ryan.miville/dev/clad/src/clad.gleam", 201).
-spec optional_field(
any(),
decode@zero:decoder(GWH),
fun((gleam@option:option(GWH)) -> decode@zero:decoder(GWK))
) -> decode@zero:decoder(GWK).
optional_field(Field_name, Field_decoder, Next) ->
Decoding_function = fun(Data) ->
gleam@bool:guard(
gleam@dynamic:classify(Data) =:= <<"Nil"/utf8>>,
{ok, none},
fun() ->
case decode@zero:run(Data, decode@zero:optional(Field_decoder)) of
{ok, none} ->
case decode@zero:run(Data, Field_decoder) of
{ok, V} ->
{ok, {some, V}};
{error, _} ->
{ok, none}
end;
Other ->
Other
end
end
)
end,
Decoder = decode@zero:new_primitive_decoder(Decoding_function, none),
decode@zero:field(Field_name, Decoder, Next).
-file("/Users/ryan.miville/dev/clad/src/clad.gleam", 236).
-spec opt(
binary(),
binary(),
decode@zero:decoder(GWN),
fun((GWN) -> decode@zero:decoder(GWP))
) -> decode@zero:decoder(GWP).
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", 262).
-spec is_number(binary()) -> boolean().
is_number(Str) ->
case gleam@regex:from_string(<<"^[-+]?(?:\\d+(?:\\.\\d*)?|\\.\\d+)$"/utf8>>) of
{ok, Re} ->
gleam@regex:check(Re, Str);
{error, _} ->
false
end.
-file("/Users/ryan.miville/dev/clad/src/clad.gleam", 269).
-spec is_alpha(binary()) -> boolean().
is_alpha(Str) ->
case gleam@regex:from_string(<<"^[a-zA-Z]+$"/utf8>>) of
{ok, Re} ->
gleam@regex:check(Re, Str);
{error, _} ->
false
end.
-file("/Users/ryan.miville/dev/clad/src/clad.gleam", 411).
-spec append_positional(state(), binary()) -> state().
append_positional(State, Value) ->
Positional = [Value | erlang:element(3, State)],
erlang:setelement(3, State, Positional).
-file("/Users/ryan.miville/dev/clad/src/clad.gleam", 423).
-spec try_parse_float(binary()) -> {ok, gleam@dynamic:dynamic_()} | {error, nil}.
try_parse_float(Input) ->
_pipe = gleam@float:parse(Input),
gleam@result:map(_pipe, fun gleam_stdlib:identity/1).
-file("/Users/ryan.miville/dev/clad/src/clad.gleam", 428).
-spec try_parse_int(binary()) -> {ok, gleam@dynamic:dynamic_()} | {error, nil}.
try_parse_int(Input) ->
_pipe = gleam@int:parse(Input),
gleam@result:map(_pipe, fun gleam_stdlib:identity/1).
-file("/Users/ryan.miville/dev/clad/src/clad.gleam", 433).
-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", 416).
-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", 351).
-spec set_arg(state(), binary(), binary()) -> state().
set_arg(State, Key, Value) ->
Opts = gleam@dict:insert(erlang:element(2, State), Key, parse_value(Value)),
erlang:setelement(2, State, Opts).
-file("/Users/ryan.miville/dev/clad/src/clad.gleam", 356).
-spec parse_short(binary(), state()) -> {state(), gleam@option:option(binary())}.
parse_short(Arg, State) ->
case gleam@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", 286).
-spec parse_arg(binary(), list(binary()), state()) -> {state(), list(binary())}.
parse_arg(Arg, Rest, State) ->
case Arg of
<<"--"/utf8, Key/binary>> ->
case gleam@string:split(Key, <<"="/utf8>>) of
[Key@1, Value] ->
New_state = set_arg(State, Key@1, Value),
{New_state, Rest};
_ ->
case Rest of
[] ->
New_state@1 = set_arg(State, Key, <<"true"/utf8>>),
{New_state@1, Rest};
[<<"-"/utf8, _/binary>> | _] ->
New_state@1 = set_arg(State, Key, <<"true"/utf8>>),
{New_state@1, Rest};
[Next | Rest@1] ->
New_state@2 = set_arg(State, Key, Next),
{New_state@2, Rest@1}
end
end;
<<"-"/utf8, Key@2/binary>> ->
case gleam@string:split(Key@2, <<"="/utf8>>) of
[Key@3, Value@1] ->
case gleam@string:pop_grapheme(Key@3) of
{ok, {Key@4, _}} ->
New_state@3 = set_arg(State, Key@4, Value@1),
{New_state@3, Rest};
_ ->
{State, Rest}
end;
_ ->
case Rest of
[] ->
case parse_short(Key@2, State) of
{New_state@4, {some, K}} ->
{set_arg(New_state@4, K, <<"true"/utf8>>),
Rest};
{New_state@5, none} ->
{New_state@5, Rest}
end;
[<<"-"/utf8, _/binary>> | _] ->
case parse_short(Key@2, State) of
{New_state@4, {some, K}} ->
{set_arg(New_state@4, K, <<"true"/utf8>>),
Rest};
{New_state@5, none} ->
{New_state@5, Rest}
end;
[Next@1 | New_rest] ->
case parse_short(Key@2, State) of
{New_state@6, {some, K@1}} ->
{set_arg(New_state@6, K@1, Next@1),
New_rest};
{New_state@7, none} ->
{New_state@7, Rest}
end
end
end;
_ ->
New_state@8 = append_positional(State, Arg),
{New_state@8, Rest}
end.
-file("/Users/ryan.miville/dev/clad/src/clad.gleam", 276).
-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", 249).
-spec parse(list(binary())) -> state().
parse(Args) ->
State = {state, gleam@dict:new(), gleam@list:new()},
State@1 = parse_args(Args, State),
erlang:setelement(3, State@1, lists:reverse(erlang:element(3, State@1))).
-file("/Users/ryan.miville/dev/clad/src/clad.gleam", 176).
-spec positional_arguments(fun((list(binary())) -> decode@zero:decoder(GWC))) -> decode@zero:decoder(GWC).
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", 256).
-spec to_dynamic(state()) -> gleam@dynamic:dynamic_().
to_dynamic(State) ->
_pipe = erlang:element(2, State),
_pipe@1 = gleam@dict:insert(
_pipe,
<<"_"/utf8>>,
gleam_stdlib:identity(erlang:element(3, State))
),
gleam_stdlib:identity(_pipe@1).
-file("/Users/ryan.miville/dev/clad/src/clad.gleam", 155).
-spec decode(list(binary()), decode@zero:decoder(GVW)) -> {ok, GVW} |
{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", 389).
-spec parse_cluster(binary(), state(), gleam@option:option(binary())) -> {state(),
list(binary())}.
parse_cluster(Cluster, State, Next) ->
case gleam@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", 372).
-spec parse_short_arg(binary(), state(), gleam@option:option(binary())) -> {state(),
list(binary())}.
parse_short_arg(Arg, State, Next) ->
case gleam@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.