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

LTF-8 integer codec from CRAM v3 section 2.3.

An LTF-8 value is a signed 64-bit integer. The prefix works like ITF-8 but
extends to nine bytes so a full 64-bit pattern fits. A first byte of `0xFF`
means the next eight bytes are the value. Encoding uses the shortest prefix
that can hold the two's-complement bit pattern.

# `decode_error`

```elixir
@type decode_error() :: :truncated
```

Why `decode/1` rejected the bytes. `:truncated` means the prefix promised
more bytes than `binary` contains.

# `decode`

```elixir
@spec decode(binary()) :: {:ok, integer(), binary()} | {:error, decode_error()}
```

Decode one LTF-8 integer from the front of `binary`.

    iex> {:ok, 128, <<9>>} = CRAM.Ltf8.decode(<<0x80, 128, 9>>)
    {:ok, 128, <<9>>}

    iex> CRAM.Ltf8.decode(<<0x80>>)
    {:error, :truncated}

# `decode!`

```elixir
@spec decode!(binary()) :: {integer(), binary()}
```

Decode one LTF-8 integer or raise `CRAM.Error`.

Returns `{value, rest}`. A short prefix raises `CRAM.Error` with
`reason: :truncated`.

    iex> CRAM.Ltf8.decode!(<<0>>)
    {0, <<>>}

    iex> CRAM.Ltf8.decode!(<<0x80>>)
    ** (CRAM.Error) cram: truncated: LTF-8 input ended early

# `encode`

```elixir
@spec encode(integer()) :: {:ok, binary()} | {:error, :overflow}
```

Encode a signed 64-bit integer.

    iex> CRAM.Ltf8.encode(0)
    {:ok, <<0>>}

    iex> CRAM.Ltf8.encode(128)
    {:ok, <<0x80, 128>>}

    iex> CRAM.Ltf8.encode(9_223_372_036_854_775_808)
    {:error, :overflow}

# `encode!`

```elixir
@spec encode!(integer()) :: binary()
```

Encode a signed 64-bit integer or raise `CRAM.Error`.

`value` must fit in a signed 64-bit integer. A value outside that range
raises `CRAM.Error` with `reason: :overflow`.

    iex> CRAM.Ltf8.encode!(128)
    <<0x80, 128>>

    iex> CRAM.Ltf8.encode!(9_223_372_036_854_775_808)
    ** (CRAM.Error) cram: overflow: LTF-8 value is outside int64

---

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