1use std::mem;
2
3#[derive(Debug, Clone)]
4pub enum RedisFmt {
5 Cmd(&'static str),
6 Raw(Vec<u8>),
7 CRLF,
8}
9
10impl RedisFmt {
11 fn is_crlf(&self) -> bool {
12 match self {
13 &RedisFmt::CRLF => true,
14 _ => false,
15 }
16 }
17 pub fn into_data(self) -> Vec<u8> {
18 match self {
19 RedisFmt::Cmd(cmd) => cmd.to_owned().into_bytes(),
20 RedisFmt::Raw(buf) => buf,
21 RedisFmt::CRLF => b"\r\n".to_vec(),
22 }
23 }
24}
25
26pub trait RedisFormat
27 where Self: Sized
28{
29 fn fmt(self, buf: &mut Vec<RedisFmt>) -> usize;
30}
31
32
33#[derive(Debug, Clone)]
34pub struct RedisCmd(pub Vec<RedisFmt>);
35
36impl RedisCmd {
37 pub fn into_data(self) -> Vec<Vec<u8>> {
38 let RedisCmd(cmds) = self;
39 cmds.into_iter().map(|x| x.into_data()).collect()
40 }
41}
42
43pub trait Group {
44 fn group(self) -> Vec<RedisCmd>;
45}
46
47impl Group for Vec<RedisFmt> {
48 fn group(self) -> Vec<RedisCmd> {
49 let mut group = vec![];
50 let mut local = vec![];
51 for fmt in self {
52 if fmt.is_crlf() {
53 let mut tmp = vec![];
54 mem::swap(&mut tmp, &mut local);
55 group.push(RedisCmd(tmp));
56 continue;
57 }
58 local.push(fmt);
59 }
60 group
61 }
62}