Current section
Files
Jump to
Current section
Files
src/fswalk.erl
-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(FWI, FWJ) :: {walk_builder,
gleam@option:option(list(fun((entry()) -> boolean()))),
gleam@option:option(binary())} |
{gleam_phantom, FWI, FWJ}.
-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(FWM, any()), binary()) -> walk_builder(FWM, som()).
with_path(Builder, Path) ->
{walk_builder, erlang:element(2, Builder), {some, Path}}.
-spec with_traversal_filter(
walk_builder(any(), FWT),
fun((entry()) -> boolean())
) -> walk_builder(som(), FWT).
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:verify_is_directory(Filename),
gleam@result:unwrap(_pipe, false)
end}}.
-spec filter_many(list(FYR), list(fun((FYR) -> boolean()))) -> list(FYR).
filter_many(Xs, Filters) ->
_pipe = Xs,
gleam@list:filter(
_pipe,
fun(Entry) -> gleam@list:all(Filters, fun(F) -> F(Entry) end) end
).
-spec unwrap_map_both(
{ok, FXF} | {error, FXG},
fun((FXG) -> FXJ),
fun((FXF) -> FXJ)
) -> FXJ.
unwrap_map_both(R, Map_err, Map_ok) ->
case R of
{ok, X} ->
Map_ok(X);
{error, E} ->
Map_err(E)
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:read_directory(gleam_community@path:to_string(Pth))
end
),
gleam@iterator:flat_map(
_pipe,
fun(Readdir_result) ->
unwrap_map_both(
Readdir_result,
fun(E) -> gleam@iterator:once(fun() -> {error, E} end) end,
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@1 = simplifile:verify_is_directory(
gleam_community@path:to_string(It)
),
gleam@result:unwrap(_pipe@1, 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
)
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).