use crate::ValkeyFuture;
use crate::commands::core::AsyncCommands;
use crate::value::FromValkeyValue;
use crate::write::ToValkeyArgs;
#[cfg(feature = "sync")]
use crate::{ValkeyResult, commands::core::Commands};
pub fn cmd(name: &str) -> Cmd {
let mut command = Cmd::new();
command.arg(name);
command
}
#[derive(Clone, Default)]
pub struct Cmd {
inner: redis::Cmd,
}
impl Cmd {
pub fn new() -> Self {
Cmd {
inner: redis::Cmd::new(),
}
}
#[inline]
pub fn arg<A: ToValkeyArgs>(&mut self, arg: A) -> &mut Self {
arg.write_valkey_args(self);
self
}
#[inline]
pub fn query_async<'a, RV: FromValkeyValue>(
&self,
con: &'a impl AsyncCommands,
) -> ValkeyFuture<'a, RV> {
con.glide_send_command_as(self.clone())
}
#[inline]
pub fn exec_async<'a>(&self, con: &'a impl AsyncCommands) -> ValkeyFuture<'a, ()> {
self.query_async(con)
}
#[cfg(feature = "sync")]
#[inline]
pub fn query<RV: FromValkeyValue>(&self, con: &impl Commands) -> ValkeyResult<RV> {
con.glide_send_command_as(self.clone())
}
#[cfg(feature = "sync")]
#[inline]
pub fn exec(&self, con: &impl Commands) -> ValkeyResult<()> {
self.query(con)
}
pub(crate) fn as_redis(&self) -> &redis::Cmd {
&self.inner
}
pub(crate) fn as_redis_mut(&mut self) -> &mut redis::Cmd {
&mut self.inner
}
pub(crate) fn into_redis(self) -> redis::Cmd {
self.inner
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn matches_redis_cmd_single_arg() {
let mut v = Cmd::new();
v.arg("SET").arg("key").arg(42i64);
let mut r = redis::Cmd::new();
r.arg("SET").arg("key").arg(42i64);
assert_eq!(v.as_redis().get_packed_command(), r.get_packed_command());
}
#[test]
fn matches_redis_cmd_multi_arg() {
let mut v = Cmd::new();
v.arg("MGET").arg(&["a", "b", "c"][..]);
let mut r = redis::Cmd::new();
r.arg("MGET").arg(&["a", "b", "c"][..]);
assert_eq!(v.as_redis().get_packed_command(), r.get_packed_command());
}
}