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
/// If an Undo's Undo is itself an Undo,
/// its undo() must produce the initial Undo. :P
pub trait Undo {
    type Undo;

    fn undo(&self) -> Self::Undo;
}

pub struct History<T: Undo> {
    events: Vec<T>,
    len: usize
}

impl<T: Undo> History<T> {
    pub fn new() -> Self {
        Self {
            events: Vec::new(),
            len: 0
        }
    }

    pub fn len(&self) -> usize {
        self.len
    }

    pub fn undone(&self) -> usize {
        self.events.len().saturating_sub(self.len)
    }

    pub fn record(&mut self, event: T) {
        self.events.truncate(self.len);
        self.events.push(event);
        self.len = self.events.len();
    }

    pub fn undo(&mut self) -> Result<T::Undo, ()> {
        if self.len > 0 {
            self.len -= 1;
            let undo = self.events[self.len].undo();
            Ok(undo)
        } else {
            Err(())
        }
    }
}

impl<T: Undo + Clone> History<T> {
    pub fn redo(&mut self) -> Result<T, ()> {
        if self.len < self.events.len() {
            let redo = self.events[self.len].clone();
            self.len += 1;
            Ok(redo)
        } else {
            Err(())
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Meaningless test event.
    #[derive(PartialEq, Clone)]
    enum Event {
        Forward,
        Backward
    }

    impl Undo for Event {
        type Undo = Event;

        fn undo(&self) -> Self::Undo {
            match *self {
                Event::Forward  => Event::Backward,
                Event::Backward => Event::Forward
            }
        }
    }

    #[test]
    fn main() {
        let mut hist = History::new();

        assert!(hist.undo().is_err());
        assert!(hist.redo().is_err());

        assert_eq!(0, hist.len());
        assert_eq!(0, hist.undone());

        hist.record(Event::Forward);

        assert_eq!(1, hist.len());
        assert_eq!(0, hist.undone());

        hist.record(Event::Backward);

        assert_eq!(2, hist.len());
        assert_eq!(0, hist.undone());

        assert!(match hist.undo() {
            Result::Ok(Event::Forward) => true,
            _                          => false
        });
        assert!(match hist.undo() {
            Result::Ok(Event::Backward) => true,
            _                           => false
        });
        assert!(hist.undo().is_err());

        assert!(match hist.redo() {
            Result::Ok(Event::Forward) => true,
            _                          => false
        });
        assert!(match hist.redo() {
            Result::Ok(Event::Backward) => true,
            _                           => false
        });
        assert!(hist.redo().is_err());
    }
}