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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use futures::compat::Future01CompatExt;
use redis::{aio::Connection as RedisConnection, FromRedisValue, RedisResult, ToRedisArgs};

use crate::Connection;

/// See [redis::Cmd](https://docs.rs/redis/latest/redis/struct.Cmd.html)
pub struct Cmd {
    pub(crate) cmd: redis::Cmd,
}

impl Cmd {
    /// See [redis::Cmd::new](https://docs.rs/redis/latest/redis/struct.Cmd.html#method.new)
    pub fn new() -> Cmd {
        Cmd {
            cmd: redis::Cmd::new(),
        }
    }
    /// See [redis::Cmd::arg](https://docs.rs/redis/latest/redis/struct.Cmd.html#method.arg)
    pub fn arg<T: ToRedisArgs>(&mut self, arg: T) -> &mut Cmd {
        self.cmd.arg(arg);
        self
    }
    /// See [redis::Cmd::cursor_arg](https://docs.rs/redis/latest/redis/struct.Cmd.html#method.cursor_arg)
    pub fn cursor_arg(&mut self, cursor: u64) -> &mut Cmd {
        self.cmd.cursor_arg(cursor);
        self
    }
    /// See [redis::Cmd::get_packed_command](https://docs.rs/redis/latest/redis/struct.Cmd.html#method.get_packed_command)
    pub fn get_packed_command(&self) -> Vec<u8> {
        self.cmd.get_packed_command()
    }
    /// See [redis::Cmd::in_scan_mode](https://docs.rs/redis/latest/redis/struct.Cmd.html#method.in_scan_mode)
    pub fn in_scan_mode(&self) -> bool {
        self.cmd.in_scan_mode()
    }
    /// See [redis::Cmd::query](https://docs.rs/redis/latest/redis/struct.Cmd.html#method.query)
    pub async fn query<T: FromRedisValue + Send>(&self, conn: &mut Connection) -> RedisResult<T> {
        let rconn = conn._take_conn()?;
        let (rconn, result) = self.cmd.query_async(rconn).compat().await?;
        conn._replace_conn(rconn);
        Ok(FromRedisValue::from_redis_value(&result)?)
    }
    /// See [redis::Cmd::execute](https://docs.rs/redis/latest/redis/struct.Cmd.html#method.execute)
    pub async fn execute(&self, conn: &mut Connection) -> RedisResult<()> {
        let rconn = conn._take_conn()?;
        let (rconn, _) = self
            .cmd
            .query_async::<RedisConnection, ()>(rconn)
            .compat()
            .await?;
        conn._replace_conn(rconn);
        Ok(())
    }
}

impl From<redis::Cmd> for Cmd {
    fn from(cmd: redis::Cmd) -> Self {
        Cmd { cmd }
    }
}

impl Into<redis::Cmd> for Cmd {
    fn into(self) -> redis::Cmd {
        self.cmd
    }
}

/// See [redis::cmd](https://docs.rs/redis/0.13.0/redis/fn.cmd.html)
pub fn cmd(name: &str) -> Cmd {
    let mut cmd = Cmd::new();
    cmd.arg(name);
    cmd
}