memcached_protocal/command/
command.rs

1use std::io::BufRead;
2use super::deletion_command::DeletionCommand;
3use super::retrieval_command::RetrievalCommand;
4use super::store_command::StoreCommand;
5
6use ::error::Result;
7use ::error::ErrorKind;
8use ::byte_utils::peek_until;
9
10
11#[derive(Debug, PartialEq)]
12pub enum Command {
13    Delete(DeletionCommand),
14    Retrieval(RetrievalCommand),
15    Store(StoreCommand),
16}
17
18
19pub fn parse<R: BufRead>(reader: &mut R) -> Result<Command> {
20    use self::Command::*;
21
22    let buf = try!(peek_until(reader, "\r\n"));
23    let len = buf.len();
24    let cmd_str = try!(String::from_utf8(buf));
25    let segments = cmd_str.split_whitespace().collect::<Vec<&str>>();
26    let length = segments.len();
27    if length < 1 {
28        reader.consume(len);
29        return Err(ErrorKind::ClientError("wrong size of params".to_owned()).into());
30    }
31    let cmd = segments[0];
32    Ok(match cmd {
33        "set" | "add" | "replace" | "append" | "prepend" | "cas" => Store(try!(StoreCommand::parse(reader))),
34        "get" | "gets" => Retrieval(try!(RetrievalCommand::parse(reader))),
35        "delete" => Delete(try!(DeletionCommand::parse(reader))),
36        _ => {
37            reader.consume(len);
38            return Err(ErrorKind::ClientError("not supported command".to_owned()).into())
39        },
40    })
41}