undo

Low-level undo-redo functionality.

Travis Crates.io Docs

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.

Features

Cargo Feature Flags

Examples

```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() -> undo::Result { let mut target = String::new(); let mut history = History::new(); history.apply(&mut target, Add('a'))?; history.apply(&mut target, Add('b'))?; history.apply(&mut target, Add('c'))?; asserteq!(target, "abc"); history.undo(&mut target)?; history.undo(&mut target)?; history.undo(&mut target)?; asserteq!(target, ""); history.redo(&mut target)?; history.redo(&mut target)?; history.redo(&mut target)?; assert_eq!(target, "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.