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
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
use crate::connection::builder::MSetBuilder;
use std::io::Write;

///Struct for defining commands manually, which allows for pipelining of several commands. If you need
///to only run one command, use [`Command`](struct.Command.html), which has almost the same API.
///# Example
/// ```
///use darkredis::{CommandList, Connection};
///use futures::TryStreamExt; //for `try_collect`
///# use darkredis::*;
///# #[cfg_attr(feature = "runtime_tokio", tokio::main)]
///# #[cfg_attr(feature = "runtime_async_std", async_std::main)]
///# async fn main() {
///# let mut connection = Connection::connect("127.0.0.1:6379").await.unwrap();
///# connection.del("pipelined-list").await.unwrap();
///
/// let command = CommandList::new("LPUSH").arg(b"pipelined-list").arg(b"bar")
///     .command("LTRIM").arg(b"pipelined-list").arg(b"0").arg(b"100");
/// let results = connection.run_commands(command).await.unwrap();
///
/// assert_eq!(results.try_collect::<Vec<Value>>().await.unwrap(), vec![Value::Integer(1), Value::Ok]);
///# connection.del("pipelined-list").await.unwrap();
///# }
/// ```
#[derive(Clone, Debug)]
pub struct CommandList<'a> {
    commands: Vec<Command<'a>>,
}

impl<'a> CommandList<'a> {
    ///Create a new command list starting with `cmd`.
    pub fn new(cmd: &'a str) -> Self {
        let commands = vec![Command::new(cmd)];
        Self { commands }
    }

    ///Consumes the command and appends an argument to it, builder style. Note that this will NOT create a new
    ///command for pipelining. That's what [`Commandlist::command`](struct.CommandList.html#method.command) is for.
    ///# See also
    ///[`append_arg`](struct.CommandList.html#method.append_arg)
    pub fn arg<D>(mut self, data: &'a D) -> Self
    where
        D: AsRef<[u8]>,
    {
        self.append_arg(data);
        self
    }

    ///Add multiple arguments from a slice, builder-style.
    ///# See also
    ///[`append_args`](struct.CommandList.html#method.append_args)
    pub fn args<D>(mut self, arguments: &'a [D]) -> Self
    where
        D: AsRef<[u8]>,
    {
        self.append_args(arguments);
        self
    }

    ///Add a command to be executed in a pipeline, builder-style. Calls to `Command::arg` will add arguments from
    ///now on.
    ///# See also
    ///[`append_command`](struct.CommandList.html#method.append_command)
    pub fn command(mut self, cmd: &'a str) -> Self {
        self.commands.push(Command::new(cmd));
        self
    }

    ///Append arguments from a slice.
    ///# See also
    ///[`args`](struct.CommandList.html#method.args)
    pub fn append_args<D>(&mut self, arguments: &'a [D])
    where
        D: AsRef<[u8]>,
    {
        let last_command = self.commands.last_mut().unwrap();
        for arg in arguments {
            last_command.args.push(arg.as_ref());
        }
    }

    ///Mutate `self` by adding an additional argument.
    ///# See also
    ///[`arg`](struct.CommandList.html#method.arg)
    pub fn append_arg<D>(&mut self, data: &'a D)
    where
        D: AsRef<[u8]>,
    {
        self.commands.last_mut().unwrap().args.push(data.as_ref());
    }

    ///Append a new command to `self`.
    ///# See also
    ///[`command`](struct.CommandList.html#method.command)
    pub fn append_command(&mut self, cmd: &'a str) {
        self.commands.push(Command::new(cmd))
    }

    ///Count the number of commands currently in the pipeline
    pub fn command_count(&self) -> usize {
        self.commands.len()
    }

    //Convert to redis protocol encoding
    pub(crate) fn serialize(self, buffer: &mut Vec<u8>) {
        for command in self.commands {
            command.serialize(buffer);
        }
    }
}

#[cfg(feature = "bench")]
impl<'a> CommandList<'a> {
    ///Workaround for benchmarking
    #[inline(always)]
    pub fn serialize_bench(self) -> Vec<u8> {
        self.serialize()
    }
}

///Struct for defining commands manually. If you want to run multiple commands in a pipeline, use [`CommandList`](struct.CommandList.html).
///# Example
/// ```
///use darkredis::{Command, Connection};
///# use darkredis::*;
///# #[cfg_attr(feature = "runtime_tokio", tokio::main)]
///# #[cfg_attr(feature = "runtime_async_std", async_std::main)]
///# async fn main() {
///# let mut connection = Connection::connect("127.0.0.1:6379").await.unwrap();
///# connection.del("singular-key").await.unwrap();
///
///let command = Command::new("SET").arg(b"singular-key").arg(b"some-value");
///let result = connection.run_command(command).await.unwrap();
///
///assert_eq!(result, Value::Ok);
///# connection.del("singular-key").await.unwrap();
///# }
/// ```
#[derive(Clone, Debug)]
pub struct Command<'a> {
    command: &'a str,
    args: Vec<&'a [u8]>,
}

impl<'a> Command<'a> {
    ///Create a new Command.
    pub fn new(cmd: &'a str) -> Self {
        Self {
            command: cmd,
            args: Vec::new(),
        }
    }

    ///Append an argument to this command, builder-style.
    ///# See also
    ///[`append_arg`](struct.Command.html#method.append_arg)
    pub fn arg<D>(mut self, data: &'a D) -> Self
    where
        D: AsRef<[u8]>,
    {
        self.append_arg(data);
        self
    }

    ///Add multiple arguments to a command in slice form.
    ///# See also
    ///[`append_args`](struct.Command.html#method.append_args)
    pub fn args<D>(mut self, arguments: &'a [D]) -> Self
    where
        D: AsRef<[u8]>,
    {
        self.append_args(arguments);

        self
    }

    ///Append an argument to `self`.
    ///# See also
    ///[`arg`](struct.Command.html#method.append_arg)
    pub fn append_arg<D>(&mut self, data: &'a D)
    where
        D: AsRef<[u8]>,
    {
        self.args.push(data.as_ref());
    }

    ///Append multiple arguments to `self`.
    ///# See also
    ///[`args`](struct.Command.html#method.args)
    pub fn append_args<D>(&mut self, arguments: &'a [D])
    where
        D: AsRef<[u8]>,
    {
        for arg in arguments {
            self.args.push(arg.as_ref());
        }
    }

    pub(crate) fn serialize(self, buffer: &mut Vec<u8>) {
        //Write array and command header
        write!(
            buffer,
            "*{}\r\n${}\r\n{}\r\n",
            self.args.len() + 1,
            self.command.len(),
            self.command
        )
        .unwrap();

        for arg in self.args {
            //Serialize as byte string
            write!(buffer, "${}\r\n", arg.len()).unwrap();
            for byte in arg {
                buffer.push(*byte);
            }
            buffer.push(b'\r');
            buffer.push(b'\n');
        }
    }

    pub(crate) fn append_msetbuilder(&mut self, builder: &'a MSetBuilder<'a>) {
        for item in builder.build() {
            self.args.push(*item);
        }
    }
}

#[cfg(any(feature = "bench", test))]
impl<'a> Command<'a> {
    #[inline(always)]
    ///Wrapper/Workaround for benchmarking and testing `serialize`
    pub fn serialize_bench(self) -> Vec<u8> {
        let mut out = Vec::new();
        self.serialize(&mut out);
        out
    }
}

#[cfg(test)]
mod test {
    use super::*;
    #[test]
    fn serialize_singular() {
        //The conversion makes it easier to understand failures
        let command = Command::new("GET").arg(b"some-key").serialize_bench();
        assert_eq!(
            String::from_utf8_lossy(&command),
            "*2\r\n$3\r\nGET\r\n$8\r\nsome-key\r\n"
        )
    }

    #[test]
    fn serialize_multiple() {
        let mut buf = Vec::new();
        CommandList::new("GET")
            .arg(b"some-key")
            .command("LLEN")
            .arg(b"some-other-key")
            .serialize(&mut buf);
        assert_eq!(
            String::from_utf8_lossy(&buf),
            "*2\r\n$3\r\nGET\r\n$8\r\nsome-key\r\n*2\r\n$4\r\nLLEN\r\n$14\r\nsome-other-key\r\n"
        );
    }

    #[test]
    fn multiple_args() {
        let arguments = vec!["a", "b", "c"];
        let command = Command::new("LPUSH")
            .arg(b"some-key")
            .args(&arguments)
            .serialize_bench();
        assert_eq!(
            String::from_utf8_lossy(&command),
            "*5\r\n$5\r\nLPUSH\r\n$8\r\nsome-key\r\n$1\r\na\r\n$1\r\nb\r\n$1\r\nc\r\n"
        );
    }
}