use crate::resp::{Command, StateSlot};
#[derive(Debug, Default, Clone)]
pub(crate) struct ConnectionState {
slots: [Option<Command>; StateSlot::ALL.len()],
}
impl ConnectionState {
pub(crate) fn record(&mut self, slot: StateSlot, command: &Command) {
if let Some(entry) = self.slots.get_mut(slot.index()) {
*entry = Some(command.clone());
}
}
pub(crate) fn clear(&mut self) {
self.slots = Default::default();
}
pub(crate) fn forget(&mut self, slot: StateSlot) {
if let Some(entry) = self.slots.get_mut(slot.index()) {
*entry = None;
}
}
pub(crate) fn holds(&self, slot: StateSlot) -> bool {
self.slots.get(slot.index()).is_some_and(Option::is_some)
}
pub(crate) fn commands(&self) -> Vec<(StateSlot, Command)> {
StateSlot::ALL
.iter()
.filter_map(|slot| {
self.slots
.get(slot.index())
.and_then(|entry| entry.as_ref())
.map(|command| (*slot, command.clone()))
})
.collect()
}
pub(crate) fn is_reply_on(&self) -> bool {
self.slots
.get(StateSlot::ReplyMode.index())
.and_then(|entry| entry.as_ref())
.is_none_or(|command| {
command
.get_arg(1)
.is_none_or(|mode| mode.eq_ignore_ascii_case(b"ON"))
})
}
}