Structs and functions for implementing the Redis protocol.
With cargo edit.
cargo add redis-protocol
This library relies heavily on the Bytes
interface to implement parsing logic such that buffer contents never need to move or be copied.
```rust use redis_protocol::resp2::prelude::*; use bytes::{Bytes, BytesMut};
fn main() { let frame = Frame::BulkString("foobar".into()); let mut buf = BytesMut::new();
let len = match encode_bytes(&mut buf, &frame) { Ok(l) => l, Err(e) => panic!("Error encoding frame: {:?}", e) }; println!("Encoded {} bytes into buffer with contents {:?}", len, buf);
let buf: Bytes = "*3\r\n$3\r\nFoo\r\n$-1\r\n$3\r\nBar\r\n".into(); let (frame, consumed) = match decode(&buf) { Ok(Some((f, c))) => (f, c), Ok(None) => panic!("Incomplete frame."), Err(e) => panic!("Error parsing bytes: {:?}", e) }; println!("Parsed frame {:?} and consumed {} bytes", frame, consumed);
let key = "foobarbaz"; println!("Hash slot for {}: {}", key, redis_keyslot(key)); } ```
Use the decode-logs
feature to enable special TRACE logs during the Frame decoding process.
BytesMut
Using the default decoder interface with BytesMut
can be challenging if the goal is to avoid copying or moving the buffer contents.
To better support this use case (such as the codec interface) this library supports a decode-mut
feature flag that can parse BytesMut
without copying or moving the buffer contents.
Enable the index-map
feature to use IndexMap instead of HashMap
and HashSet
. This is useful for testing and may also be useful to callers.