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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use std::cmp::PartialEq;
use std::convert::From;
use std::convert::{AsMut, AsRef};
use std::fmt::Debug;
use std::ops::{Deref, DerefMut};

use super::Rundo;

/// Value type like a memory undo/redo type.
/// Rundo will clone its origin value as a backup, so Clone must be implemented.
/// **Be careful use it for struct or other big size type**,
/// OpType is design for this scenario, or you must implment your custrom rundo type.
pub struct ValueType<T>
where
    T: Clone + PartialEq,
{
    pub(crate) value: T,
    pub(crate) origin: Option<T>,
}

/// impl Deref let ValueType<T> transparent to user access T value.
impl<T> Deref for ValueType<T>
where
    T: Clone + PartialEq,
{
    type Target = T;
    fn deref(&self) -> &T {
        &self.value
    }
}

/// when user try to get a mut refercence, Rundo know what changed in the later.
impl<T> DerefMut for ValueType<T>
where
    T: Clone + PartialEq,
{
    fn deref_mut(&mut self) -> &mut T {
        // when try to change the leaf value, ensure recorded origin value.
        if self.origin.is_none() {
            self.origin = Some(self.value.clone());
        }
        &mut self.value
    }
}

impl<T> From<T> for ValueType<T>
where
    T: Clone + PartialEq,
{
    fn from(from: T) -> Self {
        ValueType {
            value: from,
            origin: None,
        }
    }
}

#[derive(Debug)]
pub struct VtOp<T> {
    prev: T,
    curr: T,
}

impl<T> AsMut<T> for ValueType<T>
where
    T: 'static + Clone + PartialEq,
{
    fn as_mut(&mut self) -> &mut T {
        &mut self.value
    }
}

impl<T> AsRef<T> for ValueType<T>
where
    T: 'static + Clone + PartialEq,
{
    fn as_ref(&self) -> &T {
        &self.value
    }
}

pub trait Primitive {}

impl Primitive for bool {}
impl Primitive for char {}
impl Primitive for i8 {}
impl Primitive for u8 {}
impl Primitive for i16 {}
impl Primitive for u16 {}
impl Primitive for i32 {}
impl Primitive for u32 {}
impl Primitive for i64 {}
impl Primitive for u64 {}
impl Primitive for f32 {}
impl Primitive for f64 {}
impl Primitive for isize {}
impl Primitive for usize {}

impl<T> Rundo for ValueType<T>
where
    T: Clone + PartialEq + Debug + Primitive,
{
    type Op = VtOp<T>;

    fn dirty(&self) -> bool {
        match self.origin {
            Some(ref ori) => *ori != self.value,
            None => false,
        }
    }

    fn reset(&mut self) {
        self.origin = None;
    }

    fn change_op(&mut self) -> Option<Self::Op> {
        match self.origin {
            Some(ref ori) if ori != &self.value => Some(VtOp {
                prev: ori.clone(),
                curr: self.value.clone(),
            }),
            _ => None,
        }
    }

    fn back(&mut self, op: &Self::Op) {
        debug_assert_eq!(self.value, op.curr);
        self.value = op.prev.clone();
        self.reset();
    }

    fn forward(&mut self, op: &Self::Op) {
        debug_assert_eq!(op.prev, self.value);
        self.value = op.curr.clone();
        self.reset();
    }
}

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

    macro_rules! type_test {
        ($init:expr, $new:expr) => {{
            let mut leaf = ValueType::from($init.clone());
            assert!(!leaf.dirty());

            *leaf = $new.clone();
            assert_eq!(leaf.value, $new.clone());
            assert_eq!(leaf.origin, Some($init.clone()));
            assert!(leaf.dirty());

            let op = leaf.change_op().expect("should have op here");
            assert_eq!(op.prev, $init.clone());
            assert_eq!(op.curr, $new.clone());

            leaf.reset();
            assert!(!leaf.dirty());
            assert!(leaf.change_op().is_none());

            // test back forward
            *leaf = $new.clone();
            leaf.back(&op);
            assert_eq!(leaf.value, $init.clone());
            assert!(!leaf.dirty());

            assert_eq!(leaf.value, $init.clone());
            leaf.forward(&op);
            assert_eq!(leaf.value, $new.clone());
            assert!(!leaf.dirty());
        }};
    }

    #[test]
    fn i32() {
        type_test!(5, 6);
    }

    #[test]
    fn f32() {
        type_test!(3.0, 2.0);
    }

    #[test]
    fn i8() {
        type_test!(1i8, 2i8)
    }
}