Current section
Files
Jump to
Current section
Files
src/tale@paths.erl
-module(tale@paths).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/tale/paths.gleam").
-export([parent_directory/1, ensure_parent_dirs/1]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-file("src/tale/paths.gleam", 24).
?DOC(" Parent directory function.\n").
-spec parent_directory(binary()) -> gleam@option:option(binary()).
parent_directory(Path) ->
Segments = gleam@string:split(Path, <<"/"/utf8>>),
case erlang:length(Segments) of
Len when Len =< 1 ->
none;
Len@1 ->
Keep = Len@1 - 1,
Dir = begin
_pipe = Segments,
_pipe@1 = gleam@list:take(_pipe, Keep),
gleam@string:join(_pipe@1, <<"/"/utf8>>)
end,
case Dir of
<<""/utf8>> ->
none;
Other ->
{some, Other}
end
end.
-file("src/tale/paths.gleam", 9).
?DOC(" Ensure parent dirs function.\n").
-spec ensure_parent_dirs(binary()) -> {ok, nil} | {error, binary()}.
ensure_parent_dirs(Path) ->
case parent_directory(Path) of
none ->
{ok, nil};
{some, Dir} ->
_pipe = simplifile:create_directory_all(Dir),
gleam@result:map_error(
_pipe,
fun(Err) ->
<<<<<<"Unable to create directory "/utf8, Dir/binary>>/binary,
": "/utf8>>/binary,
(simplifile:describe_error(Err))/binary>>
end
)
end.