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
mod json;
use std::fmt;
pub trait Changes {
type Replace;
type Patch;
fn diff(&self, new: &Self) -> Result<Diff<Self::Replace, Self::Patch>, DiffError>;
}
#[derive(Debug)]
pub enum DiffError {
DiffValue,
}
impl std::fmt::Display for DiffError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "JSON value types are different")
}
}
impl std::error::Error for DiffError {}
#[derive(Debug)]
pub enum Diff<R, P> {
None,
Delete,
Patch(P),
Replace(R),
Merge(R),
}
impl<R, P> Diff<R, P> {
pub fn is_none(&self) -> bool {
matches!(self, Diff::None)
}
pub fn is_delete(&self) -> bool {
matches!(self, Diff::Delete)
}
pub fn is_replace(&self) -> bool {
matches!(self, Diff::Replace(_))
}
pub fn is_patch(&self) -> bool {
matches!(self, Diff::Patch(_))
}
pub fn is_merge(&self) -> bool {
matches!(self, Diff::Merge(_))
}
pub fn as_replace_ref(&self) -> &R {
match self {
Diff::Replace(ref val) => val,
_ => panic!("no change value"),
}
}
pub fn as_patch_ref(&self) -> &P {
match self {
Diff::Patch(ref val) => val,
_ => panic!("no change value"),
}
}
}