Crates.io | resp-protocol |
lib.rs | resp-protocol |
version | 0.0.10 |
source | src |
created_at | 2021-12-28 19:14:51.066497 |
updated_at | 2022-01-02 10:51:32.518951 |
description | REdis Serialization Protocol |
homepage | https://github.com/WatchDG/rust-resp-protocol |
repository | https://github.com/WatchDG/rust-resp-protocol |
max_upload_size | |
id | 504360 |
size | 39,113 |
REdis Serialization Protocol
add resp-protocol
to Cargo.toml
[dependencies]
resp-protocol = "0.0.10"
use resp_protocol;
"+OK\r\n"
use resp_protocol::SimpleString;
let simple_string: SimpleString = SimpleString::new(b"OK");
use resp_protocol::SimpleString;
let string: &str = "+OK\r\n";
let simple_string: SimpleString = SimpleString::parse(string.as_bytes(), &mut 0, &string.len()).unwrap();
"-ERROR\r\n"
use resp_protocol::Error;
let error: Error = Error::new(b"ERROR");
use resp_protocol::Error;
let string: &str = "-ERROR\r\n";
let error: Error = Error::parse(string.as_bytes(), &mut 0, &string.len()).unwrap();
":100\r\n"
use resp_protocol::Integer;
let integer: Integer = Integer::new(-100i64);
use resp_protocol::Integer;
let string: &str = ":-100\r\n";
let integer: Integer = Integer::parse(string.as_bytes(), &mut 0, &string.len()).unwrap();
"$6\r\nfoobar\r\n"
use resp_protocol::BulkString;
let bulk_string: BulkString = BulkString::new(b"foobar");
use resp_protocol::BulkString;
let string: &str = "$6\r\nfoobar\r\n";
let bulk_string: BulkString = BulkString::parse(string.as_bytes(), &mut 0, &string.len()).unwrap();
"*0\r\n" // empty array
"*2\r\n$3\r\nfoo\r\n$3\r\nbar\r\n" // bulk strings array
"*2\r\n:1\r\n$6\r\nfoobar\r\n" // mixed types array
use resp_procotol::{Array, ArrayBuilder, RespType, Integer, BulkString};
let mut array_builder: ArrayBuilder = ArrayBuilder::new();
array_builder.insert(RespType::Integer(Integer::new(100)));
array_builder.insert(RespType::BulkString(BulkString::new(b"foobar")));
let array: Array = array_builder.build();
println!("{:?}", array); // Array(b"*2\r\n:100\r\n$6\r\nfoobar\r\n")
use resp_protocol::Array;
let string = "*2\r\n$3\r\nfoo\r\n$3\r\nbar\r\n";
let array = Array::parse(string.as_bytes(), &mut 0, &string.len()).unwrap();
println!("{:?}", array); // Array(b"*2\r\n$3\r\nfoo\r\n$3\r\nbar\r\n")