resp 0.2.2

RESP(REdis Serialization Protocol) Serialization for Rust.
Documentation

RESP

RESP(REdis Serialization Protocol) Serialization for Rust .

Crates version Build Status Coverage Status Crates downloads

API

extern crate resp;
use resp::{Value, encode, encode_slice, Decoder};

Value

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.

Examples

let err = Value::Error("error!".to_string());
let nul = Value::Null;

impl Value

fn is_null(&self) -> bool
println!("{:?}", Value::Null.is_null())  // true
println!("{:?}", Value::NullArray.is_null())  // true
println!("{:?}", Value::Integer(123).is_null())  // false
fn is_error(&self) -> bool
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>
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>
let val = Value::String("OK正".to_string());
println!("{:?}", val.to_encoded_string().unwrap())  // "+OK正\r\n"

encode

Encode a RESP value to buffer.

fn encode(value: &Value) -> Vec<u8>
let val = Value::String("OK正".to_string());
println!("{:?}", encode(&val))  // [43, 79, 75, 230, 173, 163, 13, 10]

encode_slice

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>
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")

Decoder

struct Decoder {
    // some fields omitted
}

Decode redis reply buffers.

Examples

let mut decoder = Decoder::new();
let buf = Value::NullArray.encode();

println!("{:?}", decoder.feed(&buf))  // Ok(())
println!("{:?}", decoder.read())  // Some(Value::NullArray)

impl Decoder

fn new() -> Self
let mut decoder = Decoder::new();
fn with_buf_bulk() -> Self
let mut decoder = Decoder::with_buf_bulk();
fn feed(&mut self, buf: &[u8]) -> Result<(), io:Error>
println!("{:?}", decoder.feed(&buf))  // Ok(())
fn read(&mut self) -> Option<Value>
println!("{:?}", decoder.read())  // Some(Value::NullArray)
println!("{:?}", decoder.read())  // None
fn buffer_len(&self) -> usize
println!("{:?}", decoder.buffer_len())  // 0
fn result_len(&self) -> usize
println!("{:?}", decoder.result_len())  // 0