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
use crate::diff::hashmap_diff::HashMapDiff;
use crate::svg_data::{print_svg_element, Tag};
use flange_flat_tree::Subtree;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct RemoveDiff {
    id: String,
    parent_id: String,
    prev_child_id: Option<String>,
    next_child_id: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct AddDiff {
    svg: String,
    id: String,
    parent_id: String,
    prev_child_id: Option<String>,
    next_child_id: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MoveDiff {
    id: String,
    new_parent_id: String,
    new_prev_child_id: Option<String>,
    new_next_child_id: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ChangedProperty {
    prop: String,
    start: String,
    end: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Property {
    prop: String,
    value: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ChangePropertiesDiff {
    id: String,
    adds: Vec<Property>,
    removes: Vec<Property>,
    changes: Vec<ChangedProperty>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ChangeTextDiff {
    id: String,
    new_text: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(tag = "action")]
pub enum DiffStep {
    // Remove a node
    #[serde(rename = "remove")]
    Remove(RemoveDiff),
    // Add
    #[serde(rename = "add")]
    Add(AddDiff),
    // Change the properties of a tag
    #[serde(rename = "change")]
    ChangeProperties(ChangePropertiesDiff),
    #[serde(rename = "change_text")]
    ChangeText(ChangeTextDiff),
    #[serde(rename = "move")]
    Move(MoveDiff),
}

impl DiffStep {
    pub fn remove<'a, ST>(svg: &'a ST) -> DiffStep
    where
        ST: Subtree<Node = (&'a Tag, &'a Option<String>)>,
    {
        DiffStep::Remove(RemoveDiff {
            id: svg.value().1.clone().unwrap(),
            parent_id: svg.parent().and_then(|s| s.value().1.clone()).unwrap(),
            prev_child_id: svg.prev_sibling().and_then(|s| s.value().1.clone()),
            next_child_id: svg.next_sibling().and_then(|s| s.value().1.clone()),
        })
    }

    pub fn add<'a, SVG>(svg: &'a SVG) -> DiffStep
    where
        SVG: Subtree<Node = (&'a Tag, &'a Option<String>)>,
    {
        DiffStep::Add(AddDiff {
            svg: print_svg_element(svg),
            id: svg.value().1.clone().unwrap(),
            parent_id: svg.parent().and_then(|s| s.value().1.clone()).unwrap(),
            prev_child_id: svg.prev_sibling().and_then(|s| s.value().1.clone()),
            next_child_id: svg.next_sibling().and_then(|s| s.value().1.clone()),
        })
    }

    pub fn change(id: String, change: HashMapDiff<String>) -> DiffStep {
        let adds = change
            .adds
            .iter()
            .map(|(prop, val)| Property {
                prop: prop.clone(),
                value: val.to_string(),
            })
            .collect();
        let removes = change
            .deletes
            .iter()
            .map(|(prop, val)| Property {
                prop: prop.clone(),
                value: val.to_string(),
            })
            .collect();
        let changes = change
            .changes
            .iter()
            .map(|(prop, (from, to))| ChangedProperty {
                prop: prop.clone(),
                start: from.to_string(),
                end: to.to_string(),
            })
            .collect();
        DiffStep::ChangeProperties(ChangePropertiesDiff {
            id,
            adds,
            removes,
            changes,
        })
    }

    pub fn text_change(id: String, new_text: String) -> DiffStep {
        DiffStep::ChangeText(ChangeTextDiff { id, new_text })
    }

    pub fn move_element<'a, ST>(svg: &'a ST) -> DiffStep
    where
        ST: Subtree<Node = (&'a Tag, &'a Option<String>)>,
    {
        DiffStep::Move(MoveDiff {
            id: svg.value().1.clone().unwrap(),
            new_parent_id: svg.parent().and_then(|s| s.value().1.clone()).unwrap(),
            new_prev_child_id: svg.prev_sibling().and_then(|s| s.value().1.clone()),
            new_next_child_id: svg.next_sibling().and_then(|s| s.value().1.clone()),
        })
    }

    pub fn is_add(&self) -> bool {
        matches!(*self, DiffStep::Add(_))
    }

    pub fn is_remove(&self) -> bool {
        matches!(*self, DiffStep::Remove(_))
    }

    pub fn is_change(&self) -> bool {
        matches!(*self, DiffStep::ChangeProperties(_))
    }

    pub fn is_text_change(&self) -> bool {
        matches!(*self, DiffStep::ChangeText(_))
    }

    pub fn is_move(&self) -> bool {
        matches!(*self, DiffStep::Move(_))
    }
}