A readable byte range: a local file or an in-memory binary.
pread/3 never reads more than the caller asks for. Callers compute the
length before they call it. close/1 releases a file descriptor and is
safe to call twice.
Summary
Functions
Closes source.
Returns a label for source.
Opens source.
Reads length bytes at offset.
Opens a second view of the same bytes.
Types
@type t() :: %ASDF.Source{ data: binary() | nil, io: :file.io_device() | nil, kind: :file | :binary, path: String.t() | nil, size: non_neg_integer() }
One readable source.
:kind—:fileor:binary:path— filesystem path, ornilfor a binary:io— raw file descriptor, ornilfor a binary:data— the bytes, ornilfor a file:size— number of bytes
Functions
@spec close(t()) :: :ok
Closes source.
source is an %ASDF.Source{}. A file descriptor is closed and the result
of :file.close/1 is returned. A binary source returns :ok. Does not raise.
Examples
iex> {:ok, source} = ASDF.Source.open({:binary, "abcd"})
...> ASDF.Source.close(source)
:ok
Returns a label for source.
source is an %ASDF.Source{}. A binary source returns :binary. A file
source returns its path. Does not raise.
Examples
iex> {:ok, source} = ASDF.Source.open({:binary, "abcd"})
...> ASDF.Source.label(source)
:binary
@spec open(Path.t() | {:binary, binary()}) :: {:ok, t()} | {:error, ASDF.Error.t()}
Opens source.
source is a filesystem path or {:binary, bytes}. A path is opened with
:read, :binary, and :raw. Returns {:ok, source}.
A missing path is {:error, %ASDF.Error{reason: :io}}. Any other argument
is reason: :bad_source. Does not raise.
Examples
iex> {:ok, source} = ASDF.Source.open({:binary, "abcd"})
...> {source.kind, source.size, source.path, source.io}
{:binary, 4, nil, nil}
iex> {:error, error} = ASDF.Source.open(1)
...> error.reason
:bad_source
@spec pread(t(), non_neg_integer(), non_neg_integer()) :: {:ok, binary()} | {:error, ASDF.Error.t()}
Reads length bytes at offset.
source is an %ASDF.Source{}. offset and length are non-negative
integers. Returns {:ok, bytes}. A read that starts at size, or a
length of 0, returns {:ok, ""}. A read that starts past size is
{:error, %ASDF.Error{reason: :offset_out_of_range}}. A short read returns
the bytes that were present. Does not raise.
Examples
iex> {:ok, source} = ASDF.Source.open({:binary, "abcd"})
...> ASDF.Source.pread(source, 1, 2)
{:ok, "bc"}
iex> {:ok, source} = ASDF.Source.open({:binary, "abcd"})
...> {:error, error} = ASDF.Source.pread(source, 5, 1)
...> error.reason
:offset_out_of_range
@spec reopen(t()) :: {:ok, t()} | {:error, ASDF.Error.t()}
Opens a second view of the same bytes.
source is an %ASDF.Source{}. A binary is copied into a new struct. A
file gets a new descriptor, so closing one does not close the other.
Returns {:ok, source} or {:error, %ASDF.Error{reason: :io}} when the
path can no longer be opened. Does not raise.
Examples
iex> {:ok, source} = ASDF.Source.open({:binary, "abcd"})
...> {:ok, again} = ASDF.Source.reopen(source)
...> ASDF.Source.pread(again, 0, 4)
{:ok, "abcd"}