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
/// A Redis EXISTS command.
use crate::cmd::Command;
use crate::frame::Frame;
use bytes::Bytes;
pub struct Exists {
keys: Vec<String>,
}
impl Exists {
/// Creates a new Exists command.
///
/// # Arguments
///
/// * `keys` - The keys to check for existence in the Redis server
///
/// # Returns
///
/// A new Exists command
///
/// # Examples
///
/// ```ignore
/// let exists = Exists::new(vec!["key1", "key2"]);
/// ```
pub fn new(keys: Vec<&str>) -> Self {
Self {
keys: keys.iter().map(|s| s.to_string()).collect(),
}
}
}
impl Command for Exists {
fn into_stream(self) -> Frame {
let mut frame: Frame = Frame::array();
frame
.push_frame_to_array(Frame::BulkString("EXISTS".into()))
.unwrap();
for key in self.keys {
frame
.push_frame_to_array(Frame::BulkString(Bytes::from(key)))
.unwrap();
}
frame
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_exists() {
let exists = Exists::new(vec!["key1", "key2"]);
let frame = exists.into_stream();
assert_eq!(
frame,
Frame::Array(vec![
Frame::BulkString("EXISTS".into()),
Frame::BulkString("key1".into()),
Frame::BulkString("key2".into()),
])
)
}
}