1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
use std::io::BufRead;

use ::error::ErrorKind;
use ::error::Result;
use ::byte_utils::read_until;

#[derive(Debug, PartialEq)]
pub struct RetrievalCommand {
    pub command_name: String,
    pub keys: Vec<String>,
}


impl RetrievalCommand {
    pub fn parse<R: BufRead>(reader: &mut R) -> Result<RetrievalCommand> {
        let cmd_line = try!(read_until(reader, "\r\n"));
        let cmd_str = try!(String::from_utf8(cmd_line));
        let segments = cmd_str.split_whitespace().collect::<Vec<&str>>();
        let length = segments.len();
        if length < 2 {
            return Err(ErrorKind::ClientError("wrong size of params".to_owned()).into());
        }
        let cmd = segments[0];
        let keys = segments[1..].iter().map(|&s| s.to_owned()).collect();

        Ok(RetrievalCommand {
            command_name: cmd.to_owned(),
            keys: keys,
        })
    }
}