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
119
#![forbid(unstable_features, bad_style)]
#![deny(missing_debug_implementations, unused_import_braces, unused_qualifications)]
mod group;
pub mod record;
mod stack;
use std::error;
use std::fmt::{self, Debug, Display, Formatter};
pub use group::Group;
pub use record::Record;
pub use stack::Stack;
pub trait Command<R> {
fn redo(&mut self, receiver: &mut R) -> Result<(), Box<error::Error>>;
fn undo(&mut self, receiver: &mut R) -> Result<(), Box<error::Error>>;
#[inline]
fn id(&self) -> Option<u32> {
None
}
}
impl<R> Debug for Command<R> {
#[inline]
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.debug_tuple("Command").field(&self.id()).finish()
}
}
impl<R, C: Command<R> + ?Sized> Command<R> for Box<C> {
#[inline]
fn redo(&mut self, receiver: &mut R) -> Result<(), Box<error::Error>> {
(**self).redo(receiver)
}
#[inline]
fn undo(&mut self, receiver: &mut R) -> Result<(), Box<error::Error>> {
(**self).undo(receiver)
}
#[inline]
fn id(&self) -> Option<u32> {
(**self).id()
}
}
#[derive(Debug)]
pub struct Error<R>(pub Box<Command<R>>, pub Box<error::Error>);
impl<R> Display for Error<R> {
#[inline]
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}", self.1)
}
}
impl<R: Debug> error::Error for Error<R> {
#[inline]
fn description(&self) -> &str {
self.1.description()
}
#[inline]
fn cause(&self) -> Option<&error::Error> {
self.1.cause()
}
}
struct Merger<R> {
cmd1: Box<Command<R>>,
cmd2: Box<Command<R>>,
}
impl<R> Command<R> for Merger<R> {
#[inline]
fn redo(&mut self, receiver: &mut R) -> Result<(), Box<error::Error>> {
self.cmd1.redo(receiver)?;
self.cmd2.redo(receiver)
}
#[inline]
fn undo(&mut self, receiver: &mut R) -> Result<(), Box<error::Error>> {
self.cmd2.undo(receiver)?;
self.cmd1.undo(receiver)
}
#[inline]
fn id(&self) -> Option<u32> {
self.cmd1.id()
}
}