RESP2 and RESP3 protocol parser and encoder for Elixir.
Supports the full RESP2 and RESP3 specifications including all aggregate and streamed types. Continuation-based parsing for TCP streaming.
Parsing
{:ok, arr, ""} = Resp.parse("*2\r\n$3\r\nGET\r\n$3\r\nkey\r\n")
arr.data
#=> [%Resp.Bulk{data: "GET"}, %Resp.Bulk{data: "key"}]Encoding
Resp.encode_string("OK")
#=> "+OK\r\n"
Summary
Functions
Encodes any Elixir value to its RESP representation (default RESP3 mode).
Encodes any Elixir value with mode awareness.
Encodes an array header.
Encodes a RESP3 attribute header.
Encodes a RESP3 big number.
Encodes a RESP3 bulk error.
Encodes a RESP3 boolean.
Encodes a bulk string.
Encodes a RESP3 double.
Encodes an error.
Encodes an integer.
Encodes a RESP3 map header.
Encodes RESP3 null (_\r\n).
Encodes RESP2 null ($-1\r\n).
Encodes a RESP3 push header.
Encodes raw data (passthrough).
Encodes a RESP3 set header.
Encodes a simple string.
Encodes a RESP3 verbatim string with encoding prefix.
Generates server-info response for the HELLO command.
:resp2 returns an array, :resp3 returns a map.
Packs a list of arguments as a RESP2 array of bulk strings.
Parses a RESP command from binary data.
Parses a streamed RESP type header and returns a chunk function.
Parses any RESP2 or RESP3 typed value from binary data.
Functions
Encodes any Elixir value to its RESP representation (default RESP3 mode).
See encode_any/2 for mode-aware encoding.
Encodes any Elixir value with mode awareness.
In RESP2 mode: nil → $-1, booleans → integers.
In RESP3 mode: nil → _, booleans → #t/#f.
Encodes an array header.
Encodes a RESP3 attribute header.
Encodes a RESP3 big number.
Encodes a RESP3 bulk error.
Encodes a RESP3 boolean.
Encodes a bulk string.
Encodes a RESP3 double.
Encodes an error.
Encodes an integer.
Encodes a RESP3 map header.
@spec encode_null3() :: iodata()
Encodes RESP3 null (_\r\n).
@spec encode_null() :: iodata()
Encodes RESP2 null ($-1\r\n).
Encodes a RESP3 push header.
Encodes raw data (passthrough).
Encodes a RESP3 set header.
Encodes a simple string.
Encodes a RESP3 verbatim string with encoding prefix.
@spec hello(:resp2 | :resp3) :: iodata()
Generates server-info response for the HELLO command.
:resp2 returns an array, :resp3 returns a map.
@spec pack([String.Chars.t()]) :: iodata()
Packs a list of arguments as a RESP2 array of bulk strings.
@spec parse(binary()) :: {:ok, Resp.Array.t(), binary()} | {:continuation, function()}
Parses a RESP command from binary data.
Returns {:ok, %Resp.Array{}, rest} for RESP2 arrays and inline/telnet commands.
Returns {:continuation, fun} when more data is needed.
Raises Resp.ParseError on protocol violations.
Parses a streamed RESP type header and returns a chunk function.
Returns {:streamed, chunk_fun, rest} where chunk_fun is called with
subsequent data and returns {:chunk, data, rest} or {:done, rest}.
@spec parse_value(binary()) :: {:ok, Resp.String.t() | Resp.Error.t() | Resp.Integer.t() | Resp.Bulk.t() | Resp.Null.t() | Resp.Array.t() | Resp.Bool.t() | Resp.Double.t() | Resp.BigNumber.t() | Resp.BlobError.t() | Resp.VerbatimString.t() | Resp.Map.t() | Resp.Set.t() | Resp.Push.t() | Resp.Attribute.t(), binary()} | {:continuation, function()}
Parses any RESP2 or RESP3 typed value from binary data.
Returns {:ok, %Resp.*{}, rest} where the struct type matches the type byte.
Returns {:continuation, fun} when more data is needed.