-module(fswalk). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([builder/0, with_path/2, with_traversal_filter/2, walk/1]). -export_type([non/0, som/0, walk_builder/2, stat/0, entry/0]). -type non() :: any(). -type som() :: any(). -opaque walk_builder(FYP, FYQ) :: {walk_builder, gleam@option:option(list(fun((entry()) -> boolean()))), gleam@option:option(binary())} | {gleam_phantom, FYP, FYQ}. -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(FYT, any()), binary()) -> walk_builder(FYT, som()). with_path(Builder, Path) -> {walk_builder, erlang:element(2, Builder), {some, Path}}. -spec with_traversal_filter( walk_builder(any(), FZA), fun((entry()) -> boolean()) ) -> walk_builder(som(), FZA). with_traversal_filter(Builder, Traversal_filter) -> {walk_builder, {some, gleam@list:append( gleam@option:unwrap(erlang:element(2, Builder), []), [Traversal_filter] )}, erlang:element(3, Builder)}. -spec to_entry(gleam_community@path:path()) -> entry(). to_entry(Path) -> Filename = gleam_community@path:to_string(Path), {entry, Filename, {stat, begin _pipe = simplifile_erl:is_directory(Filename), gleam@result:unwrap(_pipe, false) end}}. -spec filter_many(list(GAT), list(fun((GAT) -> boolean()))) -> list(GAT). filter_many(Xs, Filters) -> _pipe = Xs, gleam@list:filter( _pipe, fun(Entry) -> gleam@list:all(Filters, fun(F) -> F(Entry) end) end ). -spec walk_path(gleam_community@path:path(), list(fun((entry()) -> boolean()))) -> gleam@iterator:iterator({ok, entry()} | {error, simplifile:file_error()}). walk_path(Pth, Traversal_filters) -> _pipe = gleam@iterator:once( fun() -> simplifile_erl:read_directory(gleam_community@path:to_string(Pth)) end ), gleam@iterator:flat_map( _pipe, fun(Readdir_result) -> _pipe@1 = Readdir_result, _pipe@2 = gleam@result:map_error( _pipe@1, fun(E) -> gleam@iterator:once(fun() -> {error, E} end) end ), _pipe@4 = gleam@result:map( _pipe@2, fun(Basenames) -> Paths = gleam@list:map( Basenames, fun(Basename) -> gleam_community@path:append_string(Pth, Basename) end ), {All_entries, Dir_entries} = gleam@list:fold( Paths, {[], []}, fun(Acc, It) -> Entry = to_entry(It), Is_dir = begin _pipe@3 = simplifile_erl:is_directory( gleam_community@path:to_string(It) ), gleam@result:unwrap(_pipe@3, false) end, {[Entry | erlang:element(1, Acc)], case Is_dir of true -> [Entry | erlang:element(2, Acc)]; false -> erlang:element(2, Acc) end} end ), Traverse_dirs = filter_many(Dir_entries, Traversal_filters), gleam@iterator:concat( [gleam@iterator:from_list( gleam@list:map( All_entries, fun(It@1) -> {ok, It@1} end ) ) | gleam@list:map( Traverse_dirs, fun(Ent) -> walk_path( gleam_community@path:from_string( erlang:element(2, Ent) ), Traversal_filters ) end )] ) end ), gleam@result:unwrap_both(_pipe@4) end ). -spec walk(walk_builder(any(), som())) -> gleam@iterator:iterator({ok, entry()} | {error, simplifile:file_error()}). walk(Builder) -> {walk_builder, Traversal_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 => 56}) end, Traversal_filters = case Traversal_filter_opt of {some, Xs} -> Xs; _ -> [] end, walk_path(gleam_community@path:from_string(Root_path), Traversal_filters).