Current section

Files

Jump to
gladvent src gladvent@internal@file.erl
Raw

src/gladvent@internal@file.erl

-module(gladvent@internal@file).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function]).
-export([open_file_exclusive/1, write/2, do_with_file/2]).
-export_type([io_device/0]).
-type io_device() :: any().
-spec open_file_exclusive(binary()) -> {ok, io_device()} |
{error, simplifile:file_error()}.
open_file_exclusive(S) ->
gladvent_ffi:open_file_exclusive(S).
-spec write(io_device(), binary()) -> {ok, nil} |
{error, simplifile:file_error()}.
write(Iod, S) ->
gladvent_ffi:write(Iod, S).
-spec defer(fun(() -> any()), fun(() -> IXH)) -> IXH.
defer(Later, Now) ->
Res = Now(),
Later(),
Res.
-spec do_with_file(binary(), fun((io_device()) -> IXD)) -> {ok, IXD} |
{error, simplifile:file_error()}.
do_with_file(Filename, F) ->
gleam@result:map(
gladvent_ffi:open_file_exclusive(Filename),
fun(File) ->
defer(fun() -> _assert_subject = gladvent_ffi:close_iodevice(File),
{ok, nil} = case _assert_subject of
{ok, nil} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"gladvent/internal/file"/utf8>>,
function => <<"do_with_file"/utf8>>,
line => 25})
end end, fun() -> F(File) end)
end
).