-module(fswalk). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([builder/0, with_path/2, with_filter/2, walk/1, each/2, map/2, fold/3, only_dirs/1, only_files/1]). -export_type([non/0, som/0, walk_builder/2, stat/0, entry/0]). -opaque non() :: any(). -opaque som() :: any(). -opaque walk_builder(FYT, FYU) :: {walk_builder, gleam@option:option(list(fun((entry()) -> boolean()))), gleam@option:option(binary())} | {gleam_phantom, FYT, FYU}. -type stat() :: {stat, boolean()}. -type entry() :: {entry, binary(), stat()}. -spec builder() -> walk_builder(non(), non()). builder() -> {walk_builder, none, none}. -spec with_path(walk_builder(FYX, any()), binary()) -> walk_builder(FYX, som()). with_path(Builder, Path) -> {walk_builder, erlang:element(2, Builder), {some, Path}}. -spec with_filter(walk_builder(any(), FZE), fun((entry()) -> boolean())) -> walk_builder(som(), FZE). with_filter(Builder, Filter) -> {walk_builder, {some, gleam@list:append( gleam@option:unwrap(erlang:element(2, Builder), []), [Filter] )}, erlang:element(3, Builder)}. -spec to_entry(binary()) -> entry(). to_entry(Filename) -> {entry, Filename, {stat, simplifile:is_directory(Filename)}}. -spec path_to_entry(gleam_community@path:path()) -> entry(). path_to_entry(Pth) -> to_entry(gleam_community@path:to_string(Pth)). -spec ok(FZN) -> {ok, FZN} | {error, any()}. ok(X) -> {ok, X}. -spec filter_many(list(GBN), list(fun((GBN) -> boolean()))) -> list(GBN). filter_many(Xs, Filters) -> _pipe = Xs, gleam@list:filter( _pipe, fun(Entry) -> gleam@list:all(Filters, fun(F) -> F(Entry) end) end ). -spec to_ok_iter(list(GBU)) -> gleam@iterator:iterator({ok, GBU} | {error, any()}). to_ok_iter(Xs) -> _pipe = Xs, _pipe@1 = gleam@list:map(_pipe, fun ok/1), gleam@iterator:from_list(_pipe@1). -spec walk_path(gleam_community@path:path(), list(fun((entry()) -> boolean()))) -> gleam@iterator:iterator({ok, entry()} | {error, simplifile:file_error()}). walk_path(Pth, Filters) -> _pipe = gleam@iterator:once( fun() -> simplifile:read_directory(gleam_community@path:to_string(Pth)) end ), gleam@iterator:flat_map(_pipe, fun(Readdir_result) -> case Readdir_result of {error, X} -> gleam@iterator:once(fun() -> {error, X} end); {ok, Filenames} -> {Filepaths, Folderpaths} = gleam@list:fold( Filenames, {[], []}, fun(Acc, F) -> Filepath = gleam_community@path:append_string( Pth, F ), case simplifile:is_directory( gleam_community@path:to_string(Filepath) ) of true -> {erlang:element(1, Acc), [Filepath | erlang:element(2, Acc)]}; false -> {[Filepath | erlang:element(1, Acc)], erlang:element(2, Acc)} end end ), Ok_filtered_files = begin _pipe@1 = Filepaths, _pipe@2 = gleam@list:map(_pipe@1, fun path_to_entry/1), _pipe@3 = filter_many(_pipe@2, Filters), to_ok_iter(_pipe@3) end, Filtered_folders = begin _pipe@4 = Folderpaths, _pipe@5 = gleam@list:map(_pipe@4, fun path_to_entry/1), filter_many(_pipe@5, Filters) end, Ok_filtered_folders = to_ok_iter(Filtered_folders), gleam@iterator:concat( [Ok_filtered_files, Ok_filtered_folders | gleam@list:map( Filtered_folders, fun(Ent) -> walk_path( gleam_community@path:from_string( erlang:element(2, Ent) ), Filters ) end )] ) end end). -spec walk(walk_builder(any(), som())) -> gleam@iterator:iterator({ok, entry()} | {error, simplifile:file_error()}). walk(Builder) -> {walk_builder, Filter_opt, Path_opt} = Builder, {some, Root_path} = case Path_opt of {some, _} -> Path_opt; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Assertion pattern match failed"/utf8>>, value => _assert_fail, module => <<"fswalk"/utf8>>, function => <<"walk"/utf8>>, line => 52}) end, Filters = case Filter_opt of {some, Xs} -> Xs; _ -> [] end, walk_path(gleam_community@path:from_string(Root_path), Filters). -spec each(gleam@iterator:iterator(GDR), fun((GDR) -> any())) -> nil. each(It, Fun) -> _pipe = It, gleam@iterator:each(_pipe, Fun). -spec map(gleam@iterator:iterator(GDT), fun((GDT) -> GDU)) -> gleam@iterator:iterator(GDU). map(It, Fun) -> _pipe = It, gleam@iterator:map(_pipe, Fun). -spec fold(gleam@iterator:iterator(GDV), GAF, fun((GAF, GDV) -> GAF)) -> GAF. fold(It, Init, Fun) -> _pipe = It, gleam@iterator:fold(_pipe, Init, Fun). -spec only_dirs(entry()) -> boolean(). only_dirs(Ent) -> erlang:element(2, erlang:element(3, Ent)). -spec only_files(entry()) -> boolean(). only_files(Ent) -> not only_dirs(Ent).