Current section

Files

Jump to
gladvent src cmd@new.erl
Raw

src/cmd@new.erl

-module(cmd@new).
-compile(no_auto_import).
-export([register_command/1, run/1]).
-export_type([err/0]).
-type err() :: {failed_to_create_dir, binary()} |
{failed_to_create_file, binary()} |
{file_already_exists, binary()} |
{combo, binary(), binary()} |
{other, binary()}.
-spec input_path(integer()) -> binary().
input_path(Day) ->
gleam@string:concat(
[<<"input/"/utf8>>,
<<"day_"/utf8>>,
gleam@int:to_string(Day),
<<".txt"/utf8>>]
).
-spec gleam_src_path(integer()) -> binary().
gleam_src_path(Day) ->
gleam@string:concat(
[<<"src/days/"/utf8>>,
<<"day_"/utf8>>,
gleam@int:to_string(Day),
<<".gleam"/utf8>>]
).
-spec create_dir(binary()) -> {ok, nil} | {error, err()}.
create_dir(Dir) ->
_pipe = Dir,
_pipe@1 = gleam@erlang@file:make_directory(_pipe),
handle_dir_open_res(_pipe@1, Dir).
-spec handle_dir_open_res(
{ok, nil} | {error, gleam@erlang@file:reason()},
binary()
) -> {ok, nil} | {error, err()}.
handle_dir_open_res(Res, Filename) ->
case Res of
{ok, nil} ->
{ok, nil};
{error, eexist} ->
{ok, nil};
_@1 ->
_pipe = Filename,
_pipe@1 = {failed_to_create_dir, _pipe},
{error, _pipe@1}
end.
-spec create_files(integer()) -> {ok, nil} | {error, err()}.
create_files(Day) ->
Input_path = input_path(Day),
Gleam_src_path = gleam_src_path(Day),
Create_src_res = begin
_pipe = ffi@file:open_file_exclusive(Gleam_src_path),
_pipe@1 = gleam@result:then(
_pipe,
fun(_capture) ->
ffi@file:write(
_capture,
<<"pub fn run(input) {
#(pt_1(input), pt_2(input))
}
fn pt_1(input: String) -> Int {
0
}
fn pt_2(input: String) -> Int {
0
}
"/utf8>>
)
end
),
gleam@result:map_error(
_pipe@1,
fun(_capture@1) ->
handle_file_open_failure(_capture@1, Gleam_src_path)
end
)
end,
Create_input_res = begin
_pipe@2 = ffi@file:open_file_exclusive(Input_path),
gleam@result:map_error(
_pipe@2,
fun(_capture@2) ->
handle_file_open_failure(_capture@2, Input_path)
end
)
end,
case {Create_input_res, Create_src_res} of
{{ok, _@1}, {ok, _@2}} ->
{ok, nil};
{R1, R2} ->
{error, {combo, res_to_string(R1), res_to_string(R2)}}
end.
-spec handle_file_open_failure(gleam@erlang@file:reason(), binary()) -> err().
handle_file_open_failure(Reason, Filename) ->
case Reason of
eexist ->
{file_already_exists, Filename};
_@1 ->
{failed_to_create_file, Filename}
end.
-spec res_to_string({ok, any()} | {error, err()}) -> binary().
res_to_string(R) ->
case R of
{ok, _@1} ->
<<""/utf8>>;
{error, E} ->
_pipe = E,
_pipe@1 = to_snag(_pipe),
snag:line_print(_pipe@1)
end.
-spec do(integer()) -> {ok, nil} | {error, err()}.
do(Day) ->
_pipe = [<<"input/"/utf8>>, <<"src/days/"/utf8>>],
_pipe@1 = gleam@list:try_map(_pipe, fun create_dir/1),
gleam@result:then(_pipe@1, fun(_) -> create_files(Day) end).
-spec collect({integer(), {ok, nil} | {error, err()}}) -> binary().
collect(X) ->
Day = gleam@int:to_string(erlang:element(1, X)),
case begin
_pipe = erlang:element(2, X),
_pipe@1 = gleam@result:map_error(_pipe, fun to_snag/1),
_pipe@2 = snag:context(
_pipe@1,
gleam@string:append(
<<"error occurred when initializing day "/utf8>>,
Day
)
),
gleam@result:map_error(_pipe@2, fun snag:pretty_print/1)
end of
{ok, _@1} ->
gleam@string:append(<<"initialized day: "/utf8>>, Day);
{error, Reason} ->
Reason
end.
-spec register_command(
glint:command({ok, list(binary())} | {error, snag:snag()})
) -> glint:command({ok, list(binary())} | {error, snag:snag()}).
register_command(Glint) ->
glint:add_command(
Glint,
[<<"new"/utf8>>],
fun run/1,
[],
<<"Create .gleam and input files"/utf8>>,
<<"gleam run new <dayX> <dayY> <...> "/utf8>>
).
-spec run(glint:command_input()) -> {ok, list(binary())} | {error, snag:snag()}.
run(Input) ->
_pipe = erlang:element(2, Input),
_pipe@1 = parse:days(_pipe),
gleam@result:map(
_pipe@1,
fun(_capture) ->
cmd:exec(
_capture,
endless,
fun do/1,
fun(A) -> {other, A} end,
fun collect/1
)
end
).
-spec to_snag(err()) -> snag:snag().
to_snag(E) ->
_pipe = case E of
{failed_to_create_dir, D} ->
gleam@string:append(<<"failed to create dir: "/utf8>>, D);
{failed_to_create_file, F} ->
gleam@string:append(<<"failed to create file: "/utf8>>, F);
{file_already_exists, F@1} ->
gleam@string:append(<<"file already exists: "/utf8>>, F@1);
{combo, E1, E2} ->
gleam@string:join([E1, E2], <<" && "/utf8>>);
{other, S} ->
S
end,
snag:new(_pipe).