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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use crate::{Command, Merge};
#[cfg(feature = "display")]
use std::fmt;
use std::{
iter::{FromIterator, IntoIterator},
vec::IntoIter,
};
#[macro_export]
macro_rules! merge {
($($commands:expr),*) => {{
let mut merged = $crate::Merged::default();
$(merged.push($commands);)*
merged
}};
}
pub struct Merged<R> {
commands: Vec<Box<dyn Command<R>>>,
#[cfg(feature = "display")]
text: Option<String>,
}
impl<R> Merged<R> {
#[inline]
pub fn new(cmd1: impl Command<R> + 'static, cmd2: impl Command<R> + 'static) -> Merged<R> {
Merged {
commands: vec![Box::new(cmd1), Box::new(cmd2)],
#[cfg(feature = "display")]
text: None,
}
}
#[inline]
pub fn push(&mut self, command: impl Command<R> + 'static) {
self.commands.push(Box::new(command));
}
#[inline]
pub fn join(mut self, command: impl Command<R> + 'static) -> Merged<R> {
self.push(command);
self
}
#[inline]
pub fn len(&self) -> usize {
self.commands.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.commands.is_empty()
}
#[inline]
#[cfg(feature = "display")]
pub fn set_text(&mut self, text: impl Into<String>) {
self.text = Some(text.into());
}
}
impl<R> Command<R> for Merged<R> {
#[inline]
fn apply(&mut self, receiver: &mut R) -> crate::Result {
for command in &mut self.commands {
command.apply(receiver)?;
}
Ok(())
}
#[inline]
fn undo(&mut self, receiver: &mut R) -> crate::Result {
for command in self.commands.iter_mut().rev() {
command.undo(receiver)?;
}
Ok(())
}
#[inline]
fn redo(&mut self, receiver: &mut R) -> crate::Result {
for command in &mut self.commands {
command.redo(receiver)?;
}
Ok(())
}
#[inline]
fn merge(&self) -> Merge {
self.commands.first().map_or(Merge::Yes, Command::merge)
}
#[inline]
fn is_dead(&self) -> bool {
self.commands.iter().any(Command::is_dead)
}
}
impl<R> Default for Merged<R> {
#[inline]
fn default() -> Self {
Merged {
commands: Vec::default(),
#[cfg(feature = "display")]
text: None,
}
}
}
impl<R, C: Command<R> + 'static> FromIterator<C> for Merged<R> {
#[inline]
fn from_iter<T: IntoIterator<Item = C>>(commands: T) -> Self {
Merged {
commands: commands.into_iter().map(|c| Box::new(c) as _).collect(),
#[cfg(feature = "display")]
text: None,
}
}
}
impl<R> IntoIterator for Merged<R> {
type Item = Box<dyn Command<R>>;
type IntoIter = IntoIter<Self::Item>;
#[inline]
fn into_iter(self) -> <Self as IntoIterator>::IntoIter {
self.commands.into_iter()
}
}
impl<R, C: Command<R> + 'static> Extend<C> for Merged<R> {
#[inline]
fn extend<T: IntoIterator<Item = C>>(&mut self, iter: T) {
self.commands
.extend(iter.into_iter().map(|c| Box::new(c) as _));
}
}
#[cfg(feature = "display")]
impl<R> fmt::Debug for Merged<R> {
#[inline]
#[cfg(not(feature = "display"))]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Merged")
.field("commands", &self.commands)
.finish()
}
#[inline]
#[cfg(feature = "display")]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Merged")
.field("commands", &self.commands)
.field("text", &self.text)
.finish()
}
}
#[cfg(feature = "display")]
impl<R> fmt::Display for Merged<R> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match &self.text {
Some(text) => f.write_str(text),
None => {
if let Some((first, commands)) = self.commands.split_first() {
(first as &dyn fmt::Display).fmt(f)?;
for command in commands {
write!(f, "\n\n{}", command)?;
}
}
Ok(())
}
}
}
}