Current section

Files

Jump to
gladvent src gladvent@internal@cmd@new.erl
Raw

src/gladvent@internal@cmd@new.erl

-module(gladvent@internal@cmd@new).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([new_command/0]).
-export_type([context/0, err/0]).
-type context() :: {context, integer(), integer()}.
-type err() :: {failed_to_create_dir, binary()} |
{failed_to_create_file, binary()} |
{file_already_exists, binary()}.
-spec err_to_string(err()) -> binary().
err_to_string(E) ->
case E of
{failed_to_create_dir, D} ->
<<"failed to create dir: "/utf8, D/binary>>;
{failed_to_create_file, F} ->
<<"failed to create file: "/utf8, F/binary>>;
{file_already_exists, F@1} ->
<<"file already exists: "/utf8, F@1/binary>>
end.
-spec input_path(integer(), integer()) -> binary().
input_path(Year, Day) ->
<<<<(gladvent@internal@cmd:input_dir(Year))/binary,
(gleam@int:to_string(Day))/binary>>/binary,
".txt"/utf8>>.
-spec gleam_src_path(integer(), integer()) -> binary().
gleam_src_path(Year, Day) ->
<<<<<<(gladvent@internal@cmd:src_dir(Year))/binary, "day_"/utf8>>/binary,
(gleam@int:to_string(Day))/binary>>/binary,
".gleam"/utf8>>.
-spec handle_dir_open_res(
{ok, any()} | {error, simplifile:file_error()},
binary()
) -> {ok, binary()} | {error, err()}.
handle_dir_open_res(Res, Filename) ->
case Res of
{ok, _} ->
{ok, Filename};
{error, eexist} ->
{ok, <<""/utf8>>};
_ ->
_pipe = Filename,
_pipe@1 = {failed_to_create_dir, _pipe},
{error, _pipe@1}
end.
-spec create_dir(binary()) -> {ok, binary()} | {error, err()}.
create_dir(Dir) ->
_pipe = simplifile:create_directory(Dir),
handle_dir_open_res(_pipe, Dir).
-spec create_src_dir(context()) -> {ok, binary()} | {error, err()}.
create_src_dir(Ctx) ->
_pipe = erlang:element(2, Ctx),
_pipe@1 = gladvent@internal@cmd:src_dir(_pipe),
create_dir(_pipe@1).
-spec create_input_root(context()) -> {ok, binary()} | {error, err()}.
create_input_root(_) ->
_pipe = <<"input/"/utf8>>,
create_dir(_pipe).
-spec create_input_dir(context()) -> {ok, binary()} | {error, err()}.
create_input_dir(Ctx) ->
_pipe = erlang:element(2, Ctx),
_pipe@1 = gladvent@internal@cmd:input_dir(_pipe),
create_dir(_pipe@1).
-spec handle_file_open_failure(simplifile:file_error(), binary()) -> err().
handle_file_open_failure(Reason, Filename) ->
case Reason of
eexist ->
{file_already_exists, Filename};
_ ->
{failed_to_create_file, Filename}
end.
-spec create_input_file(context()) -> {ok, binary()} | {error, err()}.
create_input_file(Ctx) ->
Input_path = input_path(erlang:element(2, Ctx), erlang:element(3, Ctx)),
_pipe = gladvent@internal@file:do_with_file(Input_path, fun(_) -> nil end),
_pipe@1 = gleam@result:map_error(
_pipe,
fun(_capture) -> handle_file_open_failure(_capture, Input_path) end
),
gleam@result:replace(_pipe@1, Input_path).
-spec collect(integer(), {integer(), binary()}) -> binary().
collect(Year, X) ->
Day = gleam@int:to_string(erlang:element(1, X)),
Year@1 = gleam@int:to_string(Year),
<<<<<<<<<<"initialized "/utf8, Year@1/binary>>/binary, " day "/utf8>>/binary,
Day/binary>>/binary,
"\n"/utf8>>/binary,
(erlang:element(2, X))/binary>>.
-spec collect_async(integer(), {integer(), {ok, binary()} | {error, binary()}}) -> binary().
collect_async(Year, X) ->
_pipe = fun(Res) -> case Res of
{ok, Res@1} ->
Res@1;
{error, Err} ->
Err
end end,
_pipe@1 = gleam@pair:map_second(X, _pipe),
collect(Year, _pipe@1).
-spec create_src_file(context()) -> {ok, binary()} | {error, err()}.
create_src_file(Ctx) ->
Gleam_src_path = gleam_src_path(
erlang:element(2, Ctx),
erlang:element(3, Ctx)
),
_pipe = Gleam_src_path,
_pipe@1 = gladvent@internal@file:do_with_file(
_pipe,
fun(_capture) ->
gladvent@internal@file:write(
_capture,
<<"pub fn pt_1(input: String) {
todo as \"part 1 not implemented\"
}
pub fn pt_2(input: String) {
todo as \"part 2 not implemented\"
}
"/utf8>>
)
end
),
_pipe@2 = gleam@result:flatten(_pipe@1),
_pipe@3 = gleam@result:map_error(
_pipe@2,
fun(_capture@1) ->
handle_file_open_failure(_capture@1, Gleam_src_path)
end
),
gleam@result:replace(_pipe@3, Gleam_src_path).
-spec do(context()) -> binary().
do(Ctx) ->
Seq = [fun create_input_root/1,
fun create_input_dir/1,
fun create_input_file/1,
fun create_src_dir/1,
fun create_src_file/1],
Successes = fun(Good) -> case Good of
<<""/utf8>> ->
<<""/utf8>>;
_ ->
<<"created:"/utf8, Good/binary>>
end end,
Errors = fun(Errs) -> case Errs of
<<""/utf8>> ->
<<""/utf8>>;
_ ->
<<"errors:"/utf8, Errs/binary>>
end end,
Newline_tab = fun(A, B) ->
<<<<A/binary, "\n\t"/utf8>>/binary, B/binary>>
end,
{Good@1, Bad} = begin
_pipe = (gleam@list:fold(
Seq,
{<<""/utf8>>, <<""/utf8>>},
fun(Acc, F) -> case F(Ctx) of
{ok, <<""/utf8>>} ->
Acc;
{ok, O} ->
gleam@pair:map_first(
Acc,
fun(_capture) -> Newline_tab(_capture, O) end
);
{error, Err} ->
gleam@pair:map_second(
Acc,
fun(_capture@1) ->
Newline_tab(_capture@1, err_to_string(Err))
end
)
end end
)),
_pipe@1 = gleam@pair:map_first(_pipe, Successes),
gleam@pair:map_second(_pipe@1, Errors)
end,
_pipe@2 = [Good@1, Bad],
_pipe@3 = gleam@list:filter(_pipe@2, fun(S) -> S /= <<""/utf8>> end),
gleam@string:join(_pipe@3, <<"\n\n"/utf8>>).
-spec new_command() -> glint:command({ok, list(binary())} | {error, snag:snag()}).
new_command() ->
_pipe = (glint:command(
fun(Input) ->
gleam@result:map(
gladvent@internal@parse:days(erlang:element(2, Input)),
fun(Days) ->
_assert_subject = glint@flag:get_int(
erlang:element(3, Input),
<<"year"/utf8>>
),
{ok, Year} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"gladvent/internal/cmd/new"/utf8>>,
function => <<"new_command"/utf8>>,
line => 173})
end,
gladvent@internal@cmd:exec(
Days,
endless,
fun(Day) -> do({context, Year, Day}) end,
fun(_capture) -> collect_async(Year, _capture) end
)
end
)
end
)),
_pipe@1 = glint:description(_pipe, <<"Create .gleam and input files"/utf8>>),
glint:unnamed_args(_pipe@1, {min_args, 1}).