A undo-redo library.
It is an implementation of the command pattern, where all modifications are done by creating objects that applies the modifications. All objects knows how to undo the changes it applies, and by using the provided data structures it is easy to apply, undo, and redo changes made to a target.
N
most recent changes are stored.no_std
.alloc
: Enables the use of the alloc crate, enabled by default.arrayvec
: Required for the timeline module, enabled by default.chrono
: Enables time stamps and time travel.serde
: Enables serialization and deserialization.colored
: Enables colored output when visualizing the display structures.```rust use undo::{Action, History};
struct Add(char);
impl Action for Add { type Target = String; type Output = (); type Error = &'static str;
fn apply(&mut self, s: &mut String) -> undo::Result<Add> {
s.push(self.0);
Ok(())
}
fn undo(&mut self, s: &mut String) -> undo::Result<Add> {
self.0 = s.pop().ok_or("s is empty")?;
Ok(())
}
}
fn main() { let mut target = String::new(); let mut history = History::new(); history.apply(&mut target, Add('a')).unwrap(); history.apply(&mut target, Add('b')).unwrap(); history.apply(&mut target, Add('c')).unwrap(); asserteq!(target, "abc"); history.undo(&mut target).unwrap(); history.undo(&mut target).unwrap(); history.undo(&mut target).unwrap(); asserteq!(target, ""); history.redo(&mut target).unwrap(); history.redo(&mut target).unwrap(); history.redo(&mut target).unwrap(); assert_eq!(target, "abc"); } ```
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.