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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
use crate::{cmd, resp::BulkString, Command, CommandSend, Result, SingleArgOrCollection};
use futures::Future;
use std::pin::Pin;

/// A group of generic Redis commands
///
/// # See Also
/// [Redis Generic Commands](https://redis.io/commands/?group=generic)
pub trait GenericCommands: CommandSend {
    /// This command copies the value stored at the source key to the destination key.
    ///
    /// # See Also
    /// [https://redis.io/commands/copy/](https://redis.io/commands/copy/)
    fn copy<S, D>(&self, source: S, destination: D) -> Copy<Self>
    where
        S: Into<BulkString>,
        D: Into<BulkString>,
    {
        Copy {
            generic_commands: &self,
            cmd: cmd("COPY").arg(source).arg(destination),
        }
    }

    /// Removes the specified keys. A key is ignored if it does not exist.
    ///
    /// # Return
    /// The number of keys that were removed.
    ///
    /// # See Also
    /// [https://redis.io/commands/del/](https://redis.io/commands/del/)
    fn del<K, C>(&self, keys: C) -> Pin<Box<dyn Future<Output = Result<usize>> + '_>>
    where
        K: Into<BulkString>,
        C: SingleArgOrCollection<K>,
    {
        self.send_into(cmd("DEL").arg(keys))
    }

    /// Returns if keys exist.
    ///
    /// # Return
    /// The number of keys that exist from those specified as arguments.
    ///
    /// # See Also
    /// [https://redis.io/commands/exists/](https://redis.io/commands/exists/)
    fn exists<K, C>(&self, keys: C) -> Pin<Box<dyn Future<Output = Result<usize>> + '_>>
    where
        K: Into<BulkString>,
        C: SingleArgOrCollection<K>,
    {
        self.send_into(cmd("EXISTS").arg(keys))
    }

    /// Set a timeout on key in seconds
    ///
    /// # Return
    /// * `true` - if the timeout was set.
    /// * `false` - if the timeout was not set. e.g. key doesn't exist, or operation skipped due to the provided arguments.
    ///
    /// # See Also
    /// [https://redis.io/commands/expire/](https://redis.io/commands/expire/)
    fn expire<K>(&self, key: K, seconds: u64) -> Expire<Self>
    where
        K: Into<BulkString>,
    {
        Expire {
            generic_commands: &self,
            cmd: cmd("EXPIRE").arg(key).arg(seconds),
        }
    }

    /// EXPIREAT has the same effect and semantic as EXPIRE,
    /// but instead of specifying the number of seconds representing the TTL (time to live),
    /// it takes an absolute Unix timestamp (seconds since January 1, 1970)
    ///
    /// A timestamp in the past will delete the key
    ///
    /// # Return
    /// * `true` - if the timeout was set.
    /// * `false` - if the timeout was not set. e.g. key doesn't exist, or operation skipped due to the provided arguments.
    ///
    /// # See Also
    /// [https://redis.io/commands/expireat/](https://redis.io/commands/expireat/)
    fn expireat<K>(&self, key: K, unix_time_seconds: u64) -> Expire<Self>
    where
        K: Into<BulkString>,
    {
        Expire {
            generic_commands: &self,
            cmd: cmd("EXPIREAT").arg(key).arg(unix_time_seconds),
        }
    }

    /// Returns the absolute Unix timestamp (since January 1, 1970) in seconds at which the given key will expire.
    ///
    /// # Return
    /// Expiration Unix timestamp in seconds, or a negative value in order to signal an error (see the description below).
    /// - The command returns -1 if the key exists but has no associated expiration time.
    /// - The command returns -2 if the key does not exist.
    ///
    /// # See Also
    /// [https://redis.io/commands/expiretime/](https://redis.io/commands/expiretime/)
    fn expiretime<K>(&self, key: K) -> Pin<Box<dyn Future<Output = Result<i64>> + '_>>
    where
        K: Into<BulkString>,
    {
        self.send_into(cmd("EXPIRETIME").arg(key))
    }

    /// Move key from the currently selected database to the specified destination database.
    ///
    /// # Return
    /// * `true` - if key was moved.
    /// * `false` - f key was not moved.
    ///
    /// # See Also
    /// [https://redis.io/commands/move/](https://redis.io/commands/move/)
    fn move_<K>(&self, key: K, db: usize) -> Pin<Box<dyn Future<Output = Result<bool>> + '_>>
    where
        K: Into<BulkString>,
    {
        self.send_into(cmd("MOVE").arg(key).arg(db))
    }

    /// Remove the existing timeout on key,
    /// turning the key from volatile (a key with an expire set)
    /// to persistent (a key that will never expire as no timeout is associated).
    ///
    /// # Return
    /// * `true` - if the timeout was removed.
    /// * `false` - if key does not exist or does not have an associated timeout.
    ///
    /// # See Also
    /// [https://redis.io/commands/persist/](https://redis.io/commands/persist/)
    fn persist<K>(&self, key: K) -> Pin<Box<dyn Future<Output = Result<bool>> + '_>>
    where
        K: Into<BulkString>,
    {
        self.send_into(cmd("PERSIST").arg(key))
    }

    /// This command works exactly like EXPIRE but the time to live of the key is specified in milliseconds instead of seconds.
    ///
    /// # Return
    /// * `true` - if the timeout was set.
    /// * `false` - if the timeout was not set. e.g. key doesn't exist, or operation skipped due to the provided arguments.
    ///
    /// # See Also
    /// [https://redis.io/commands/pexpire/](https://redis.io/commands/pexpire/)
    fn pexpire<K>(&self, key: K, milliseconds: u64) -> Expire<Self>
    where
        K: Into<BulkString>,
    {
        Expire {
            generic_commands: &self,
            cmd: cmd("PEXPIRE").arg(key).arg(milliseconds),
        }
    }

    /// PEXPIREAT has the same effect and semantic as EXPIREAT,
    /// but the Unix time at which the key will expire is specified in milliseconds instead of seconds.
    ///
    /// # Return
    /// * `true` - if the timeout was set.
    /// * `false` - if the timeout was not set. e.g. key doesn't exist, or operation skipped due to the provided arguments.
    ///
    /// # See Also
    /// [https://redis.io/commands/pexpireat/](https://redis.io/commands/pexpireat/)
    fn pexpireat<K>(&self, key: K, unix_time_milliseconds: u64) -> Expire<Self>
    where
        K: Into<BulkString>,
    {
        Expire {
            generic_commands: &self,
            cmd: cmd("PEXPIREAT").arg(key).arg(unix_time_milliseconds),
        }
    }

    /// PEXPIRETIME has the same semantic as EXPIRETIME,
    /// but returns the absolute Unix expiration timestamp in milliseconds instead of seconds.
    ///
    /// # Return
    ///  Expiration Unix timestamp in milliseconds, or a negative value in order to signal an error (see the description below).
    /// - The command returns -1 if the key exists but has no associated expiration time.
    /// - The command returns -2 if the key does not exist.
    ///
    /// # See Also
    /// [https://redis.io/commands/pexpiretime/](https://redis.io/commands/pexpiretime/)
    fn pexpiretime<K>(&self, key: K) -> Pin<Box<dyn Future<Output = Result<i64>> + '_>>
    where
        K: Into<BulkString>,
    {
        self.send_into(cmd("PEXPIRETIME").arg(key))
    }

    /// Returns the remaining time to live of a key that has a timeout.
    ///
    /// # Return
    /// TTL in milliseconds, or a negative value in order to signal an error:
    /// -2 if the key does not exist.
    /// -1 if the key exists but has no associated expire.
    ///
    /// # See Also
    /// [https://redis.io/commands/pttl/](https://redis.io/commands/pttl/)
    fn pttl<K>(&self, key: K) -> Pin<Box<dyn Future<Output = Result<i64>> + '_>>
    where
        K: Into<BulkString>,
    {
        self.send_into(cmd("PTTL").arg(key))
    }

    /// Returns the remaining time to live of a key that has a timeout.
    ///
    /// # Return
    /// TTL in seconds, or a negative value in order to signal an error:
    /// -2 if the key does not exist.
    /// -1 if the key exists but has no associated expire.
    ///
    /// # See Also
    /// [https://redis.io/commands/ttl/](https://redis.io/commands/ttl/)
    fn ttl<K>(&self, key: K) -> Pin<Box<dyn Future<Output = Result<i64>> + '_>>
    where
        K: Into<BulkString>,
    {
        self.send_into(cmd("TTL").arg(key))
    }

    /// Returns the string representation of the type of the value stored at key.
    ///
    /// The different types that can be returned are: string, list, set, zset, hash and stream.
    ///
    /// # Return
    /// type of key, or empty string when key does not exist.
    ///
    /// # See Also
    /// [https://redis.io/commands/type/](https://redis.io/commands/type/)
    fn type_<K>(&self, key: K) -> Pin<Box<dyn Future<Output = Result<String>> + '_>>
    where
        K: Into<BulkString>,
    {
        self.send_into(cmd("TYPE").arg(key))
    }
}

/// Builder for the [copy](crate::GenericCommands::copy) command
pub struct Copy<'a, T: GenericCommands + ?Sized> {
    generic_commands: &'a T,
    cmd: Command,
}

impl<'a, T: GenericCommands> Copy<'a, T> {
    /// Allows specifying an alternative logical database index for the destination key.
    pub fn db(self, destination_db: usize) -> Self {
        Self {
            generic_commands: self.generic_commands,
            cmd: self.cmd.arg("DB").arg(destination_db),
        }
    }

    /// Removes the destination key before copying the value to it
    pub fn replace(self) -> Self {
        Self {
            generic_commands: self.generic_commands,
            cmd: self.cmd.arg("REPLACE"),
        }
    }

    /// Execute the command
    ///
    /// # Return
    ///  Success of the operation
    pub fn execute(self) -> Pin<Box<dyn Future<Output = Result<bool>> + 'a>> {
        self.generic_commands.send_into(self.cmd)
    }
}

/// Builder for the [expire](crate::GenericCommands::expire) command
pub struct Expire<'a, T: GenericCommands + ?Sized> {
    generic_commands: &'a T,
    cmd: Command,
}

impl<'a, T: GenericCommands> Expire<'a, T> {
    /// Set expiry only when the key has no expiry
    pub fn nx(self) -> Pin<Box<dyn Future<Output = Result<bool>> + 'a>> {
        self.generic_commands.send_into(self.cmd.arg("NX"))
    }

    /// Set expiry only when the key has an existing expiry
    pub fn xx(self) -> Pin<Box<dyn Future<Output = Result<bool>> + 'a>> {
        self.generic_commands.send_into(self.cmd.arg("XX"))
    }

    /// Set expiry only when the new expiry is greater than current one
    pub fn gt(self) -> Pin<Box<dyn Future<Output = Result<bool>> + 'a>> {
        self.generic_commands.send_into(self.cmd.arg("GT"))
    }

    /// Set expiry only when the new expiry is less than current one
    pub fn lt(self) -> Pin<Box<dyn Future<Output = Result<bool>> + 'a>> {
        self.generic_commands.send_into(self.cmd.arg("LT"))
    }

    /// execute with no option
    pub fn execute(self) -> Pin<Box<dyn Future<Output = Result<bool>> + 'a>> {
        self.generic_commands.send_into(self.cmd)
    }
}