/// It will read buffers from the inner BufReader, decode it to a Value.
It will read buffers from the inner BufReader, decode it to a Value.
It will read buffers from the inner BufReader decode it to a Value .
pub fn decode(&mut self) -> Result<Value> {
        let mut res: Vec<u8> = Vec::with_capacity(1024);
        self.reader.read_until(b'\n', &mut res)?;
        let len = res.len();
        if len < 3 {
            return Err(Error::new(ErrorKind::InvalidInput, format!("too short: {}", len)));
        }
        if !is_crlf(res[len - 2], res[len - 1]) {
            return Err(Error::new(ErrorKind::InvalidInput, format!("invalid CRLF: {:?}", res)));
        }
        let bytes = res[1..len - 2].as_ref();
        match res[0] {
            // Value::String
            b'+' => parse_string(bytes).map(Value::String),
            // Value::Error
            b'-' => parse_string(bytes).map(Value::Error),
            // Value::Integer
            b':' => parse_integer(bytes).map(Value::Integer),
            // Value::Bulk
            b'$' => {
                let int = parse_integer(bytes)?;
                if int == -1 {
                    // Null bulk
                    return Ok(Value::Null);
                }
                if int < -1 || int >= RESP_MAX_SIZE {
                    return Err(Error::new(ErrorKind::InvalidInput,
                                          format!("invalid bulk length: {}", int)));
                }
                let int = int as usize;
//                let mut buf: Vec<u8> = Vec::new();
//                buf.resize(int+2, 0);
                // set_len is faster than vec.resize()
                let mut buf: Vec<u8> = Vec::with_capacity(int+2);
                unsafe { buf.set_len(int+2); }
                self.reader.read_exact(buf.as_mut_slice())?;
                if !is_crlf(buf[int], buf[int + 1]) {
                    return Err(Error::new(ErrorKind::InvalidInput,
                                          format!("invalid CRLF: {:?}", buf)));
                }
//                buf.truncate(int);
                unsafe { buf.set_len(int);}
                if self.buf_bulk {
                    return Ok(Value::BufBulk(buf));
                }
                parse_string(buf.as_slice()).map(Value::Bulk)
            }
            // Value::Array
            b'*' => {
                let int = parse_integer(bytes)?;
                if int == -1 {
                    // Null array
                    return Ok(Value::NullArray);
                }
                if int < -1 || int >= RESP_MAX_SIZE {
                    return Err(Error::new(ErrorKind::InvalidInput,
                                          format!("invalid array length: {}", int)));
                }
                let mut array: Vec<Value> = Vec::with_capacity(int as usize);
                for _ in 0..int {
                    let val = self.decode()?;
                    array.push(val);
                }
                Ok(Value::Array(array))
            }
            prefix => {
                Err(Error::new(ErrorKind::InvalidInput,
                               format!("invalid RESP type: {:?}", prefix)))
            }
        }
    }
pub fn decode ( & mut self ) -> Result < Value > { let mut res : Vec < u8 > = Vec :: with_capacity ( 1024 ) ; self . reader . read_until ( b'\n' , & mut res ) ? ; let len = res . len ( ) ; if len < 3 { return Err ( Error :: new ( ErrorKind :: InvalidInput , format ! ( " " len ) ) ) ; } if ! is_crlf ( res [ len - 2 ] , res [ len - 1 ] ) { return Err ( Error :: new ( ErrorKind :: InvalidInput , format ! ( " " res ) ) ) ; } let bytes = res [ 1 .. len - 2 ] . as_ref ( ) ; match res [ 0 ] { // Value::String b'+' => parse_string ( bytes ) . map ( Value :: String ) , // Value::Error b'-' => parse_string ( bytes ) . map ( Value :: Error ) , // Value::Integer b':' => parse_integer ( bytes ) . map ( Value :: Integer ) , // Value::Bulk b'$' => { let int = parse_integer ( bytes ) ? ; if int == - 1 { // Null bulk return Ok ( Value :: Null ) ; } if int < - 1 || int >= RESP_MAX_SIZE { return Err ( Error :: new ( ErrorKind :: InvalidInput , format ! ( " " int ) ) ) ; } let int = int as usize ; //                let mut buf: Vec<u8> = Vec::new(); //                buf.resize(int+2, 0); // set_len is faster than vec.resize() let mut buf : Vec < u8 > = Vec :: with_capacity ( int + 2 ) ; unsafe { buf . set_len ( int + 2 ) ; } self . reader . read_exact ( buf . as_mut_slice ( ) ) ? ; if ! is_crlf ( buf [ int ] , buf [ int + 1 ] ) { return Err ( Error :: new ( ErrorKind :: InvalidInput , format ! ( " " buf ) ) ) ; } //                buf.truncate(int); unsafe { buf . set_len ( int ) ; } if self . buf_bulk { return Ok ( Value :: BufBulk ( buf ) ) ; } parse_string ( buf . as_slice ( ) ) . map ( Value :: Bulk ) } // Value::Array b'*' => { let int = parse_integer ( bytes ) ? ; if int == - 1 { // Null array return Ok ( Value :: NullArray ) ; } if int < - 1 || int >= RESP_MAX_SIZE { return Err ( Error :: new ( ErrorKind :: InvalidInput , format ! ( " " int ) ) ) ; } let mut array : Vec < Value > = Vec :: with_capacity ( int as usize ) ; for _ in 0 .. int { let val = self . decode ( ) ? ; array . push ( val ) ; } Ok ( Value :: Array ( array ) ) } prefix => { Err ( Error :: new ( ErrorKind :: InvalidInput , format ! ( " " prefix ) ) ) } } }
It will read buffers from the inner BufReader, decode it to a Value.
It will read buffers from the inner BufReader decode it to a Value .
&mut self