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
74
75
76
77
78
79
80
81
82
use crate::resp::{CommandArgs, ToArgs};

#[cfg(debug_assertions)]
use std::sync::atomic::{AtomicUsize, Ordering};

#[cfg(debug_assertions)]
static COMMAND_SEQUENCE_COUNTER: AtomicUsize = AtomicUsize::new(0);

/// Shortcut function for creating a command.
#[must_use]
#[inline(always)]
pub fn cmd(name: &'static str) -> Command {
    Command::new(name)
}

/// Generic command meant to be sent to the Redis Server
#[derive(Debug, Clone)]
pub struct Command {
    /// Name of the command.
    ///
    /// Note: Sub commands are expressed as the first argument of the command.
    ///
    /// e.g. `cmd("CONFIG").arg("SET").arg("hash-max-listpack-entries").arg("1024")`
    pub name: &'static str,
    /// Collection of arguments of the command.
    pub args: CommandArgs,
    #[doc(hidden)]
    #[cfg(debug_assertions)]
    pub kill_connection_on_write: usize,
    #[cfg(debug_assertions)]
    #[allow(unused)]
    pub (crate) command_seq: usize,
}

impl Command {
    /// Creates an new command.
    ///
    /// [`cmd`](crate::resp::cmd) function can be used as a shortcut.
    #[must_use]
    #[inline(always)]
    pub fn new(name: &'static str) -> Self {
        Self {
            name,
            args: CommandArgs::default(),
            #[cfg(debug_assertions)]
            kill_connection_on_write: 0,
            #[cfg(debug_assertions)]
            command_seq: COMMAND_SEQUENCE_COUNTER.fetch_add(1, Ordering::SeqCst),
        }
    }

    /// Builder function to add an argument to an existing command.
    #[must_use]
    #[inline(always)]
    pub fn arg<A>(mut self, arg: A) -> Self
    where
        A: ToArgs,
    {
        arg.write_args(&mut self.args);
        self
    }

    /// Builder function to add an argument to an existing command, only if a condition is `true`.
    #[must_use]
    #[inline(always)]
    pub fn arg_if<A>(mut self, condition: bool, arg: A) -> Self
    where
        A: ToArgs,
    {
        if condition {
            arg.write_args(&mut self.args); 
        }
        self
    }

    #[cfg(debug_assertions)]
    #[inline]
    pub fn kill_connection_on_write(mut self, num_kills: usize) -> Self {
        self.kill_connection_on_write = num_kills;
        self
    }
}