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
use crate::Action;
use std::fmt::{self, Debug, Formatter};
use std::mem;

/// Action made from a function.
///
/// # Examples
/// ```
/// # include!("doctest.rs");
/// # fn main() {
/// # use undo::{Any, Record, FromFn};
/// let mut target = String::new();
/// let mut record = Record::new();
///
/// let a: fn(&mut String) = |s| s.push('a');
/// let b: fn(&mut String) = |s| s.push('b');
/// record.apply(&mut target, FromFn::new(a));
/// record.apply(&mut target, FromFn::new(b));
/// assert_eq!(target, "ab");
///
/// record.undo(&mut target);
/// record.undo(&mut target);
/// assert_eq!(target, "");
///
/// record.redo(&mut target);
/// record.redo(&mut target);
/// assert_eq!(target, "ab");
/// # }
/// ```
#[derive(Clone)]
pub struct FromFn<F, T> {
    f: F,
    target: Option<T>,
}

impl<F, T> FromFn<F, T> {
    /// Creates a new `FromFn` from `f`.
    pub const fn new(f: F) -> Self {
        FromFn { f, target: None }
    }
}

impl<F, T> Action for FromFn<F, T>
where
    F: FnMut(&mut T),
    T: Clone,
{
    type Target = T;
    type Output = ();

    fn apply(&mut self, target: &mut Self::Target) -> Self::Output {
        self.target = Some(target.clone());
        (self.f)(target)
    }

    fn undo(&mut self, target: &mut Self::Target) -> Self::Output {
        if let Some(old) = self.target.as_mut() {
            mem::swap(old, target);
        }
    }

    fn redo(&mut self, target: &mut Self::Target) -> Self::Output {
        if let Some(new) = self.target.as_mut() {
            mem::swap(new, target);
        }
    }
}

impl<F, T> Debug for FromFn<F, T> {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        f.debug_struct("FromFn").finish_non_exhaustive()
    }
}

/// Action made from a fallible function.
///
/// Same as [`FromFn`] but for functions that outputs [`Result`].
#[derive(Clone, Debug)]
pub struct TryFromFn<F, T> {
    f: F,
    target: Option<T>,
}

impl<F, T> TryFromFn<F, T> {
    /// Creates a new `TryFromFn` from `f`.
    pub const fn new(f: F) -> Self {
        TryFromFn { f, target: None }
    }
}

impl<F, T, E> Action for TryFromFn<F, T>
where
    F: FnMut(&mut T) -> Result<(), E>,
    T: Clone,
{
    type Target = T;
    type Output = Result<(), E>;

    fn apply(&mut self, target: &mut Self::Target) -> Self::Output {
        self.target = Some(target.clone());
        (self.f)(target)
    }

    fn undo(&mut self, target: &mut Self::Target) -> Self::Output {
        if let Some(old) = self.target.as_mut() {
            mem::swap(old, target);
        }
        Ok(())
    }

    fn redo(&mut self, target: &mut Self::Target) -> Self::Output {
        if let Some(new) = self.target.as_mut() {
            mem::swap(new, target);
        }
        Ok(())
    }
}