# `Skeg.RESP`

RESP2 and RESP3 wire codec.

The codec is pure: it never touches a socket. `decode/2` reads one frame
from the front of the bytes it is given, continuing from a decoder state.
When the frame is not complete it returns the state holding its progress;
pass that state the next bytes from the socket and it resumes where it
stopped, so a reply arriving in many fragments is examined once, not once
per fragment:

    case Skeg.RESP.decode(bytes, state) do
      {:ok, value, rest, state} -> # use value; rest starts the next frame
      {:incomplete, state} -> # read more bytes and decode them with state
      {:error, reason} -> # the stream is corrupt: close the connection
    end

Values decode to:

  * simple and bulk strings: binaries
  * nulls (`_`, `$-1`, `*-1`): `nil`
  * integers and big numbers: integers
  * booleans: `true` and `false`
  * doubles: floats, or `:infinity`, `:neg_infinity`, `:nan`
  * arrays: lists
  * maps: `{:map, [{key, value}]}`, pairs in the order the server sent them
  * sets: `{:set, list}`; pushes: `{:push, list}`
  * verbatim strings: `{:verbatim, format, data}`
  * errors (`-` and `!`): `%Skeg.Error{kind: :server}`. They are values,
    not decode failures: a batch reply can hold an error next to results.

No atom is ever created from remote input.

The decoder enforces the same limits as the skeg server's own parser, so a
misbehaving peer cannot make the client buffer or nest without bound.
Streamed aggregates (`*?`), streamed strings (`$?`) and attributes (`|`)
are not supported; skeg never sends them.

# `reason`

```elixir
@type reason() ::
  {:unknown_marker, byte()}
  | :invalid_utf8
  | {:invalid_integer, String.t()}
  | {:invalid_length, integer()}
  | :header_too_long
  | {:bulk_too_large, integer()}
  | {:aggregate_too_large, integer()}
  | :nesting_too_deep
  | :missing_terminator
  | :invalid_boolean
  | {:invalid_double, String.t()}
  | :invalid_verbatim
  | :unexpected_payload
```

Why a byte stream cannot be RESP. The connection must be closed.

# `t`

```elixir
@opaque t()
```

# `value`

```elixir
@type value() ::
  binary()
  | nil
  | integer()
  | boolean()
  | float()
  | :infinity
  | :neg_infinity
  | :nan
  | [value()]
  | {:map, [{value(), value()}]}
  | {:set, [value()]}
  | {:push, [value()]}
  | {:verbatim, String.t(), binary()}
  | Skeg.Error.t()
```

# `decode`

```elixir
@spec decode(binary(), t()) ::
  {:ok, value(), binary(), t()} | {:incomplete, t()} | {:error, reason()}
```

Appends `bytes` to what `state` has buffered and decodes one frame.

`{:incomplete, state}` is returned only while the bytes could still become
a valid frame: oversized lengths and malformed headers are rejected as soon
as they are visible, without waiting for the bytes they announce. The state
returned with a complete frame is a fresh decoder.

# `encode`

```elixir
@spec encode([binary()]) :: iodata()
```

Encodes a command as an array of bulk strings, the only request form the
server accepts. Every argument must be a binary and is sent byte for byte;
anything else raises `ArgumentError` rather than being stringified.

# `examined_bytes`

```elixir
@spec examined_bytes(t()) :: non_neg_integer()
```

Bytes the decoder has examined since it was created. Every byte is
examined at most once, however the input is fragmented.

# `max_aggregate_count`

Largest number of elements in an aggregate, or of pairs in a map.

# `max_bulk_bytes`

Largest bulk string, blob error or verbatim payload, in bytes.

# `max_depth`

Deepest aggregate nesting. A top-level scalar is at depth 0.

# `max_header_bytes`

Longest length, count or integer line, without its CRLF.

# `new`

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

A decoder that has not read anything yet.

---

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