use crate::ValkeyResult;
use crate::cmd::Cmd;
use crate::routes::Route;
use crate::value::ValkeyValue;
use crate::write::ToValkeyArgs;
use async_trait::async_trait;
#[async_trait]
pub trait CommandExecutor: Send + Sync {
async fn execute_command(&self, cmd: Cmd, route: Option<Route>) -> ValkeyResult<ValkeyValue>;
}
#[async_trait]
pub trait CustomCommand: CommandExecutor {
async fn custom_command<A>(&self, args: &[A]) -> ValkeyResult<ValkeyValue>
where
A: ToValkeyArgs + Sync,
{
let mut cmd = Cmd::new();
for a in args {
cmd.arg(a);
}
self.execute_command(cmd, None).await
}
async fn custom_command_with_route<A>(
&self,
args: &[A],
route: Route,
) -> ValkeyResult<ValkeyValue>
where
A: ToValkeyArgs + Sync,
{
let mut cmd = Cmd::new();
for a in args {
cmd.arg(a);
}
self.execute_command(cmd, Some(route)).await
}
}
impl<T: CommandExecutor + ?Sized> CustomCommand for T {}