Crates.io | serde-resp |
lib.rs | serde-resp |
version | 0.3.1 |
source | src |
created_at | 2021-02-15 07:12:14.151633 |
updated_at | 2021-02-16 05:30:45.115717 |
description | Redis RESP protocol serialization and deserialization with serde |
homepage | |
repository | https://github.com/DEDZTBH/serde-RESP |
max_upload_size | |
id | 355359 |
size | 42,099 |
Redis RESP protocol serialization and deserialization with serde. RESP Specification
To use the crate, add this to your Cargo.toml
:
[dependencies]
serde-resp = "0.3.1"
IMPORTANT: Do NOT (de)serialize with any other types besides RESP/RESPType
! You may get panic or incorrect results!
Why?
Here are the Redis RESP types and their corresponding Rust types for (de)serialize operations.
Simple String
RESP::SimpleString(String)
Error
RESP::Error(String)
Integer
RESP::Integer(i64)
Bulk String
RESP::BulkString(Option<Vec<u8>>)
None
for null bulk strings and Some
for non-null ones.Array
RESP::Array(Option<Vec<RESP>>)
None
for null arrays and Some
for non-null ones.To serialize, use ser::to_string or ser::to_writer.
To deserialize, use de::from_str or de::from_reader or de::from_buf_reader.
For usage examples, refer to https://docs.rs/serde_resp/0.3.1/serde_resp/enum.RESPType.html
Since 0.3.0, you can start using very handy macros! Here is a demo:
use serde_resp::{array, array_null, bulk, bulk_null, de, err_str, int, ser, simple, RESP};
let resp_array = array![
simple!("simple string".to_owned()),
err_str!("error string".to_owned()),
int!(42),
bulk!(b"bulk string".to_vec()),
bulk_null!(),
array![
simple!("arrays of arrays!".to_owned()),
array![simple!("OK ENOUGH!".to_owned())],
],
array_null!(),
];
let serialized = ser::to_string(&resp_array).unwrap();
assert_eq!(
"*7\r\n+simple string\r\n-error string\r\n:42\r\n$11\r\nbulk string\r\n$-1\r\n*2\r\n+arrays of arrays!\r\n*1\r\n+OK ENOUGH!\r\n*-1\r\n",
serialized
);
let deserialized = de::from_str(&serialized).unwrap();
assert_eq!(resp_array, deserialized);
https://docs.rs/serde_resp/0.3.1/serde_resp
The RESP protocol does not fit the serde data model well.
struct
, enum
, or tuple
does not make sense.String
map to? Simple or error string? Using what serde offers here will introduce a lot of ambiguity.
Another problem is that serde assumes there is only one string type so I need to apply some "hacks" to make (de)serializer work correctly.
These "hacks" involve (de)serialize methods that users are not supposed to use.A bonus reason is this makes it easier to define array, which can be recursive.
I know this is very strict and tedious to write. But less ambiguity is always a good thing. Plus now with macros, it should be more pleasant to use now.