Current section
Files
Jump to
Current section
Files
src/thrifty@file_io.erl
-module(thrifty@file_io).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/thrifty/file_io.gleam").
-export([read_binary/1, write_binary_to_path/2, ensure_dir/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/thrifty/file_io.gleam", 22).
?DOC(
" Read the binary contents at `path` returning a Gleam `BitArray`.\n"
"\n"
" Inputs\n"
" - `path`: filesystem path to the file to read.\n"
"\n"
" Outputs\n"
" - `Ok(BitArray)` containing the file contents on success.\n"
" - `Error(String)` with a human-readable reason on failure.\n"
"\n"
" Error modes\n"
" - The function converts Erlang atoms returned by the underlying call\n"
" into strings for easier consumption by tests and higher-level code.\n"
).
-spec read_binary(binary()) -> {ok, bitstring()} | {error, binary()}.
read_binary(Path) ->
case file:read_file(Path) of
{ok, Data} ->
{ok, Data};
{error, Reason} ->
{error, erlang:atom_to_binary(Reason)}
end.
-file("src/thrifty/file_io.gleam", 41).
?DOC(
" Write binary contents to `path`.\n"
"\n"
" Inputs\n"
" - `path`: filesystem path where to write the data.\n"
" - `data`: `BitArray` to write to disk.\n"
"\n"
" Outputs\n"
" - `Ok(Nil)` on success.\n"
" - `Error(String)` with a human-readable reason on failure.\n"
).
-spec write_binary_to_path(binary(), bitstring()) -> {ok, nil} |
{error, binary()}.
write_binary_to_path(Path, Data) ->
case file_io_ffi:write_file(Path, Data) of
{ok, _} ->
{ok, nil};
{error, Reason} ->
{error, erlang:atom_to_binary(Reason)}
end.
-file("src/thrifty/file_io.gleam", 54).
?DOC(
" Ensure a directory exists, creating it if necessary.\n"
"\n"
" Returns `Ok(Nil)` on success or `Error(String)` on failure.\n"
).
-spec ensure_dir(binary()) -> {ok, nil} | {error, binary()}.
ensure_dir(Path) ->
case file_io_ffi:make_dir(Path) of
{ok, _} ->
{ok, nil};
{error, Reason} ->
{error, erlang:atom_to_binary(Reason)}
end.