use crate::resp::{CommandArgs, ToArgs};
#[cfg(debug_assertions)]
use std::sync::atomic::{AtomicUsize, Ordering};
#[cfg(debug_assertions)]
static COMMAND_SEQUENCE_COUNTER: AtomicUsize = AtomicUsize::new(0);
#[must_use]
#[inline(always)]
pub fn cmd(name: &'static str) -> Command {
Command::new(name)
}
#[derive(Debug, Clone)]
pub struct Command {
pub name: &'static str,
pub args: CommandArgs,
#[doc(hidden)]
#[cfg(debug_assertions)]
pub kill_connection_on_write: usize,
#[cfg(debug_assertions)]
#[allow(unused)]
pub (crate) command_seq: usize,
}
impl Command {
#[must_use]
#[inline(always)]
pub fn new(name: &'static str) -> Self {
Self {
name,
args: CommandArgs::default(),
#[cfg(debug_assertions)]
kill_connection_on_write: 0,
#[cfg(debug_assertions)]
command_seq: COMMAND_SEQUENCE_COUNTER.fetch_add(1, Ordering::SeqCst),
}
}
#[must_use]
#[inline(always)]
pub fn arg<A>(mut self, arg: A) -> Self
where
A: ToArgs,
{
arg.write_args(&mut self.args);
self
}
#[must_use]
#[inline(always)]
pub fn arg_if<A>(mut self, condition: bool, arg: A) -> Self
where
A: ToArgs,
{
if condition {
arg.write_args(&mut self.args);
}
self
}
#[cfg(debug_assertions)]
#[inline]
pub fn kill_connection_on_write(mut self, num_kills: usize) -> Self {
self.kill_connection_on_write = num_kills;
self
}
}