1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use super::Queue;
use crate::{Action, History, Slot};
#[derive(Debug)]
enum CheckpointEntry {
Apply(usize),
Undo,
Redo,
}
#[derive(Debug)]
pub struct Checkpoint<'a, A, S> {
history: &'a mut History<A, S>,
entries: Vec<CheckpointEntry>,
}
impl<A, S> Checkpoint<'_, A, S> {
pub fn queue(&mut self) -> Queue<A, S> {
self.history.queue()
}
pub fn checkpoint(&mut self) -> Checkpoint<A, S> {
self.history.checkpoint()
}
}
impl<A: Action, S: Slot> Checkpoint<'_, A, S> {
pub fn apply(&mut self, target: &mut A::Target, action: A) -> A::Output {
let branch = self.history.branch();
self.entries.push(CheckpointEntry::Apply(branch));
self.history.apply(target, action)
}
pub fn undo(&mut self, target: &mut A::Target) -> Option<A::Output> {
self.entries.push(CheckpointEntry::Undo);
self.history.undo(target)
}
pub fn redo(&mut self, target: &mut A::Target) -> Option<A::Output> {
self.entries.push(CheckpointEntry::Redo);
self.history.redo(target)
}
pub fn commit(self) {}
pub fn cancel(self, target: &mut A::Target) -> Vec<A::Output> {
self.entries
.into_iter()
.rev()
.filter_map(|entry| match entry {
CheckpointEntry::Apply(branch) => {
let output = self.history.undo(target)?;
let root = self.history.branch();
if root == branch {
self.history.record.entries.pop_back();
} else {
self.history.jump_to(branch);
self.history.branches.remove(&root).unwrap();
}
Some(output)
}
CheckpointEntry::Undo => self.history.redo(target),
CheckpointEntry::Redo => self.history.undo(target),
})
.collect()
}
}
impl<'a, A, S> From<&'a mut History<A, S>> for Checkpoint<'a, A, S> {
fn from(history: &'a mut History<A, S>) -> Self {
Checkpoint {
history,
entries: Vec::new(),
}
}
}
#[cfg(test)]
mod tests {
use crate::*;
const A: FromFn<fn(&mut String), String> = FromFn::new(|s| s.push('a'));
const B: FromFn<fn(&mut String), String> = FromFn::new(|s| s.push('b'));
const C: FromFn<fn(&mut String), String> = FromFn::new(|s| s.push('c'));
const D: FromFn<fn(&mut String), String> = FromFn::new(|s| s.push('d'));
const E: FromFn<fn(&mut String), String> = FromFn::new(|s| s.push('e'));
#[test]
fn checkpoint() {
let mut target = String::new();
let mut history = History::new();
let mut checkpoint = history.checkpoint();
checkpoint.apply(&mut target, A);
checkpoint.apply(&mut target, B);
checkpoint.apply(&mut target, C);
assert_eq!(target, "abc");
checkpoint.undo(&mut target);
checkpoint.undo(&mut target);
assert_eq!(target, "a");
checkpoint.apply(&mut target, D);
checkpoint.apply(&mut target, E);
assert_eq!(target, "ade");
checkpoint.cancel(&mut target);
assert_eq!(target, "");
}
}