RESP(REdis Serialization Protocol) Serialization for Rust.
Implementations:
Rust
extern crate resp;
use resp::{Value, encode, encode_slice, Decoder};
Rust
enum Value {
/// Null bulk reply, $-1\r\n
Null,
/// Null array reply, *-1\r\n
NullArray,
/// For Simple Strings the first byte of the reply is "+"
String(String),
/// For Errors the first byte of the reply is "-"
Error(String),
/// For Integers the first byte of the reply is ":"
Integer(i64),
/// For Bulk Strings the first byte of the reply is "$"
Bulk(String),
/// For Bulk <binary> Strings the first byte of the reply is "$"
BufBulk(Vec<u8>),
/// For Arrays the first byte of the reply is "*"
Array(Vec<Value>),
}
RESP values.
Rust
let error = Value::Error("error!".to_string());
let null = Value::Null;
fn is_null(&self) -> bool
Rust
println!("{:?}", Value::Null.is_null()); // true
println!("{:?}", Value::NullArray.is_null()); // true
println!("{:?}", Value::Integer(123).is_null()); // false
fn is_error(&self) -> bool
Rust
println!("{:?}", Value::Null.is_error()); // false
println!("{:?}", Value::NullArray.is_error()); // false
println!("{:?}", Value::Error("".to_string()).is_error()); // true
fn encode(&self) -> Vec<u8>
Rust
let val = Value::String("OK正".to_string());
println!("{:?}", val.encode()); // [43, 79, 75, 230, 173, 163, 13, 10]
fn to_encoded_string(&self) -> io::Result<String>
Rust
let val = Value::String("OK正".to_string());
println!("{:?}", val.to_encoded_string().unwrap()); // "+OK正\r\n"
fn to_beautify_string(&self) -> String
A test result:
1) (Null)
2) (Null Array)
3) OK
4) (Error) Err
5) (Integer) 123
6) \"Bulk String\"
7) (Empty Array)
8) (Buffer) 00 64
9) 1) (Empty Array)
2) (Integer) 123
3) \"Bulk String\"
10) 1) (Null)
2) (Null Array)
3) OK
4) (Error) Err
5) (Integer) 123
6) \"Bulk String\"
7) (Empty Array)
8) (Buffer) 00 64
9) 1) (Empty Array)
2) (Integer) 123
3) \"Bulk String\"
11) (Null)
12) 1) (Null)
2) (Null Array)
3) OK
4) (Error) Err
5) (Integer) 123
6) \"Bulk String\"
7) (Empty Array)
8) (Buffer) 00 64
9) 1) (Empty Array)
2) (Integer) 123
3) \"Bulk String\"
10) 1) (Null)
2) (Null Array)
3) OK
4) (Error) Err
5) (Integer) 123
6) \"Bulk String\"
7) (Empty Array)
8) (Buffer) 00 64
9) 1) (Empty Array)
2) (Integer) 123
3) \"Bulk String\"
11) (Null)
13) (Null)
Encode a RESP value to buffer.
fn encode(value: &Value) -> Vec<u8>
Rust
let val = Value::String("OK正".to_string());
println!("{:?}", encode(&val)); // [43, 79, 75, 230, 173, 163, 13, 10]
Encode a slice of string to RESP request buffer. It is usefull for redis client to encode request command.
fn encode_slice(array: &[&str]) -> Vec<u8>
Rust
let array = ["SET", "a", "1"];
println!("{:?}", String::from_utf8(encode_slice(&array)));
// Ok("*3\r\n$3\r\nSET\r\n$1\r\na\r\n$1\r\n1\r\n")
Rust
struct Decoder {
// some fields omitted
}
Decode redis reply buffers.
```Rust let mut decoder = Decoder::new(); let buf = Value::NullArray.encode();
println!("{:?}", decoder.feed(&buf)); // Ok(()) println!("{:?}", decoder.read()); // Some(Value::NullArray) ```
fn new() -> Self
Rust
let mut decoder = Decoder::new();
fn with_buf_bulk() -> Self
Rust
let mut decoder = Decoder::with_buf_bulk();
fn feed(&mut self, buf: &[u8]) -> Result<(), io:Error>
Rust
println!("{:?}", decoder.feed(&buf)); // Ok(())
fn read(&mut self) -> Option<Value>
Rust
println!("{:?}", decoder.read()); // Some(Value::NullArray)
println!("{:?}", decoder.read()); // None
fn buffer_len(&self) -> usize
Rust
println!("{:?}", decoder.buffer_len()); // 0
fn result_len(&self) -> usize
Rust
println!("{:?}", decoder.result_len()); // 0