-module(cmd@new). -compile(no_auto_import). -export([register_command/1, run/1]). -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 do(integer()) -> {ok, nil} | {error, snag:snag()}. do(Day) -> case case gleam@erlang@file:make_directory(<<"input/"/utf8>>) of {ok, _@1} -> {ok, nil}; {error, eexist} -> {ok, nil}; _@1 -> {error, failed_to_create_dir_err(<<"input/"/utf8>>)} end of {error, _try} -> {error, _try}; {ok, _@2} -> case case gleam@erlang@file:make_directory(<<"src/days/"/utf8>>) of {ok, _@3} -> {ok, nil}; {error, eexist} -> {ok, nil}; _@3 -> {error, failed_to_create_dir_err(<<"src/days/"/utf8>>)} end of {error, _try@1} -> {error, _try@1}; {ok, _@4} -> 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, Input_path = input_path(Day), 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_src_res, Create_input_res} of {{ok, _@5}, {ok, _@6}} -> {ok, nil}; {{error, {snag, S1, Errs1}}, {error, {snag, S2, Errs2}}} -> _pipe@3 = [gleam@string:join( [S1 | Errs1], <<": "/utf8>> ), gleam@string:join( [S2 | Errs2], <<": "/utf8>> )], _pipe@4 = gleam@string:join( _pipe@3, <<" && "/utf8>> ), snag:error(_pipe@4); {_@7, {error, Err}} -> {error, Err}; {{error, Err@1}, _@8} -> {error, Err@1} end end end. -spec handle_file_open_failure(gleam@erlang@file:reason(), binary()) -> snag:snag(). handle_file_open_failure(Reason, Filename) -> case Reason of eexist -> file_already_exists_err(Filename); _@1 -> failed_to_create_file_err(Filename) end. -spec file_already_exists_err(binary()) -> snag:snag(). file_already_exists_err(Filename) -> _pipe = Filename, _pipe@1 = snag:new(_pipe), snag:layer(_pipe@1, <<"file already exists"/utf8>>). -spec failed_to_create_file_err(binary()) -> snag:snag(). failed_to_create_file_err(Filename) -> _pipe = Filename, _pipe@1 = snag:new(_pipe), snag:layer(_pipe@1, <<"failed to create file"/utf8>>). -spec failed_to_create_dir_err(binary()) -> snag:snag(). failed_to_create_dir_err(Dir) -> _pipe = Dir, _pipe@1 = snag:new(_pipe), snag:layer(_pipe@1, <<"failed to create dir"/utf8>>). -spec collect({{ok, nil} | {error, snag:snag()}, integer()}) -> binary(). collect(X) -> Day = gleam@int:to_string(erlang:element(2, X)), case begin _pipe = erlang:element(1, X), _pipe@1 = snag:context( _pipe, gleam@string:append( <<"error occurred when initializing day "/utf8>>, Day ) ), gleam@result:map_error(_pipe@1, 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 <...> "/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 collect/1) end ).