use crate::{Edit, History, Slot};
use alloc::vec::Vec;
#[derive(Debug)]
enum QueueEntry<E> {
Edit(E),
Undo,
Redo,
}
#[derive(Debug)]
pub struct Queue<'a, E, S> {
history: &'a mut History<E, S>,
entries: Vec<QueueEntry<E>>,
}
impl<E, S> Queue<'_, E, S> {
pub fn reserve(&mut self, additional: usize) {
self.entries.reserve(additional);
}
pub fn edit(&mut self, edit: E) {
self.entries.push(QueueEntry::Edit(edit));
}
pub fn undo(&mut self) {
self.entries.push(QueueEntry::Undo);
}
pub fn redo(&mut self) {
self.entries.push(QueueEntry::Redo);
}
pub fn cancel(self) {}
}
impl<E: Edit, S: Slot> Queue<'_, E, S> {
pub fn commit(self, target: &mut E::Target) -> Vec<E::Output> {
self.entries
.into_iter()
.filter_map(|entry| match entry {
QueueEntry::Edit(edit) => Some(self.history.edit(target, edit)),
QueueEntry::Undo => self.history.undo(target),
QueueEntry::Redo => self.history.redo(target),
})
.collect()
}
}
impl<'a, E, S> From<&'a mut History<E, S>> for Queue<'a, E, S> {
fn from(history: &'a mut History<E, S>) -> Self {
Queue {
history,
entries: Vec::new(),
}
}
}