# `CRAM.FileDefinition`
[🔗](https://github.com/baoulo/cram-elixir/blob/v0.1.0/lib/cram/file_definition.ex#L1)

The 26-byte CRAM file definition (CRAMv3 §6).

Versions other than 3.0 and 3.1 are reported and not walked. CRAM 2.1 uses
a different container layout; this struct only records the bytes.

# `t`

```elixir
@type t() :: %CRAM.FileDefinition{file_id: binary(), major: byte(), minor: byte()}
```

The file definition.

  * `:major` — major version byte
  * `:minor` — minor version byte
  * `:file_id` — the 20 raw file-id bytes

    iex> %CRAM.FileDefinition{major: 3, minor: 0, file_id: "cram-elixir-fixture!"}.major
    3

# `version`

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

`{major, minor}` from the two version bytes.

# `display_id`

```elixir
@spec display_id(t()) :: String.t()
```

Printable file id, or hex when the 20 bytes are not graphic ASCII.

`definition` is a `%CRAM.FileDefinition{}`. The result is a display string.
Callers that need the raw id use the `file_id` field. Does not raise.

    iex> definition = %CRAM.FileDefinition{major: 3, minor: 0, file_id: "cram-elixir-fixture!"}
    iex> CRAM.FileDefinition.display_id(definition)
    "cram-elixir-fixture!"
    iex> CRAM.FileDefinition.display_id(%{definition | file_id: <<0::160>>})
    "0000000000000000000000000000000000000000"

# `read`

```elixir
@spec read(CRAM.Source.t()) :: {:ok, t()} | {:error, CRAM.Error.t()}
```

Read the definition at the start of `source`.

Returns `{:ok, definition}` when the first 26 bytes start with `CRAM`.
A shorter source is `reason: :truncated`. Any other 26 bytes are
`reason: :bad_magic`. Does not raise.

    iex> {:ok, source} = CRAM.Source.open("test/fixtures/tiny.cram")
    iex> {:ok, definition} = CRAM.FileDefinition.read(source)
    iex> CRAM.FileDefinition.version(definition)
    {3, 0}
    iex> {:ok, closed} = CRAM.Source.close(source)
    iex> closed.closed
    true

    iex> {:ok, source} = CRAM.Source.open({:binary, String.duplicate("N", 26)})
    iex> {:error, error} = CRAM.FileDefinition.read(source)
    iex> error.reason
    :bad_magic

# `size`

```elixir
@spec size() :: 26
```

Byte length of a file definition.

    iex> CRAM.FileDefinition.size()
    26

# `supported?`

```elixir
@spec supported?(t()) :: boolean()
```

True for CRAM 3.0 and 3.1.

Other versions, including 2.1, are false. This does not raise.

    iex> CRAM.FileDefinition.supported?(%CRAM.FileDefinition{major: 3, minor: 0, file_id: <<0::160>>})
    true
    iex> CRAM.FileDefinition.supported?(%CRAM.FileDefinition{major: 2, minor: 1, file_id: <<0::160>>})
    false

# `version`

```elixir
@spec version(t()) :: version()
```

`{major, minor}` from `definition`.

    iex> CRAM.FileDefinition.version(%CRAM.FileDefinition{major: 3, minor: 1, file_id: <<0::160>>})
    {3, 1}

---

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