Current section

Files

Jump to
gladvent src cmd@new.erl
Raw

src/cmd@new.erl

-module(cmd@new).
-compile(no_auto_import).
-export([new_command/0]).
-export_type([err/0]).
-type err() :: {failed_to_create_dir, binary()} |
{failed_to_create_file, binary()} |
{file_already_exists, binary()} |
{combo, err(), err()} |
{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_ffi: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, snag:snag()}.
create_files(Day) ->
Input_path = input_path(Day),
Gleam_src_path = gleam_src_path(Day),
Create_src_res = begin
_pipe = gladvent_ffi:open_file_exclusive(Gleam_src_path),
_pipe@1 = gleam@result:then(
_pipe,
fun(_capture) ->
ffi@file:write(
_capture,
<<"pub fn pt_1(input: String) -> Int {
todo
}
pub fn pt_2(input: String) -> Int {
todo
}
"/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 = gladvent_ffi: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};
{{error, E1}, {ok, _@3}} ->
{error,
begin
_pipe@3 = [<<"created "/utf8>>,
Gleam_src_path,
<<", but failed to create "/utf8>>,
Input_path],
_pipe@4 = gleam@string:concat(_pipe@3),
snag:layer(to_snag(E1), _pipe@4)
end};
{{ok, _@4}, {error, E2}} ->
{error,
begin
_pipe@5 = [<<"created "/utf8>>,
Input_path,
<<", but failed to create "/utf8>>,
Gleam_src_path],
_pipe@6 = gleam@string:concat(_pipe@5),
snag:layer(to_snag(E2), _pipe@6)
end};
{{error, E1@1}, {error, E2@1}} ->
{error,
begin
_pipe@7 = {combo, E1@1, E2@1},
to_snag(_pipe@7)
end}
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 do(integer()) -> {ok, nil} | {error, snag:snag()}.
do(Day) ->
case begin
_pipe = gleam@list:try_map(
[<<"input/"/utf8>>, <<"src/days/"/utf8>>],
fun create_dir/1
),
gleam@result:map_error(_pipe, fun to_snag/1)
end of
{error, _try} -> {error, _try};
{ok, _@1} ->
create_files(Day)
end.
-spec collect({integer(), {ok, nil} | {error, snag:snag()}}) -> binary().
collect(X) ->
Day = gleam@int:to_string(erlang:element(1, X)),
case begin
_pipe = erlang:element(2, X),
_pipe@1 = snag:context(
_pipe,
<<"error occurred when initializing day "/utf8, Day/binary>>
),
gleam@result:map_error(_pipe@1, fun snag:pretty_print/1)
end of
{ok, _@1} ->
<<"initialized day: "/utf8, Day/binary>>;
{error, Reason} ->
Reason
end.
-spec new_command() -> glint:stub({ok, list(binary())} | {error, snag:snag()}).
new_command() ->
{stub,
[<<"new"/utf8>>],
fun run/1,
[],
<<"Create .gleam and input files"/utf8>>}.
-spec run(glint:command_input()) -> {ok, list(binary())} | {error, snag:snag()}.
run(Input) ->
_pipe = erlang:element(2, Input),
_pipe@1 = parse:days(_pipe),
_pipe@2 = snag:context(
_pipe@1,
gleam@string:join(
[<<"failed to initialize:"/utf8>> | erlang:element(2, Input)],
<<" "/utf8>>
)
),
gleam@result:map(
_pipe@2,
fun(_capture) ->
cmd:exec(_capture, endless, fun do/1, fun snag:new/1, fun collect/1)
end
).
-spec to_snag(err()) -> snag:snag().
to_snag(E) ->
_pipe@4 = 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>>;
{combo, E1, E2} ->
_pipe = [E1, E2],
_pipe@1 = gleam@list:map(_pipe, fun to_snag/1),
_pipe@2 = gleam@list:map(_pipe@1, fun snag:line_print/1),
_pipe@3 = gleam@list:filter(_pipe@2, fun(S) -> S /= <<""/utf8>> end),
gleam@string:join(_pipe@3, <<" && "/utf8>>);
{other, S@1} ->
S@1
end,
snag:new(_pipe@4).