undo 0.26.0

An undo-redo library with dynamic dispatch and automatic command merging.
Documentation

undo

Travis Appveyor Crates.io Docs

An undo-redo library with dynamic dispatch and automatic command merging.

It uses the command pattern where the user modifies the receiver by applying commands on it. Since each command knows how to undo and redo the changes it applies to the receiver, the state of the receiver can be rolled forwards or backwards by calling undo or redo in the correct order.

The Record and History provides functionality to store and keep track of the applied commands, and makes it easy to undo and redo changes. The Record provides a stack based undo-redo functionality, while the History provides a tree based undo-redo functionality where you can jump between different branches.

Commands can be merged using the merge! macro or the id method. When two commands are merged, undoing and redoing them are done in a single step.

Examples

Add this to Cargo.toml:

[dependencies]
undo = "0.25"

And this to main.rs:

extern crate undo;

use undo::{Command, Record};

#[derive(Debug)]
struct Add(char);

impl Command<String> for Add {
   fn apply(&mut self, s: &mut String) -> Result<(), Box<dyn Error + Send + Sync>> {
       s.push(self.0);
       Ok(())
   }

   fn undo(&mut self, s: &mut String) -> Result<(), Box<dyn Error + Send + Sync>> {
       self.0 = s.pop().ok_or("`s` is empty")?;
       Ok(())
   }
}

fn main() -> Result<(), Box<dyn Error>> {
   let mut record = Record::default();
   record.apply(Add('a'))?;
   record.apply(Add('b'))?;
   record.apply(Add('c'))?;
   assert_eq!(record.as_receiver(), "abc");

   record.undo().unwrap()?;
   record.undo().unwrap()?;
   record.undo().unwrap()?;
   assert_eq!(record.as_receiver(), "");

   record.redo().unwrap()?;
   record.redo().unwrap()?;
   record.redo().unwrap()?;
   assert_eq!(record.as_receiver(), "abc");
   Ok(())
}

License

Licensed under either of

at your option.

Contribution

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.