Current section
Files
Jump to
Current section
Files
src/cmd@run.erl
-module(cmd@run).
-compile(no_auto_import).
-export([do_run/2, register_command/2, run/3]).
-export_type([err/0]).
-type err() :: undef |
run_failed |
{failed_to_read_input, binary()} |
{unrecognized, integer()} |
{other, binary()}.
-spec do_run(fun((binary()) -> {integer(), integer()}), binary()) -> {ok,
{integer(),
integer()}} |
{error, err()}.
do_run(A, B) ->
gladvent_ffi:do_run(A, B).
-spec to_snag(err()) -> snag:snag().
to_snag(Err) ->
_pipe = case Err of
undef ->
<<"run function undefined"/utf8>>;
run_failed ->
<<"some error occurred"/utf8>>;
{unrecognized, Day} ->
gleam@string:join(
[<<"day"/utf8>>,
gleam@int:to_string(Day),
<<"unrecognized"/utf8>>],
<<" "/utf8>>
);
{failed_to_read_input, Input_path} ->
gleam@string:append(
<<"failed to read input file: "/utf8>>,
Input_path
);
{other, S} ->
S
end,
snag:new(_pipe).
-spec do(
integer(),
gleam@map:map_(integer(), fun((binary()) -> {integer(), integer()}))
) -> {ok, {integer(), integer()}} | {error, err()}.
do(Day, Runners) ->
case begin
_pipe = gleam@map:get(Runners, Day),
gleam@result:replace_error(_pipe, {unrecognized, Day})
end of
{error, _try} -> {error, _try};
{ok, Day_runner} ->
Input_path = gleam@string:join(
[<<"input/day_"/utf8>>,
gleam@int:to_string(Day),
<<".txt"/utf8>>],
<<""/utf8>>
),
case begin
_pipe@1 = Input_path,
_pipe@2 = gleam@erlang@file:read(_pipe@1),
_pipe@3 = gleam@result:map(_pipe@2, fun gleam@string:trim/1),
gleam@result:replace_error(
_pipe@3,
{failed_to_read_input, Input_path}
)
end of
{error, _try@1} -> {error, _try@1};
{ok, Input} ->
gladvent_ffi:do_run(Day_runner, Input)
end
end.
-spec collect({integer(), {ok, {integer(), integer()}} | {error, err()}}) -> binary().
collect(X) ->
Day = gleam@int:to_string(erlang:element(1, X)),
case erlang:element(2, X) of
{ok, {Res_1, Res_2}} ->
_pipe = [<<"Ran day "/utf8>>,
Day,
<<":"/utf8>>,
<<"\n Part 1: "/utf8>>,
gleam@int:to_string(Res_1),
<<"\n Part 2: "/utf8>>,
gleam@int:to_string(Res_2)],
gleam@string:concat(_pipe);
{error, Err} ->
_pipe@1 = Err,
_pipe@2 = to_snag(_pipe@1),
_pipe@3 = snag:layer(
_pipe@2,
gleam@string:append(<<"Error on day "/utf8>>, Day)
),
_pipe@4 = snag:layer(
_pipe@3,
<<"failed to run advent of code"/utf8>>
),
snag:pretty_print(_pipe@4)
end.
-spec register_command(
glint:command({ok, list(binary())} | {error, snag:snag()}),
gleam@map:map_(integer(), fun((binary()) -> {integer(), integer()}))
) -> glint:command({ok, list(binary())} | {error, snag:snag()}).
register_command(Cli, Runners) ->
Timeout_flag = glint@flag:int(
<<"timeout"/utf8>>,
0,
<<"Run with specified timeout"/utf8>>
),
_pipe = Cli,
_pipe@1 = glint:add_command(
_pipe,
[<<"run"/utf8>>],
fun(_capture) -> run(_capture, Runners, false) end,
[Timeout_flag],
<<"Run the specified days"/utf8>>,
<<"gleam run run <FLAGS> <dayX> <dayY> <...>"/utf8>>
),
glint:add_command(
_pipe@1,
[<<"run"/utf8>>, <<"all"/utf8>>],
fun(_capture@1) -> run(_capture@1, Runners, true) end,
[Timeout_flag],
<<"Run all registered days"/utf8>>,
<<"gleam run run <FLAGS>"/utf8>>
).
-spec run(
glint:command_input(),
gleam@map:map_(integer(), fun((binary()) -> {integer(), integer()})),
boolean()
) -> {ok, list(binary())} | {error, snag:snag()}.
run(Input, Runners, Run_all) ->
{ok, {i, Timeout@1}} = case glint@flag:get_value(
erlang:element(3, Input),
<<"timeout"/utf8>>
) of
{ok, {i, Timeout}} -> {ok, {i, Timeout}};
_try ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try,
module => <<"cmd/run"/utf8>>,
function => <<"run"/utf8>>,
line => 114})
end,
case case Timeout@1 of
0 ->
{ok, endless};
_@1 when Timeout@1 < 0 ->
invalid_timeout_err(Timeout@1);
_@2 ->
{ok, {ending, Timeout@1}}
end of
{error, _try@1} -> {error, _try@1};
{ok, Timing} ->
case case Run_all of
true ->
_pipe = Runners,
_pipe@1 = gleam@map:keys(_pipe),
_pipe@2 = gleam@list:sort(_pipe@1, fun gleam@int:compare/2),
{ok, _pipe@2};
false ->
_pipe@3 = erlang:element(2, Input),
_pipe@4 = parse:days(_pipe@3),
wrap_failed_to_parse_err(_pipe@4, erlang:element(2, Input))
end of
{error, _try@2} -> {error, _try@2};
{ok, Days} ->
_pipe@5 = Days,
_pipe@6 = cmd:exec(
_pipe@5,
Timing,
fun(_capture) -> do(_capture, Runners) end,
fun(A) -> {other, A} end,
fun collect/1
),
{ok, _pipe@6}
end
end.
-spec invalid_timeout_err(integer()) -> {ok, any()} | {error, snag:snag()}.
invalid_timeout_err(Timeout) ->
_pipe = [<<"invalid timeout value "/utf8>>,
<<"'"/utf8>>,
gleam@int:to_string(Timeout),
<<"'"/utf8>>],
_pipe@1 = gleam@string:concat(_pipe),
_pipe@2 = snag:error(_pipe@1),
snag:context(
_pipe@2,
<<"timeout must be greater than or equal to 1 ms"/utf8>>
).
-spec wrap_failed_to_parse_err({ok, HHA} | {error, snag:snag()}, list(binary())) -> {ok,
HHA} |
{error, snag:snag()}.
wrap_failed_to_parse_err(Res, Args) ->
snag:context(
Res,
gleam@string:join(
[<<"failed to parse arguments:"/utf8>> | Args],
<<" "/utf8>>
)
).