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
use std::mem; #[derive(Debug, Clone)] pub enum RedisFmt { Cmd(&'static str), Raw(Vec<u8>), CRLF, } impl RedisFmt { fn is_crlf(&self) -> bool { match self { &RedisFmt::CRLF => true, _ => false, } } pub fn into_data(self) -> Vec<u8> { match self { RedisFmt::Cmd(cmd) => cmd.to_owned().into_bytes(), RedisFmt::Raw(buf) => buf, RedisFmt::CRLF => b"\r\n".to_vec(), } } } pub trait RedisFormat where Self: Sized { fn fmt(self, buf: &mut Vec<RedisFmt>) -> usize; } #[derive(Debug, Clone)] pub struct RedisCmd(pub Vec<RedisFmt>); impl RedisCmd { pub fn into_data(self) -> Vec<Vec<u8>> { let RedisCmd(cmds) = self; cmds.into_iter().map(|x| x.into_data()).collect() } } pub trait Group { fn group(self) -> Vec<RedisCmd>; } impl Group for Vec<RedisFmt> { fn group(self) -> Vec<RedisCmd> { let mut group = vec![]; let mut local = vec![]; for fmt in self { if fmt.is_crlf() { let mut tmp = vec![]; mem::swap(&mut tmp, &mut local); group.push(RedisCmd(tmp)); continue; } local.push(fmt); } group } }