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

The `#ASDF <file-format-version>` line and the comment lines that follow it.

File format version `1.0.z` is the only layout this library parses. The
optional `#ASDF_STANDARD <version>` comment records the specification
version. It is not part of the header grammar in the file-layout chapter;
the versioning chapter shows it as the comment that carries that version.

# `t`

```elixir
@type t() :: %ASDF.Header{
  bytes: non_neg_integer(),
  comments: [String.t()],
  format_version: version(),
  standard_version: version() | nil
}
```

One parsed header.

  * `:format_version` — the `#ASDF` version
  * `:standard_version` — the `#ASDF_STANDARD` comment, or `nil`
  * `:bytes` — bytes consumed, including the header newline and comments
  * `:comments` — comment lines after the format line, including `#`
    and any `#ASDF_STANDARD` line

# `version`

```elixir
@type version() :: {non_neg_integer(), non_neg_integer(), non_neg_integer()}
```

A `major.minor.patch` triple. Each component is a non-negative integer
without a leading zero in the file text.

# `parse`

```elixir
@spec parse(binary(), keyword()) :: {:ok, t()} | {:error, ASDF.Error.t()}
```

Parses a header from the front of `binary`.

`binary` is the start of a file. `opts` accepts `:limits`, an
`%ASDF.Limits{}` or a keyword list of its fields.

Returns `{:ok, header}`. `:bytes` includes the header newline and the
comment lines that follow it. Bytes after the comments are left for the
tree or the first block.

Returns `{:error, %ASDF.Error{}}` and does not raise. Reasons:

  * `:not_asdf` — the input does not start with `#ASDF `
  * `:truncated_header` — the header line has no newline
  * `:unsupported_format_version` — the version is not `1.0.z`
  * `:duplicate_standard_version` — more than one `#ASDF_STANDARD` line
  * `:line_too_long` — a header line exceeds `max_header_line_bytes`
  * `:too_many_comments` — more comments than `max_comment_lines`
  * `:invalid_encoding` — a header line is not UTF-8
  * `:invalid_limit` or `:unknown_limit` — `:limits` is not usable

## Examples

    iex> {:ok, header} = ASDF.Header.parse("#ASDF 1.0.0\n#ASDF_STANDARD 1.5.0\n# note\n")
    ...> {header.format_version, header.standard_version, header.comments, header.bytes}
    {{1, 0, 0}, {1, 5, 0}, ["#ASDF_STANDARD 1.5.0", "# note"], 40}

    iex> {:error, error} = ASDF.Header.parse("nope\n")
    ...> error.reason
    :not_asdf

    iex> {:error, error} = ASDF.Header.parse("#ASDF 2.0.0\n")
    ...> error.reason
    :unsupported_format_version

    iex> text = "#ASDF 1.0.0\n#ASDF_STANDARD 1.5.0\n#ASDF_STANDARD 1.6.0\n"
    ...> {:error, error} = ASDF.Header.parse(text)
    ...> error.reason
    :duplicate_standard_version

# `parse!`

```elixir
@spec parse!(binary(), keyword()) :: t()
```

Parses a header and returns the struct.

`binary` and `opts` are the same as `parse/2`. Returns the `%ASDF.Header{}`.
Raises `ASDF.Error` for the same reasons `parse/2` returns an error.

## Examples

    iex> ASDF.Header.parse!("#ASDF 1.0.0\n").format_version
    {1, 0, 0}

    iex> ASDF.Header.parse!("nope\n")
    ** (ASDF.Error) input does not start with an #ASDF file-format header

---

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