redis_asio/base/
command.rs1use crate::RespInternalValue;
2
3
4pub fn command(cmd: &str) -> RedisCommand {
14 RedisCommand::cmd(cmd)
15}
16
17pub enum RedisArgument {
20 Int(i64),
21 String(String),
22 Bytes(Vec<u8>),
23}
24
25#[derive(Clone)]
27pub struct RedisCommand {
28 args: Vec<RespInternalValue>,
29}
30
31pub trait IntoRedisArgument {
50 fn into_redis_argument(self) -> RedisArgument;
51}
52
53impl RedisCommand {
54 pub(crate) fn new() -> RedisCommand {
55 RedisCommand { args: Vec::new() }
56 }
57
58 pub(crate) fn cmd(cmd: &str) -> RedisCommand {
60 RedisCommand {
61 args: vec![RespInternalValue::BulkString(cmd.as_bytes().to_vec())]
62 }
63 }
64
65 pub fn arg<T: IntoRedisArgument>(mut self, arg: T) -> RedisCommand {
68 self.args.push(arg.into_redis_argument().into_resp_value());
69 self
70 }
71
72 pub fn arg_mut<T: IntoRedisArgument>(&mut self, arg: T) {
75 self.args.push(arg.into_redis_argument().into_resp_value());
76 }
77
78 pub fn append(&mut self, mut other: RedisCommand) {
80 self.args.append(&mut other.args);
81 }
82
83 pub fn into_resp_value(self) -> RespInternalValue {
86 RespInternalValue::Array(self.args)
87 }
88}
89
90impl RedisArgument {
91 pub(crate) fn into_resp_value(self) -> RespInternalValue {
92 match self {
93 RedisArgument::Int(x) => RespInternalValue::BulkString(x.to_string().into()),
94 RedisArgument::String(x) => RespInternalValue::BulkString(x.into()),
95 RedisArgument::Bytes(x) => RespInternalValue::BulkString(x),
96 }
97 }
98}
99
100impl IntoRedisArgument for RedisArgument {
101 fn into_redis_argument(self) -> RedisArgument {
102 self
103 }
104}
105
106impl IntoRedisArgument for &str {
107 fn into_redis_argument(self) -> RedisArgument {
108 RedisArgument::String(self.to_string())
109 }
110}
111
112impl IntoRedisArgument for String {
113 fn into_redis_argument(self) -> RedisArgument {
114 RedisArgument::String(self)
115 }
116}
117
118impl IntoRedisArgument for Vec<u8> {
119 fn into_redis_argument(self) -> RedisArgument {
120 RedisArgument::Bytes(self)
121 }
122}
123
124macro_rules! declare_to_int_argument {
125 ($itype:ty) => {
126 impl IntoRedisArgument for $itype {
127 fn into_redis_argument(self) -> RedisArgument {
128 RedisArgument::Int(self as i64)
129 }
130 }
131 };
132}
133
134declare_to_int_argument!(u8);
135declare_to_int_argument!(i8);
136declare_to_int_argument!(u16);
137declare_to_int_argument!(i16);
138declare_to_int_argument!(u32);
139declare_to_int_argument!(i32);
140declare_to_int_argument!(i64);
141declare_to_int_argument!(u64);
142declare_to_int_argument!(usize);