-module(gladvent@internal@file). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -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 do_with_file(binary(), fun((io_device()) -> MQT)) -> {ok, MQT} | {error, simplifile:file_error()}. do_with_file(Filename, F) -> gleam@result:map( gladvent_ffi:open_file_exclusive(Filename), fun(File) -> gladvent@internal@util: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 => 26}) end end, fun() -> F(File) end ) end ).