@@ -57,37 +57,31 @@ impl History {
57 self.cursor == 0
57 self.cursor == 0
60+ // TODO: I'd like to pass Transaction by reference but it fights with the borrowck
60− pub fn undo(&mut self, state: &mut State) {
62+ pub fn undo(&mut self) -> Option<Transaction> {
61 if self.at_root() {
63 if self.at_root() {
62 // We're at the root of undo, nothing to do.
64 // We're at the root of undo, nothing to do.
63− return;
65+ return None;
66 let current_revision = &self.revisions[self.cursor];
68 let current_revision = &self.revisions[self.cursor];
68− // TODO: pass the return value through? It should always succeed
69− let success = current_revision.revert.apply(state);
72− panic!("Failed to apply undo!");
75 self.cursor = current_revision.parent;
70 self.cursor = current_revision.parent;
72+ Some(current_revision.revert.clone())
78− pub fn redo(&mut self, state: &mut State) {
75+ pub fn redo(&mut self) -> Option<Transaction> {
79 let current_revision = &self.revisions[self.cursor];
76 let current_revision = &self.revisions[self.cursor];
81 // for now, simply pick the latest child (linear undo / redo)
78 // for now, simply pick the latest child (linear undo / redo)
82 if let Some((index, transaction)) = current_revision.children.last() {
79 if let Some((index, transaction)) = current_revision.children.last() {
83− let success = transaction.apply(state);
86− panic!("Failed to apply redo!");
89 self.cursor = *index;
80 self.cursor = *index;
82+ return Some(transaction.clone());
@@ -120,17 +114,27 @@ mod test {
120 assert_eq!("hello 世界!", state.doc());
114 assert_eq!("hello 世界!", state.doc());
117+ fn undo(history: &mut History, state: &mut State) {
118+ if let Some(transaction) = history.undo() {
119+ transaction.apply(state);
122+ fn redo(history: &mut History, state: &mut State) {
123+ if let Some(transaction) = history.redo() {
124+ transaction.apply(state);
124− history.undo(&mut state);
128+ undo(&mut history, &mut state);
125 assert_eq!("hello world!", state.doc());
129 assert_eq!("hello world!", state.doc());
126− history.redo(&mut state);
130+ redo(&mut history, &mut state);
127 assert_eq!("hello 世界!", state.doc());
131 assert_eq!("hello 世界!", state.doc());
128− history.undo(&mut state);
132+ undo(&mut history, &mut state);
129− history.undo(&mut state);
133+ undo(&mut history, &mut state);
130 assert_eq!("hello", state.doc());
134 assert_eq!("hello", state.doc());
132 // undo at root is a no-op
136 // undo at root is a no-op
133− history.undo(&mut state);
137+ undo(&mut history, &mut state);
134 assert_eq!("hello", state.doc());
138 assert_eq!("hello", state.doc());