use undoredo::UndoRedo;
#[derive(Debug, Clone, PartialEq)]
enum Command {
PushChar(char),
}
fn command(s: &mut String, undoredo: &mut UndoRedo<(), Command>, cmd: Command) {
match &cmd {
Command::PushChar(c) => s.push(*c),
}
undoredo.command(cmd);
}
fn undo(s: &mut String, undoredo: &mut UndoRedo<(), Command>) {
if let Some(cmd) = undoredo.undo(s) {
match &cmd {
Command::PushChar(_) => {
s.pop();
}
}
}
}
fn redo(s: &mut String, undoredo: &mut UndoRedo<(), Command>) {
if let Some(cmd) = undoredo.redo(s) {
match &cmd {
Command::PushChar(c) => s.push(*c),
}
}
}
fn main() {
let mut s = String::new();
let mut undoredo: UndoRedo<(), Command> = UndoRedo::new();
command(&mut s, &mut undoredo, Command::PushChar('a'));
command(&mut s, &mut undoredo, Command::PushChar('b'));
assert_eq!(s, "ab");
undo(&mut s, &mut undoredo);
assert_eq!(s, "a");
undo(&mut s, &mut undoredo);
assert_eq!(s, "");
redo(&mut s, &mut undoredo);
assert_eq!(s, "a");
redo(&mut s, &mut undoredo);
assert_eq!(s, "ab");
}
#[test]
fn test() {
main();
}