Crates.io | deseresp |
lib.rs | deseresp |
version | 0.1.9 |
source | src |
created_at | 2022-04-10 13:18:07.213025 |
updated_at | 2022-06-28 09:14:55.313964 |
description | deseresp is an implementation of serializer and deserializer for Redis's RESP3 format using serde framework. |
homepage | |
repository | https://github.com/unrealhoang/deseresp |
max_upload_size | |
id | 565077 |
size | 116,674 |
Deser-RESP is an implementation of serializer and deserializer for Redis's RESP3 format using serde framework.
Simple usage:
let buf = deseresp::to_vec(&(42, "the Answer")).unwrap();
assert_eq!(&buf, b"*2\r\n:42\r\n+the Answer\r\n");
let source: (usize, &str) = deseresp::from_slice(&buf).unwrap();
assert_eq!(source, (42, "the Answer"));
Serialize/Deserialize specific RESP's types with:
BlobError:
use deseresp::types::borrowed::BlobError;
let buf = deseresp::to_vec(&(42, BlobError::from("the Answer"))).unwrap();
assert_eq!(&buf, b"*2\r\n:42\r\n!10\r\nthe Answer\r\n");
let source: (usize, BlobError) = deseresp::from_slice(&buf).unwrap();
assert_eq!(source, (42, BlobError::from("the Answer")));
Push:
use deseresp::types::Push;
let buf = deseresp::to_vec(&Push(("message", "channel", "data"))).unwrap();
assert_eq!(&buf, b">3\r\n+message\r\n+channel\r\n+data\r\n");
let source: Push<(&str, &str, &str)> = deseresp::from_slice(&buf).unwrap();
assert_eq!(source.into_inner(), ("message", "channel", "data"));
Attribute:
use deseresp::types::WithAttribute;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct TtlMeta {
ttl: usize
}
let val_with_attr = WithAttribute::new(TtlMeta { ttl: 3600}, 200);
let buf = deseresp::to_vec(&val_with_attr).unwrap();
assert_eq!(&buf, b"|1\r\n+ttl\r\n:3600\r\n:200\r\n");
let source: WithAttribute<TtlMeta, usize> = deseresp::from_slice(&buf).unwrap();
assert_eq!(source.into_inner(), (TtlMeta { ttl: 3600 }, 200));
Advance usage, zero-copy network parsing:
use bytes::{BytesMut, BufMut, Buf};
use serde::Deserialize;
let mut bytes_mut = BytesMut::new();
// loop
{
// read from network
bytes_mut.put(&b"*2\r\n:42\r\n+the Answer\r\n"[..]);
let mut d = deseresp::Deserializer::from_slice(&bytes_mut);
let r: (usize, &str) = Deserialize::deserialize(&mut d).unwrap();
// do something with r
assert_eq!(r, (42, "the Answer"));
let consumed_bytes = d.get_consumed_bytes();
// advance bytes_mut
bytes_mut.advance(consumed_bytes);
}