CRAM.FileDefinition (cram v0.1.0)

Copy Markdown View Source

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.

Summary

Types

t()

The file definition.

{major, minor} from the two version bytes.

Functions

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

Read the definition at the start of source.

Byte length of a file definition.

True for CRAM 3.0 and 3.1.

{major, minor} from definition.

Types

t()

@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()

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

{major, minor} from the two version bytes.

Functions

display_id(file_definition)

@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(source)

@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()

@spec size() :: 26

Byte length of a file definition.

iex> CRAM.FileDefinition.size()
26

supported?(file_definition)

@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(file_definition)

@spec version(t()) :: version()

{major, minor} from definition.

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