RESP

Type Alias RESP 

Source
pub type RESP = RESPType;
Expand description

Refer to RESPType. This is just an alias.

Aliased Type§

pub enum RESP {
    SimpleString(String),
    Error(String),
    Integer(i64),
    BulkString(Option<Vec<u8>>),
    Array(Option<Vec<RESPType>>),
}

Variants§

§

SimpleString(String)

Correspond to simple string in RESP. Also refer to simple! macro.

§Examples

use serde_resp::{de, ser, simple, RESP};

/// Serialization
let obj = simple!("OK".to_owned()); // equivalent to RESP::SimpleString("OK".to_owned());
let serialized = ser::to_string(&obj).unwrap();
assert_eq!("+OK\r\n".to_owned(), serialized);

/// Deserialization
let deserialized: RESP = de::from_str("+OK\r\n").unwrap();
assert_eq!(simple!("OK".to_owned()), deserialized);
§

Error(String)

Correspond to error string in RESP. Also refer to err_str! macro.

§Examples

use serde_resp::{de, err_str, ser, RESP};

/// Serialization
// Example 1
let obj = err_str!("ERR unknown command 'foobar'".to_owned()); // equivalent to RESP::Error(...)
let serialized = ser::to_string(&obj).unwrap();
assert_eq!("-ERR unknown command 'foobar'\r\n".to_owned(), serialized);

// Example 2
let obj = err_str!("WRONGTYPE Operation against a key holding the wrong kind of value".to_owned());
let serialized = ser::to_string(&obj).unwrap();
assert_eq!(
    "-WRONGTYPE Operation against a key holding the wrong kind of value\r\n".to_owned(),
    serialized
);

/// Deserialization
let deserialized: RESP = de::from_str("-ERR unknown command 'foobar'\r\n").unwrap();
assert_eq!(err_str!("ERR unknown command 'foobar'".to_owned()), deserialized);
§

Integer(i64)

Correspond to integer in RESP. Also refer to int! macro.

§Examples

use serde_resp::{de, int, ser, RESP};

/// Serialization
// Regular Example
let obj = int!(1000); // equivalent to RESP::Integer(1000);
let serialized = ser::to_string(&obj).unwrap();
assert_eq!(":1000\r\n".to_owned(), serialized);

/// Deserialization
let deserialized: RESP = de::from_str(":1000\r\n").unwrap();
assert_eq!(int!(1000), deserialized);
§

BulkString(Option<Vec<u8>>)

Correspond to bulk string in RESP. Use None for null bulk string and Some for non-null ones. Also refer to bulk! macro and bulk_null! macro.

According to specification, bulk string is binary-safe so it is NOT recommended to use ser::to_string (may cause Error::FromUtf8). Use ser::to_writer to write to a byte buffer instead.

§Examples

use serde_resp::{bulk, bulk_null, de, ser, RESP};

/// Serialization
// Regular Example
let obj = bulk!(b"foobar".to_vec()); // equivalent to RESP::BulkString(Some(...))
let serialized = ser::to_string(&obj).unwrap();
assert_eq!("$6\r\nfoobar\r\n".to_owned(), serialized);

// Empty
let obj = bulk!(b"".to_vec());
let serialized = ser::to_string(&obj).unwrap();
assert_eq!("$0\r\n\r\n".to_owned(), serialized);

// Null
let obj = bulk_null!(); // equivalent to RESP::BulkString(None)
let serialized = ser::to_string(&obj).unwrap();
assert_eq!("$-1\r\n".to_owned(), serialized);

/// Deserialization
// Regular Example
let deserialized: RESP = de::from_str("$6\r\nfoobar\r\n").unwrap();
assert_eq!(bulk!(b"foobar".to_vec()), deserialized);

// Empty
let deserialized: RESP = de::from_str("$0\r\n\r\n").unwrap();
assert_eq!(bulk!(b"".to_vec()), deserialized);

// Null
let deserialized: RESP = de::from_str("$-1\r\n").unwrap();
assert_eq!(bulk_null!(), deserialized);
§

Array(Option<Vec<RESPType>>)

Correspond to array in RESP. Use None for null array and Some for non-null ones. Also refer to array! macro and array_null! macro.

Mixed type, arrays of arrays are allowed.

§Examples

use serde_resp::{array, array_null, bulk, bulk_null, de, err_str, int, ser, simple, RESP};

/// Serialization
// Empty
let obj = array![]; // equivalent to RESP::Array(Some(vec![]))
let serialized = ser::to_string(&obj).unwrap();
assert_eq!("*0\r\n".to_owned(), serialized);

// Regular Example
let obj = array![
    bulk!(b"foo".to_vec()),
    bulk!(b"bar".to_vec()),
]; // equivalent to RESP::Array(Some(vec![bulk!(...), bulk!(...)]))
let serialized = ser::to_string(&obj).unwrap();
assert_eq!("*2\r\n$3\r\nfoo\r\n$3\r\nbar\r\n".to_owned(), serialized);

// Another Regular Example
let obj = array![
    int!(1),
    int!(2),
    int!(3),
];
let serialized = ser::to_string(&obj).unwrap();
assert_eq!("*3\r\n:1\r\n:2\r\n:3\r\n".to_owned(), serialized);

// Mixed Type
let obj = array![
    int!(1),
    int!(2),
    int!(3),
    int!(4),
    bulk!(b"foobar".to_vec()),
];
let serialized = ser::to_string(&obj).unwrap();
assert_eq!(
    "*5\r\n:1\r\n:2\r\n:3\r\n:4\r\n$6\r\nfoobar\r\n".to_owned(),
    serialized
);

// Null Array
let obj = array_null!(); // equivalent to RESP::Array(None)
let serialized = ser::to_string(&obj).unwrap();
assert_eq!("*-1\r\n".to_owned(), serialized);

// Arrays of Arrays
let obj = array![
    array![
        int!(1),
        int!(2),
        int!(3),
    ],
    array![
        simple!("Foo".to_owned()),
        err_str!("Bar".to_owned()),
    ],
];
let serialized = ser::to_string(&obj).unwrap();
assert_eq!(
    "*2\r\n*3\r\n:1\r\n:2\r\n:3\r\n*2\r\n+Foo\r\n-Bar\r\n".to_owned(),
    serialized
);

// Null elements in Arrays
let obj = array![
    bulk!(b"foo".to_vec()),
    bulk_null!(),
    bulk!(b"bar".to_vec()),
];
let serialized = ser::to_string(&obj).unwrap();
assert_eq!(
    "*3\r\n$3\r\nfoo\r\n$-1\r\n$3\r\nbar\r\n".to_owned(),
    serialized
);

/// Deserialization
// Null
let deserialized: RESP = de::from_str("*-1\r\n").unwrap();
assert_eq!(array_null!(), deserialized);

// Mixed Type
let deserialized: RESP = de::from_str("*2\r\n:1\r\n$6\r\nfoobar\r\n").unwrap();
let expected = array![
    int!(1),
    bulk!(b"foobar".to_vec()),
];
assert_eq!(expected, deserialized);

// Arrays of Arrays with Null Bulk String
let deserialized: RESP = de::from_str("*3\r\n*3\r\n:1\r\n:2\r\n:3\r\n$-1\r\n*2\r\n+Foo\r\n-Bar\r\n").unwrap();
let expected = array![
    int!(1),
    bulk!(b"foobar".to_vec()),
];
let expected = array![
    array![
        int!(1),
        int!(2),
        int!(3),
    ],
    bulk_null!(),
    array![
        simple!("Foo".to_owned()),
        err_str!("Bar".to_owned()),
    ],
];
assert_eq!(expected, deserialized);