-module(checkmark). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([new/0, snippets_in/2, filtering/2, using/2, check_in_current_package/2, check_in_tmp_package/2]). -export_type([operation/0, check_error/0, check_config/0]). -type operation() :: check | build | run. -type check_error() :: {run_failed, binary()} | {check_failed, binary()}. -opaque check_config() :: {check_config, binary(), fun((binary()) -> boolean()), operation()}. -file("/home/sbergen/source/checkmark/src/checkmark.gleam", 46). -spec new() -> check_config(). new() -> {check_config, <<"README.md"/utf8>>, fun(_) -> true end, check}. -file("/home/sbergen/source/checkmark/src/checkmark.gleam", 51). -spec snippets_in(check_config(), binary()) -> check_config(). snippets_in(Config, Filename) -> erlang:setelement(2, Config, Filename). -file("/home/sbergen/source/checkmark/src/checkmark.gleam", 56). -spec filtering(check_config(), fun((binary()) -> boolean())) -> check_config(). filtering(Config, Filter) -> erlang:setelement(3, Config, Filter). -file("/home/sbergen/source/checkmark/src/checkmark.gleam", 61). -spec using(check_config(), operation()) -> check_config(). using(Config, Operation) -> erlang:setelement(4, Config, Operation). -file("/home/sbergen/source/checkmark/src/checkmark.gleam", 134). -spec extract_gleam_code(binary(), fun((binary()) -> boolean())) -> list(binary()). extract_gleam_code(Markdown, Filter) -> Tokens = kirala@markdown@parser:parse_all(Markdown), gleam@list:filter_map(Tokens, fun(Token) -> case Token of {code_block, <<"gleam"/utf8>>, _, Code} -> case Filter(Code) of true -> {ok, Code}; _ -> {error, nil} end; _ -> {error, nil} end end). -file("/home/sbergen/source/checkmark/src/checkmark.gleam", 122). -spec check_snippets_in( binary(), fun((binary()) -> boolean()), fun((binary()) -> {ok, nil} | {error, check_error()}) ) -> {ok, list({ok, nil} | {error, check_error()})} | {error, binary()}. check_snippets_in(Filename, Filter, Check) -> gleam@result:'try'( begin _pipe = simplifile:read(Filename), gleam@result:map_error(_pipe, fun gleam@string:inspect/1) end, fun(Content) -> {ok, begin _pipe@1 = extract_gleam_code(Content, Filter), gleam@list:map(_pipe@1, Check) end} end ). -file("/home/sbergen/source/checkmark/src/checkmark.gleam", 180). -spec with_tempdir( fun((binary()) -> {ok, list({ok, nil} | {error, check_error()})} | {error, binary()}) ) -> {ok, list({ok, nil} | {error, check_error()})} | {error, binary()}. with_tempdir(Operation) -> _pipe = (temporary:create( temporary:directory(), fun(Temp_dir) -> Operation(Temp_dir) end )), _pipe@1 = gleam@result:map_error(_pipe, fun gleam@string:inspect/1), gleam@result:flatten(_pipe@1). -file("/home/sbergen/source/checkmark/src/checkmark.gleam", 201). -spec create_file(binary(), boolean()) -> {ok, nil} | {error, check_error()}. create_file(Path, Allow_overwrite) -> case {Allow_overwrite, simplifile:create_file(Path)} of {true, {error, eexist}} -> {ok, nil}; {_, {ok, _}} -> {ok, nil}; {_, E} -> {error, {run_failed, gleam@string:inspect(E)}} end. -file("/home/sbergen/source/checkmark/src/checkmark.gleam", 191). -spec with_tempfile( binary(), boolean(), fun((binary()) -> {ok, nil} | {error, check_error()}) ) -> {ok, nil} | {error, check_error()}. with_tempfile(Path, Allow_overwrite, Operation) -> gleam@result:'try'( create_file(Path, Allow_overwrite), fun(_) -> exception_ffi:defer( fun() -> simplifile_erl:delete(Path) end, fun() -> Operation(Path) end ) end ). -file("/home/sbergen/source/checkmark/src/checkmark.gleam", 209). -spec run_gleam(binary(), list(binary()), fun((binary()) -> HJA)) -> {ok, nil} | {error, HJA}. run_gleam(Directory, Args, Make_error) -> case shellout:command(<<"gleam"/utf8>>, Args, Directory, []) of {ok, _} -> {ok, nil}; {error, {_, E}} -> {error, Make_error(E)} end. -file("/home/sbergen/source/checkmark/src/checkmark.gleam", 220). -spec to_args(operation(), binary()) -> list(binary()). to_args(Op, Module) -> case Op of build -> [<<"build"/utf8>>]; check -> [<<"check"/utf8>>]; run -> [<<"run"/utf8>>, <<"--module"/utf8>>, Module] end. -file("/home/sbergen/source/checkmark/src/checkmark.gleam", 153). -spec check_in_dir(binary(), binary(), binary(), boolean(), operation()) -> {ok, nil} | {error, check_error()}. check_in_dir(Code, Package_dir, Filename, Allow_overwrite, Operation) -> Source_dir = filepath:join(Package_dir, <<"src"/utf8>>), Source_file = filepath:join(Source_dir, Filename), with_tempfile( Source_file, Allow_overwrite, fun(File) -> gleam@result:'try'( begin _pipe = simplifile:write(File, Code), gleam@result:map_error( _pipe, fun(E) -> {run_failed, gleam@string:inspect(E)} end ) end, fun(_) -> case gleam@string:split(Filename, <<"."/utf8>>) of [] -> {error, {check_failed, <<"Invalid source file name: "/utf8, Filename/binary>>}}; [Module | _] -> run_gleam( Package_dir, to_args(Operation, Module), fun(Field@0) -> {check_failed, Field@0} end ) end end ) end ). -file("/home/sbergen/source/checkmark/src/checkmark.gleam", 69). -spec check_in_current_package(check_config(), binary()) -> {ok, list({ok, nil} | {error, check_error()})} | {error, binary()}. check_in_current_package(Config, Filename) -> check_snippets_in( erlang:element(2, Config), erlang:element(3, Config), fun(Snippet) -> check_in_dir( Snippet, <<"."/utf8>>, Filename, false, erlang:element(4, Config) ) end ). -file("/home/sbergen/source/checkmark/src/checkmark.gleam", 98). -spec set_up_package(binary(), list(binary())) -> {ok, binary()} | {error, binary()}. set_up_package(Tmp_dir, Dependencies) -> gleam@result:'try'( run_gleam( Tmp_dir, [<<"new"/utf8>>, <<"checkmark_tmp"/utf8>>], fun gleam@function:identity/1 ), fun(_) -> Package_dir = filepath:join(Tmp_dir, <<"checkmark_tmp"/utf8>>), gleam@result:'try'(case Dependencies of [] -> {ok, nil}; _ -> run_gleam( Package_dir, [<<"add"/utf8>> | Dependencies], fun gleam@function:identity/1 ) end, fun(_) -> {ok, Package_dir} end) end ). -file("/home/sbergen/source/checkmark/src/checkmark.gleam", 81). -spec check_in_tmp_package(check_config(), list(binary())) -> {ok, list({ok, nil} | {error, check_error()})} | {error, binary()}. check_in_tmp_package(Config, Dependencies) -> with_tempdir( fun(Temp_dir) -> gleam@result:'try'( set_up_package(Temp_dir, Dependencies), fun(Package_dir) -> check_snippets_in( erlang:element(2, Config), erlang:element(3, Config), fun(Snippet) -> check_in_dir( Snippet, Package_dir, <<"checkmark_tmp"/utf8, ".gleam"/utf8>>, true, erlang:element(4, Config) ) end ) end ) end ).