use crate::error::Result;
use crate::routes::Route;
use async_trait::async_trait;
use redis::cluster_routing::RoutingInfo;
use redis::{Cmd, Value};
#[async_trait]
pub trait CommandExecutor: Send + Sync {
async fn execute_command(&self, cmd: Cmd, routing: Option<RoutingInfo>) -> Result<Value>;
}
#[async_trait]
pub trait CustomCommand: CommandExecutor {
async fn custom_command<A>(&self, args: &[A]) -> Result<Value>
where
A: redis::ToRedisArgs + 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) -> Result<Value>
where
A: redis::ToRedisArgs + Sync,
{
let mut cmd = Cmd::new();
for a in args {
cmd.arg(a);
}
let routing = route.to_routing_info(Some(&cmd));
self.execute_command(cmd, Some(routing)).await
}
}
impl<T: CommandExecutor + ?Sized> CustomCommand for T {}