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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use super::{
    CellTrait::Cell,
    Datacell::{CellType, DataCell},
};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Default, Debug, Clone)]
pub struct ElementCell {
    #[serde(rename = "tag")]
    pub name: String,
    #[serde(rename = "attributes")]
    pub props: Vec<Prop>,
    pub children: Vec<DataCell>,
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct Prop {
    pub key: String,
    pub value: String,
}

impl ElementCell {
    pub fn push_attribute(&mut self, line: &str) {
        if let Some(prop_line) = line.split_once(" ") {
            if self
                .props
                .iter()
                .any(|x| x.key == prop_line.0 && x.value == prop_line.1)
            {
                return;
            }
            self.props.push(Prop {
                key: prop_line.0.to_string(),
                value: prop_line.1.to_string(),
            })
        }
    }

    pub fn add_attribute(tree: &mut DataCell, cell_id: usize, prop_line: &str) {
        if tree.id == cell_id {
            match &mut tree.cell_type {
                CellType::Element(ref mut el) => el.push_attribute(prop_line),
                _ => (),
            }
            return;
        }

        match &mut tree.cell_type {
            CellType::Element(ref mut el) => el
                .children
                .iter_mut()
                .for_each(|x| Self::add_attribute(x, cell_id, prop_line)),
            CellType::Root(ref mut el) => el
                .children
                .iter_mut()
                .for_each(|x| Self::add_attribute(x, cell_id, prop_line)),
            _ => (),
        }
    }

    pub fn add_existing_cell(add_to: &mut DataCell, parent_id: usize, cell: &DataCell) {
        if add_to.id == parent_id {
            match add_to.cell_type {
                CellType::Element(ref mut el) => el.children.push(DataCell {
                    parent_id,
                    ..cell.to_owned()
                }),
                CellType::Root(ref mut el) => el.children.push(DataCell {
                    parent_id,
                    ..cell.to_owned()
                }),
                _ => (),
            }
            return;
        }

        match &mut add_to.cell_type {
            CellType::Element(ref mut el) => el
                .children
                .iter_mut()
                .for_each(|x| Self::add_existing_cell(x, parent_id, cell)),

            CellType::Root(ref mut el) => el
                .children
                .iter_mut()
                .for_each(|x| Self::add_existing_cell(x, parent_id, cell)),
            _ => (),
        }
    }

    pub fn move_cell(
        tree: &mut DataCell,
        (cell_to_move_parent_id, cell_to_move_id): (usize, usize),
        move_to: usize,
    ) {
        if tree.id == cell_to_move_parent_id {
            if let CellType::Element(ref mut el) = tree.cell_type {
                // Get a mutable reference to `el`
                let (child_to_move, remaining_children): (Vec<_>, Vec<_>) = el
                    .children // Split children into two vecs
                    .iter()
                    .cloned() // Clone to avoid ownership issues
                    .partition(|child| child.id == cell_to_move_id);

                el.children = remaining_children;
                if let Some(child_to_move) = child_to_move.first() {
                    // Use if let to unwrap the child
                    Self::add_existing_cell(tree, move_to, child_to_move);
                }
            }

            return;
        }

        match &mut tree.cell_type {
            CellType::Element(ref mut el) => el.children.iter_mut().for_each(|x| {
                Self::move_cell(x, (cell_to_move_parent_id, cell_to_move_id), move_to)
            }),
            CellType::Root(ref mut el) => el.children.iter_mut().for_each(|x| {
                Self::move_cell(x, (cell_to_move_parent_id, cell_to_move_id), move_to)
            }),
            _ => (),
        }
    }

    pub fn move_children(tree: &mut DataCell, cell: usize, move_to: usize) {
        if tree.id == cell {
            let mut cloned_tree = tree.clone();
            if let CellType::Element(ref mut el) = &mut tree.cell_type {
                let move_to_cell = DataCell::get_cell_by_id(&mut cloned_tree, move_to);
                //let mut root = tree.to_owned();
                if let Some(move_to_cell) = move_to_cell {
                    if let CellType::Element(ref mut move_to_el) = move_to_cell.cell_type {
                        move_to_el.children = el
                            .children
                            .iter()
                            .cloned()
                            .partition(|child| child.id != move_to)
                            .0;
                        el.children.clear();
                        el.children.push(move_to_cell.to_owned())
                    }
                }
            }

            return;
        }

        match &mut tree.cell_type {
            CellType::Element(ref mut el) => el
                .children
                .iter_mut()
                .for_each(|x| Self::move_children(x, cell, move_to)),
            CellType::Root(ref mut el) => el
                .children
                .iter_mut()
                .for_each(|x| Self::move_children(x, cell, move_to)),
            _ => (),
        }
    }
}

impl Cell<&str> for ElementCell {
    fn init_cell(id: usize, parent_id: usize, tag_name: &str) -> DataCell {
        DataCell {
            id,
            parent_id,
            cell_type: CellType::Element(ElementCell {
                name: tag_name.to_string(),
                ..Default::default()
            }),
        }
    }

    fn push_cell(parent: &mut DataCell, id: usize, tag_name: &str) {
        match parent.cell_type {
            CellType::Element(ref mut el) => {
                el.children.push(Self::init_cell(id, parent.id, tag_name))
            }
            CellType::Root(ref mut el) => {
                el.children.push(Self::init_cell(id, parent.id, tag_name))
            }
            _ => (),
        }
    }

    fn add_cell(add_to: &mut DataCell, parent_id: usize, id: usize, tag_name: &str) {
        if add_to.id == parent_id {
            Self::push_cell(add_to, id, tag_name);
            return;
        }

        match &mut add_to.cell_type {
            CellType::Element(ref mut el) => el
                .children
                .iter_mut()
                .for_each(|x| Self::add_cell(x, parent_id, id, tag_name)),

            CellType::Root(ref mut el) => el
                .children
                .iter_mut()
                .for_each(|x| Self::add_cell(x, parent_id, id, tag_name)),
            _ => (),
        }
    }
}