# `ASDF.Source`
[🔗](https://github.com/baoulo/asdf-elixir/blob/v0.1.0/lib/asdf/source.ex#L1)

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.

# `t`

```elixir
@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` — `:file` or `:binary`
  * `:path` — filesystem path, or `nil` for a binary
  * `:io` — raw file descriptor, or `nil` for a binary
  * `:data` — the bytes, or `nil` for a file
  * `:size` — number of bytes

# `close`

```elixir
@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

# `label`

```elixir
@spec label(t()) :: String.t() | :binary
```

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

# `open`

```elixir
@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

# `pread`

```elixir
@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

# `reopen`

```elixir
@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"}

---

*Consult [api-reference.md](api-reference.md) for complete listing*
