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
310
311
312
313
314
315
316
317
318
319
320
use crate::connection::{Connection, ConnectionError};
use crate::frame::Frame;
use bytes::Bytes;
use std::str::{self, Utf8Error};
use thiserror::Error;
pub trait ToSegmentFrame {
fn to_segment_frame(&self) -> Frame;
}
pub trait FromSegmentFrame: Sized {
fn from_segment_frame(frame: &Frame) -> Result<Self, CommandError>;
}
#[derive(Debug)]
pub struct Command {
args: Vec<Frame>,
}
#[derive(Debug, Error)]
pub enum CommandError {
#[error("incompatible response type")]
IncompatibleType,
#[error(transparent)]
Utf8Error(#[from] Utf8Error),
#[error("segment: {0}")]
QueryError(String),
#[error(transparent)]
ConnectionError(#[from] ConnectionError),
}
impl Command {
pub fn new() -> Self {
Command { args: Vec::new() }
}
pub fn arg<T: ToSegmentFrame>(&mut self, arg: T) -> &mut Self {
self.args.push(arg.to_segment_frame());
self
}
pub async fn query<T: FromSegmentFrame>(
self,
connection: &mut Connection,
) -> Result<T, CommandError> {
let cmd = Frame::Array(self.args);
connection.write_frame(&cmd).await?;
let response = connection.read_frame().await?;
match response {
Frame::Error(val) => Err(CommandError::QueryError(
str::from_utf8(&val[..])?.to_string(),
)),
_ => T::from_segment_frame(&response),
}
}
}
impl Default for Command {
fn default() -> Self {
Self::new()
}
}
impl ToSegmentFrame for u8 {
fn to_segment_frame(&self) -> Frame {
Frame::Integer(*self as i64)
}
}
impl ToSegmentFrame for i8 {
fn to_segment_frame(&self) -> Frame {
Frame::Integer(*self as i64)
}
}
impl ToSegmentFrame for u16 {
fn to_segment_frame(&self) -> Frame {
Frame::Integer(*self as i64)
}
}
impl ToSegmentFrame for i16 {
fn to_segment_frame(&self) -> Frame {
Frame::Integer(*self as i64)
}
}
impl ToSegmentFrame for u32 {
fn to_segment_frame(&self) -> Frame {
Frame::Integer(*self as i64)
}
}
impl ToSegmentFrame for i32 {
fn to_segment_frame(&self) -> Frame {
Frame::Integer(*self as i64)
}
}
impl ToSegmentFrame for u64 {
fn to_segment_frame(&self) -> Frame {
Frame::Integer(*self as i64)
}
}
impl ToSegmentFrame for i64 {
fn to_segment_frame(&self) -> Frame {
Frame::Integer(*self as i64)
}
}
impl ToSegmentFrame for usize {
fn to_segment_frame(&self) -> Frame {
Frame::Integer(*self as i64)
}
}
impl ToSegmentFrame for isize {
fn to_segment_frame(&self) -> Frame {
Frame::Integer(*self as i64)
}
}
impl ToSegmentFrame for f32 {
fn to_segment_frame(&self) -> Frame {
Frame::Double(*self as f64)
}
}
impl ToSegmentFrame for f64 {
fn to_segment_frame(&self) -> Frame {
Frame::Double(*self as f64)
}
}
impl ToSegmentFrame for bool {
fn to_segment_frame(&self) -> Frame {
Frame::Boolean(*self)
}
}
impl ToSegmentFrame for String {
fn to_segment_frame(&self) -> Frame {
Frame::String(Bytes::from(self.clone()))
}
}
impl ToSegmentFrame for &str {
fn to_segment_frame(&self) -> Frame {
Frame::String(Bytes::from(self.to_string()))
}
}
impl ToSegmentFrame for Bytes {
fn to_segment_frame(&self) -> Frame {
Frame::String(self.clone())
}
}
impl<T: ToSegmentFrame> ToSegmentFrame for Option<T> {
fn to_segment_frame(&self) -> Frame {
if let Some(val) = self {
return T::to_segment_frame(val);
}
Frame::Null
}
}
impl FromSegmentFrame for u8 {
fn from_segment_frame(frame: &Frame) -> Result<Self, CommandError> {
match frame {
Frame::Integer(val) => Ok(*val as u8),
Frame::Double(val) => Ok(*val as u8),
_ => Err(CommandError::IncompatibleType),
}
}
}
impl FromSegmentFrame for i8 {
fn from_segment_frame(frame: &Frame) -> Result<Self, CommandError> {
match frame {
Frame::Integer(val) => Ok(*val as i8),
Frame::Double(val) => Ok(*val as i8),
_ => Err(CommandError::IncompatibleType),
}
}
}
impl FromSegmentFrame for u16 {
fn from_segment_frame(frame: &Frame) -> Result<Self, CommandError> {
match frame {
Frame::Integer(val) => Ok(*val as u16),
Frame::Double(val) => Ok(*val as u16),
_ => Err(CommandError::IncompatibleType),
}
}
}
impl FromSegmentFrame for i16 {
fn from_segment_frame(frame: &Frame) -> Result<Self, CommandError> {
match frame {
Frame::Integer(val) => Ok(*val as i16),
Frame::Double(val) => Ok(*val as i16),
_ => Err(CommandError::IncompatibleType),
}
}
}
impl FromSegmentFrame for u32 {
fn from_segment_frame(frame: &Frame) -> Result<Self, CommandError> {
match frame {
Frame::Integer(val) => Ok(*val as u32),
Frame::Double(val) => Ok(*val as u32),
_ => Err(CommandError::IncompatibleType),
}
}
}
impl FromSegmentFrame for i32 {
fn from_segment_frame(frame: &Frame) -> Result<Self, CommandError> {
match frame {
Frame::Integer(val) => Ok(*val as i32),
Frame::Double(val) => Ok(*val as i32),
_ => Err(CommandError::IncompatibleType),
}
}
}
impl FromSegmentFrame for u64 {
fn from_segment_frame(frame: &Frame) -> Result<Self, CommandError> {
match frame {
Frame::Integer(val) => Ok(*val as u64),
Frame::Double(val) => Ok(*val as u64),
_ => Err(CommandError::IncompatibleType),
}
}
}
impl FromSegmentFrame for i64 {
fn from_segment_frame(frame: &Frame) -> Result<Self, CommandError> {
match frame {
Frame::Integer(val) => Ok(*val),
Frame::Double(val) => Ok(*val as i64),
_ => Err(CommandError::IncompatibleType),
}
}
}
impl FromSegmentFrame for f32 {
fn from_segment_frame(frame: &Frame) -> Result<Self, CommandError> {
match frame {
Frame::Integer(val) => Ok(*val as f32),
Frame::Double(val) => Ok(*val as f32),
_ => Err(CommandError::IncompatibleType),
}
}
}
impl FromSegmentFrame for f64 {
fn from_segment_frame(frame: &Frame) -> Result<Self, CommandError> {
match frame {
Frame::Integer(val) => Ok(*val as f64),
Frame::Double(val) => Ok(*val),
_ => Err(CommandError::IncompatibleType),
}
}
}
impl FromSegmentFrame for bool {
fn from_segment_frame(frame: &Frame) -> Result<Self, CommandError> {
match frame {
Frame::Boolean(val) => Ok(*val),
_ => Err(CommandError::IncompatibleType),
}
}
}
impl FromSegmentFrame for String {
fn from_segment_frame(frame: &Frame) -> Result<Self, CommandError> {
match frame {
Frame::String(val) => Ok(str::from_utf8(&val[..])?.to_string()),
_ => Err(CommandError::IncompatibleType),
}
}
}
impl FromSegmentFrame for Bytes {
fn from_segment_frame(frame: &Frame) -> Result<Self, CommandError> {
match frame {
Frame::String(val) => Ok(val.clone()),
_ => Err(CommandError::IncompatibleType),
}
}
}
impl<T: FromSegmentFrame> FromSegmentFrame for Option<T> {
fn from_segment_frame(frame: &Frame) -> Result<Self, CommandError> {
match frame {
Frame::Null => Ok(None),
_ => Ok(Some(T::from_segment_frame(frame)?)),
}
}
}