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
use crate::commands::custom::CustomCommand;
use alloc::string::{String, ToString};
use alloc::vec;
use alloc::vec::Vec;
use bytes::Bytes;
use redis_protocol::resp2::types::Frame as Resp2Frame;
use redis_protocol::resp3::types::Frame as Resp3Frame;
#[derive(Clone, Default)]
pub struct CommandBuilder {
pub(crate) elements: Vec<Bytes>,
}
impl CommandBuilder {
pub fn new(keyword: &'static str) -> Self {
CommandBuilder {
elements: vec![Bytes::from_static(keyword.as_bytes())],
}
}
pub fn to_command(self) -> CustomCommand {
self.into()
}
pub fn arg_static(mut self, arg: &'static str) -> Self {
self.elements.push(Bytes::from_static(arg.as_bytes()));
self
}
pub fn arg_static_option(mut self, arg: Option<&'static str>) -> Self {
if let Some(arg_str) = arg {
self.elements.push(Bytes::from_static(arg_str.as_bytes()));
}
self
}
pub fn arg_uint(mut self, arg: usize) -> Self {
self.elements.push(Bytes::from(arg.to_string()));
self
}
pub fn arg(mut self, arg: &Bytes) -> Self {
self.elements.push(arg.clone());
self
}
pub fn arg_option(mut self, arg: Option<&Bytes>) -> Self {
if let Some(inner) = arg {
self.elements.push(inner.clone());
}
self
}
}
impl From<CommandBuilder> for Resp2Frame {
fn from(builder: CommandBuilder) -> Self {
let mut frames = Vec::with_capacity(builder.elements.len());
for byte in builder.elements {
frames.push(Resp2Frame::BulkString(byte));
}
Resp2Frame::Array(frames)
}
}
impl From<CommandBuilder> for Resp3Frame {
fn from(builder: CommandBuilder) -> Self {
let mut frames = Vec::with_capacity(builder.elements.len());
for byte in builder.elements {
frames.push(Resp3Frame::BlobString {
data: byte,
attributes: None,
});
}
Resp3Frame::Array {
data: frames,
attributes: None,
}
}
}
impl From<CommandBuilder> for CustomCommand {
fn from(builder: CommandBuilder) -> Self {
CustomCommand::new(builder)
}
}
pub trait ToStringOption {
fn to_string_option(&self) -> Option<String>;
}
impl ToStringOption for Resp2Frame {
fn to_string_option(&self) -> Option<String> {
self.to_string()
}
}
impl ToStringOption for Resp3Frame {
fn to_string_option(&self) -> Option<String> {
self.to_string()
}
}
pub trait IsNullFrame {
fn is_null_frame(&self) -> bool;
}
impl IsNullFrame for Resp2Frame {
fn is_null_frame(&self) -> bool {
self.is_null()
}
}
impl IsNullFrame for Resp3Frame {
fn is_null_frame(&self) -> bool {
self.is_null()
}
}
pub trait ToInteger {
fn to_integer(&self) -> Option<i64>;
}
impl ToInteger for Resp2Frame {
fn to_integer(&self) -> Option<i64> {
match self {
Resp2Frame::Integer(number) => Some(*number),
_ => None,
}
}
}
impl ToInteger for Resp3Frame {
fn to_integer(&self) -> Option<i64> {
match self {
Resp3Frame::Number { data, attributes: _ } => Some(*data),
_ => None,
}
}
}
pub trait ToStringBytes {
fn to_string_bytes(&self) -> Option<Bytes>;
}
impl ToStringBytes for Resp2Frame {
fn to_string_bytes(&self) -> Option<Bytes> {
match self {
Resp2Frame::BulkString(data) => Some(data.clone()),
_ => None,
}
}
}
impl ToStringBytes for Resp3Frame {
fn to_string_bytes(&self) -> Option<Bytes> {
match self {
Resp3Frame::BlobString { data, attributes: _ } => Some(data.clone()),
_ => None,
}
}
}