diffus/edit/
mod.rs

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
pub mod collection;
pub mod enm;
pub mod map;
pub mod set;
pub mod string;

use crate::Diffable;

#[cfg_attr(feature = "serialize-impl", derive(serde::Serialize))]
#[derive(Debug, PartialEq, Eq)]
pub enum Edit<'a, T: Diffable<'a> + ?Sized> {
    Copy(&'a T),
    Change(T::Diff),
}

impl<'a, T: Diffable<'a> + ?Sized> Edit<'a, T> {
    pub fn is_copy(&self) -> bool {
        if let Self::Copy(_) = self {
            true
        } else {
            false
        }
    }

    pub fn copy(&self) -> Option<&'a T> {
        if let Self::Copy(value) = self {
            Some(value)
        } else {
            None
        }
    }

    pub fn is_change(&self) -> bool {
        if let Self::Change(_) = self {
            true
        } else {
            false
        }
    }

    pub fn change(&self) -> Option<&T::Diff> {
        if let Self::Change(value_diff) = self {
            Some(value_diff)
        } else {
            None
        }
    }
}

impl<'a, Diff, T: Diffable<'a, Diff = Diff> + 'a> Into<map::Edit<'a, T>> for Edit<'a, T> {
    fn into(self) -> map::Edit<'a, T> {
        match self {
            Self::Copy(value) => map::Edit::Copy(value),
            Self::Change(diff) => map::Edit::Change(diff),
        }
    }
}