#redis #serde #deserialize #protocols #de

seredies

An implementation of the Redis Protocol as a serde (de)serializer

3 releases (stable)

1.0.1 May 16, 2023
1.0.0 May 15, 2023
0.0.1 May 14, 2023

#1961 in Encoding

MPL-2.0 license

170KB
4.5K SLoC

seredies

Seredies is a Redis Protocol implementation for serde.


lib.rs:

seredies is a low-level implementation of RESP, the Redis Serialization Protocol. It is implemented using the [serde] data model, as a Serializer and Deserializer.

See the [de] and [ser] modules for examples on how to serialize and deserialize RESP data.

Faithful

seredies is a mostly faithful serde implementation of RESP. This means that it (mostly) doesn't try to go above and beyond what the RESP data model can express, which is mostly strings, integers, and arrays. In particular it's not capable of deserializing structs, maps, or complex enums. Instead, seredies provides a collection of components, which translate common patterns into Redis's minimal data model. This ensures that developers should never be surprised by the deserializer trying to do something unexpectedly "clever", but can opt-in to more streamlined behavior.

Supported types

These are the types supported by the serializer and deserializer.

  • bool (treated as an integer 0 or 1).
  • All integers (though note that RESP only supports integers in the signed 64 bit range).
  • Unit (treated as null).
  • Sequences, tuples, and tuple structs.
  • Bytes and string types.
  • See the RedisString component for a wrapper type that converts any primitive value to or from a Redis string.
  • RESP is totally binary safe, so it's easy to deserialize &str and other borrowed data from the payload.
  • Result (see below).
  • Option: similar to JSON, an Option is handled as either a null or as an untagged value
  • Unit variants: these are encoded as strings.

Unsupported types

  • Floats.
  • Consider RedisString for the common case that Redis is treating your float data as a string.
  • Maps, structs, complex enums.
  • Consider KeyValuePairs for the common case that your key-value data is being treated by Redis as a flattened array of key-value pairs.

If you're trying to serialize a Redis command, consider additionally using the Command component; it handles converting all of the command data into a list of strings, using conventions that follow the typical redis command conventions.

Errors and Results

RESP includes an error type, which is delivered in the response when something has gone wrong. By default, when deserializing, this error type is treated as a deserialize error, and appears as the Error::Redis variant when encountered. However, you can handle them by (de)serializing a Result directly; in this case, the [Ok] variant will contain the data, and a successfully (de)serialized [Err] variant will contain a redis error.

Additionally, seredies ubiquitously uses the simple string "OK" to signal an uninteresting success. This pattern is so common that seredies supports (de)serializing it directly to an Ok(()) Result value.

Dependencies

~0.6–1.3MB
~27K SLoC