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
#![no_std]
#![doc(html_root_url = "https://docs.rs/redo")]
#![deny(missing_docs)]
#![forbid(unsafe_code)]
extern crate alloc;
mod format;
pub mod history;
pub mod record;
#[cfg(feature = "chrono")]
use chrono::{DateTime, Utc};
use core::fmt;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
pub use self::{history::History, record::Record};
pub type Result<C> = core::result::Result<(), <C as Command>::Error>;
pub trait Command: Sized {
type Target;
type Error;
fn apply(&mut self, target: &mut Self::Target) -> Result<Self>;
fn undo(&mut self, target: &mut Self::Target) -> Result<Self>;
fn redo(&mut self, target: &mut Self::Target) -> Result<Self> {
self.apply(target)
}
fn merge(&mut self, command: Self) -> Merge<Self> {
Merge::No(command)
}
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
pub enum Signal {
Undo(bool),
Redo(bool),
Saved(bool),
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
pub enum Merge<C> {
Yes,
No(C),
Annul,
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Copy, Clone, Debug, Default, Hash, Eq, PartialEq)]
struct At {
branch: usize,
current: usize,
}
impl At {
fn new(branch: usize, current: usize) -> At {
At { branch, current }
}
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone)]
struct Slot<F> {
#[cfg_attr(feature = "serde", serde(default = "Option::default", skip))]
f: Option<F>,
}
impl<F: FnMut(Signal)> Slot<F> {
fn emit(&mut self, signal: Signal) {
if let Some(ref mut f) = self.f {
f(signal);
}
}
fn emit_if(&mut self, cond: bool, signal: Signal) {
if cond {
self.emit(signal)
}
}
}
impl<F> Default for Slot<F> {
fn default() -> Self {
Slot { f: None }
}
}
impl<F> fmt::Debug for Slot<F> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.f {
Some(_) => f.pad("Slot { .. }"),
None => f.pad("Empty"),
}
}
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
struct Entry<C> {
command: C,
#[cfg(feature = "chrono")]
timestamp: DateTime<Utc>,
}
impl<C> From<C> for Entry<C> {
fn from(command: C) -> Self {
Entry {
command,
#[cfg(feature = "chrono")]
timestamp: Utc::now(),
}
}
}
impl<C: Command> Command for Entry<C> {
type Target = C::Target;
type Error = C::Error;
fn apply(&mut self, target: &mut Self::Target) -> Result<Self> {
self.command.apply(target)
}
fn undo(&mut self, target: &mut Self::Target) -> Result<Self> {
self.command.undo(target)
}
fn redo(&mut self, target: &mut Self::Target) -> Result<Self> {
self.command.redo(target)
}
fn merge(&mut self, command: Self) -> Merge<Self> {
match self.command.merge(command.command) {
Merge::Yes => Merge::Yes,
Merge::No(command) => Merge::No(Entry::from(command)),
Merge::Annul => Merge::Annul,
}
}
}
impl<C: fmt::Display> fmt::Display for Entry<C> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
(&self.command as &dyn fmt::Display).fmt(f)
}
}