Current section

Files

Jump to
fswalk src fswalk.erl
Raw

src/fswalk.erl

-module(fswalk).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/fswalk.gleam").
-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]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-type non() :: any().
-type som() :: any().
-opaque walk_builder(GQJ, GQK) :: {walk_builder,
gleam@option:option(list(fun((entry()) -> boolean()))),
gleam@option:option(binary())} |
{gleam_phantom, GQJ, GQK}.
-type stat() :: {stat, boolean()}.
-type entry() :: {entry, binary(), stat()}.
-file("src/fswalk.gleam", 22).
?DOC(" Create a new WalkBuilder\n").
-spec builder() -> walk_builder(non(), non()).
builder() ->
{walk_builder, none, none}.
-file("src/fswalk.gleam", 28).
?DOC(" Create a new WalkBuilder bound to a directory path to walk\n").
-spec with_path(walk_builder(GQN, any()), binary()) -> walk_builder(GQN, som()).
with_path(Builder, Path) ->
{walk_builder, erlang:element(2, Builder), {some, Path}}.
-file("src/fswalk.gleam", 37).
?DOC(" Create a new WalkBuilder adding an additional entry filter function.\n").
-spec with_traversal_filter(
walk_builder(any(), GQU),
fun((entry()) -> boolean())
) -> walk_builder(som(), GQU).
with_traversal_filter(Builder, Traversal_filter) ->
{walk_builder,
{some,
lists:append(
gleam@option:unwrap(erlang:element(2, Builder), []),
[Traversal_filter]
)},
erlang:element(3, Builder)}.
-file("src/fswalk.gleam", 81).
-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}}.
-file("src/fswalk.gleam", 92).
-spec filter_many(list(GSK), list(fun((GSK) -> boolean()))) -> list(GSK).
filter_many(Xs, Filters) ->
_pipe = Xs,
gleam@list:filter(
_pipe,
fun(Entry) -> gleam@list:all(Filters, fun(F) -> F(Entry) end) end
).
-file("src/fswalk.gleam", 99).
?DOC(" Walk the filesystem, starting at the provided path.\n").
-spec walk_path(gleam_community@path:path(), list(fun((entry()) -> boolean()))) -> gleam@yielder:yielder({ok,
entry()} |
{error, simplifile:file_error()}).
walk_path(Pth, Traversal_filters) ->
_pipe = gleam@yielder:once(
fun() ->
simplifile_erl:read_directory(gleam_community@path:to_string(Pth))
end
),
gleam@yielder:flat_map(
_pipe,
fun(Readdir_result) -> _pipe@1 = Readdir_result,
_pipe@2 = gleam@result:map_error(
_pipe@1,
fun(E) -> gleam@yielder: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@yielder:concat(
[gleam@yielder: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
).
-file("src/fswalk.gleam", 53).
?DOC(" Walks the filesystem lazily.\n").
-spec walk(walk_builder(any(), som())) -> gleam@yielder:yielder({ok, entry()} |
{error, simplifile:file_error()}).
walk(Builder) ->
{walk_builder, Traversal_filter_opt, Path_opt} = Builder,
Root_path@1 = case Path_opt of
{some, Root_path} -> Root_path;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"fswalk"/utf8>>,
function => <<"walk"/utf8>>,
line => 56,
value => _assert_fail,
start => 1350,
'end' => 1387,
pattern_start => 1361,
pattern_end => 1376})
end,
Traversal_filters = case Traversal_filter_opt of
{some, Xs} ->
Xs;
_ ->
[]
end,
walk_path(gleam_community@path:from_string(Root_path@1), Traversal_filters).