embedded_redis/commands/
helpers.rs

1//! Helpers for command abstraction.
2use alloc::borrow::ToOwned;
3use alloc::string::{String, ToString};
4use bytes::Bytes;
5use redis_protocol::resp2::types::BytesFrame as Resp2Frame;
6use redis_protocol::resp3::types::{BytesFrame as Resp3Frame, FrameMap, Resp3Frame as _};
7
8/// Helper for casting Strings to frame types
9pub struct CmdStr<'a> {
10    inner: &'a str,
11}
12
13impl<'a> CmdStr<'a> {
14    pub fn new(inner: &'a str) -> Self {
15        CmdStr { inner }
16    }
17
18    pub fn to_blob(self) -> Resp3Frame {
19        Resp3Frame::BlobString {
20            data: Bytes::from(self.inner.to_owned()),
21            attributes: None,
22        }
23    }
24
25    pub fn to_simple(self) -> Resp3Frame {
26        Resp3Frame::SimpleString {
27            data: Bytes::from(self.inner.to_owned()),
28            attributes: None,
29        }
30    }
31}
32
33impl CmdStr<'static> {
34    pub fn to_bulk(self) -> Resp2Frame {
35        Resp2Frame::BulkString(Bytes::from(self.inner))
36    }
37}
38
39/// Helper for casting response integers to frame types
40pub struct RespInt {
41    inner: i64,
42}
43
44impl RespInt {
45    pub fn new(inner: i64) -> Self {
46        RespInt { inner }
47    }
48
49    pub fn to_number(&self) -> Resp3Frame {
50        Resp3Frame::Number {
51            data: self.inner,
52            attributes: None,
53        }
54    }
55}
56
57/// Helper for casting integers to frame types
58pub struct CmdInt {
59    inner: usize,
60}
61
62impl CmdInt {
63    pub fn new(inner: usize) -> Self {
64        CmdInt { inner }
65    }
66
67    pub fn to_blob(&self) -> Resp3Frame {
68        Resp3Frame::BlobString {
69            data: Bytes::from(self.inner.to_string()),
70            attributes: None,
71        }
72    }
73
74    pub fn to_bulk_string(&self) -> Resp2Frame {
75        Resp2Frame::BulkString(Bytes::from(self.inner.to_string()))
76    }
77}
78
79/// Helper for casting Bytes to frames
80pub struct CmdBytes {
81    inner: Bytes,
82}
83
84impl CmdBytes {
85    pub fn new(bytes: &Bytes) -> Self {
86        CmdBytes { inner: bytes.clone() }
87    }
88
89    pub fn to_blob(&self) -> Resp3Frame {
90        Resp3Frame::BlobString {
91            data: self.inner.clone(),
92            attributes: None,
93        }
94    }
95}
96
97/// Helper for finding & casting map elements
98pub struct RespMap<'a> {
99    inner: &'a FrameMap<Resp3Frame, Resp3Frame>,
100}
101
102impl<'a> RespMap<'a> {
103    pub fn new(inner: &'a FrameMap<Resp3Frame, Resp3Frame>) -> Self {
104        RespMap { inner }
105    }
106
107    pub fn find_string(&self, key: &str) -> Option<String> {
108        self.inner.get(&CmdStr::new(key).to_blob())?.to_string()
109    }
110
111    pub fn find_integer(&self, key: &str) -> Option<i64> {
112        let element = self.inner.get(&CmdStr::new(key).to_blob())?;
113
114        match element {
115            Resp3Frame::Number { data, attributes: _ } => Some(*data),
116            _ => None,
117        }
118    }
119}