Module seredies::de

source ·
Expand description

Serde deserializer for turning Redis RESP data into Rust data structures.

This module contains a faithful implementation of the Redis Serialization Protocol.

See the crate docs for an overview of how seredies maps the RESP data model to the serde data model.

Basic example

use serde::Deserialize;
use seredies::de::from_bytes;

let data = b"\
    *4\r\n\
    +OK\r\n\
    :24\r\n\
    $8\r\nBorrowed\r\n\
    *3\r\n\
        $-1\r\n\
        +Hello,\r\n\
        +World!\r\n\
";

#[derive(Deserialize, PartialEq, Eq, Debug)]
struct Data<'a>(
    String,
    i32,
    &'a str,
    Vec<Option<String>>,
);

let data: Data = from_bytes(data).expect("failed to deserialize");

assert_eq!(
    data,
    Data(
        "OK".to_owned(),
        24,
        "Borrowed",
        Vec::from([
            None,
            Some("Hello,".to_owned()),
            Some("World!".to_owned()),
        ]),
    ),
);

Error example

use seredies::de::{from_bytes, Error};

let error = b"-ERR unknown command \"helloworld\"\r\n";

// Normally, Redis errors appear as deserialize errors (in the same way that
// a parse error would appear):
let res: Result<Vec<i32>, Error> = from_bytes(error);
assert!(res.is_err());

// However, you can instead Deserialize the Result directly:
let data: Result<Vec<i32>, String> = from_bytes(error).expect("deserialize shouldn't fail");
assert_eq!(data, Err("ERR unknown command \"helloworld\"".to_owned()));

Result::Ok example

use seredies::de::from_bytes;

type BoringResult<'a> = Result<(), &'a str>;

let result: BoringResult = from_bytes(b"+OK\r\n")
    .expect("deserialize shouldn't fail");

assert_eq!(result, Ok(()));

let result: BoringResult = from_bytes(b"-ERR error message\r\n")
    .expect("deserialize shouldn't fail");

assert_eq!(result, Err("ERR error message"));

Modules

  • Low level parser implementations for RESP.

Structs

Enums

  • Errors that can occur while deserializing RESP data.

Functions

  • Deserialize a T object from a byte slice containing RESP data.
  • Deserialize a T object from a string containing RESP data.