redo 0.7.0

An undo/redo library with static dispatch and manual command merging.
Documentation
redo-0.7.0 has been yanked.

Redo

Travis Appveyor Crates.io Docs

An undo/redo library with static dispatch and manual command merging. It uses the Command Pattern where the user modifies a receiver by applying Commands on it.

The library has currently two data structures that can be used to modify the receiver:

  • A simple Stack that pushes and pops commands to modify the receiver.
  • A Record that can roll the state of the receiver forwards and backwards.

Examples

use redo::{Command, Stack};

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

impl Command<String> for Push {
    type Err = &'static str;

    fn redo(&mut self, s: &mut String) -> Result<(), &'static str> {
        s.push(self.0);
        Ok(())
    }

    fn undo(&mut self, s: &mut String) -> Result<(), &'static str> {
        self.0 = s.pop().ok_or("`String` is unexpectedly empty")?;
        Ok(())
    }
}

fn foo() -> Result<(), (Push, &'static str)> {
    let mut stack = Stack::default();

    stack.push(Push('a'))?;
    stack.push(Push('b'))?;
    stack.push(Push('c'))?;

    assert_eq!(stack.as_receiver(), "abc");

    let c = stack.pop().unwrap()?;
    let b = stack.pop().unwrap()?;
    let a = stack.pop().unwrap()?;

    assert_eq!(stack.into_receiver(), "");

    stack.push(a)?;
    stack.push(b)?;
    stack.push(c)?;

    assert_eq!(stack.into_receiver(), "abc");

    Ok(())
}