disque_cli/
disque.rs

1use std::io::{Result, Error, ErrorKind};
2use std::net::{ToSocketAddrs};
3
4use super::{Value, encode_slice};
5use super::connection::{Connection};
6
7pub fn create_client(hostname: &str, port: u16, password: &str) -> Result<Client> {
8    let mut client = try!(Client::new((hostname, port)));
9    try!(client.init(password));
10    Ok(client)
11}
12
13pub struct Client {
14    conn: Connection,
15}
16
17impl Client {
18    pub fn new<A: ToSocketAddrs>(addrs: A) -> Result<Self> {
19        Ok(Client {
20            conn: try!(Connection::new(addrs)),
21        })
22    }
23
24    pub fn cmd(&mut self, slice: &[&str]) -> Result<Value> {
25        let buf = encode_slice(slice);
26        self.conn.write(&buf).unwrap();
27        self.conn.read()
28    }
29
30    pub fn read_more(&mut self) -> Result<Value> {
31        self.conn.read()
32    }
33
34    fn init(&mut self, password: &str) -> Result<()> {
35        if password.len() > 0 {
36            if let Value::Error(err)  = try!(self.cmd(&["auth", password])) {
37                return Err(Error::new(ErrorKind::PermissionDenied, err));
38            }
39
40        }
41        Ok(())
42    }
43}