use std::fmt::{Debug, Formatter};
use redis::Cmd;
pub trait CommandBuilder: Send + Sync {
fn build(&self) -> Cmd;
fn needs_result(&self) -> bool;
fn box_clone(&self) -> Box<dyn CommandBuilder>;
fn command_name(&self) -> &str;
fn keys(&self) -> Vec<String>;
fn cmd_to_string(&self) -> String {
let cmd = self.build();
let mut parts = Vec::new();
parts.push(self.command_name().to_uppercase());
for arg in cmd.args_iter().skip(1) {
parts.push(format!("{:?}", arg));
}
parts.join(":")
}
}
impl Clone for Box<dyn CommandBuilder> {
fn clone(&self) -> Self {
self.box_clone()
}
}
impl Debug for dyn CommandBuilder {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "CommandBuilder({})", self.cmd_to_string())
}
}