elm_parser/datacell/
ElementCell.rs

1use super::{
2    CellTrait::Cell,
3    Datacell::{CellType, DataCell, FlatElement},
4};
5use serde::{Deserialize, Serialize};
6
7#[derive(Serialize, Deserialize, Default, Debug, Clone)]
8pub struct ElementCell {
9    #[serde(rename = "tag")]
10    pub name: String,
11    #[serde(rename = "attributes")]
12    pub props: Vec<Prop>,
13    pub children: Vec<DataCell>,
14}
15
16#[derive(Serialize, Deserialize, Debug, Clone, Default)]
17pub struct Prop {
18    pub key: String,
19    pub value: String,
20}
21
22impl ElementCell {
23    pub fn push_attribute(&mut self, line: &str) {
24        if let Some(prop_line) = line.split_once(" ") {
25            if self
26                .props
27                .iter()
28                .any(|x| x.key == prop_line.0 && x.value == prop_line.1)
29            {
30                return;
31            }
32            self.props.push(Prop {
33                key: prop_line.0.to_string(),
34                value: prop_line.1.to_string(),
35            })
36        }
37    }
38
39    pub fn add_attribute(tree: &mut DataCell, cell_id: usize, prop_line: &str) {
40        if tree.id == cell_id {
41            match &mut tree.cell_type {
42                CellType::Element(ref mut el) => el.push_attribute(prop_line),
43                _ => (),
44            }
45            return;
46        }
47
48        match &mut tree.cell_type {
49            CellType::Element(ref mut el) => el
50                .children
51                .iter_mut()
52                .for_each(|x| Self::add_attribute(x, cell_id, prop_line)),
53            CellType::Root(ref mut el) => el
54                .children
55                .iter_mut()
56                .for_each(|x| Self::add_attribute(x, cell_id, prop_line)),
57            _ => (),
58        }
59    }
60
61    pub fn add_existing_cell(add_to: &mut DataCell, parent_id: usize, cell: &DataCell) {
62        if add_to.id == parent_id {
63            match add_to.cell_type {
64                CellType::Element(ref mut el) => el.children.push(DataCell {
65                    parent_id,
66                    ..cell.to_owned()
67                }),
68                CellType::Root(ref mut el) => el.children.push(DataCell {
69                    parent_id,
70                    ..cell.to_owned()
71                }),
72                _ => (),
73            }
74            return;
75        }
76
77        match &mut add_to.cell_type {
78            CellType::Element(ref mut el) => el
79                .children
80                .iter_mut()
81                .for_each(|x| Self::add_existing_cell(x, parent_id, cell)),
82
83            CellType::Root(ref mut el) => el
84                .children
85                .iter_mut()
86                .for_each(|x| Self::add_existing_cell(x, parent_id, cell)),
87            _ => (),
88        }
89    }
90
91    pub fn move_cell(
92        tree: &mut DataCell,
93        (cell_to_move_parent_id, cell_to_move_id): (usize, usize),
94        move_to: usize,
95    ) {
96        if tree.id == cell_to_move_parent_id {
97            if let Some(children) = tree.children() {
98                // Get a mutable reference to `el`
99                let (child_to_move, remaining_children): (Vec<_>, Vec<_>) = children // Split children into two vecs
100                    .iter()
101                    .cloned() // Clone to avoid ownership issues
102                    .partition(|child| child.id == cell_to_move_id);
103
104                let _ = tree.update_children(remaining_children);
105                if let Some(child_to_move) = child_to_move.first() {
106                    // Use if let to unwrap the child
107                    Self::add_existing_cell(tree, move_to, child_to_move);
108                }
109            }
110
111            return;
112        }
113
114        match &mut tree.cell_type {
115            CellType::Element(ref mut el) => el.children.iter_mut().for_each(|x| {
116                Self::move_cell(x, (cell_to_move_parent_id, cell_to_move_id), move_to)
117            }),
118            CellType::Root(ref mut el) => el.children.iter_mut().for_each(|x| {
119                Self::move_cell(x, (cell_to_move_parent_id, cell_to_move_id), move_to)
120            }),
121            _ => (),
122        }
123    }
124
125    pub fn move_children(tree: &mut DataCell, cell: usize, move_to: usize) {
126        if tree.id == cell {
127            let mut cloned_tree = tree.clone();
128            if let CellType::Element(ref mut el) = &mut tree.cell_type {
129                let move_to_cell = DataCell::get_cell_by_id(&mut cloned_tree, move_to);
130                //let mut root = tree.to_owned();
131                if let Some(move_to_cell) = move_to_cell {
132                    if let CellType::Element(ref mut move_to_el) = move_to_cell.cell_type {
133                        move_to_el.children = el
134                            .children
135                            .iter()
136                            .cloned()
137                            .partition(|child| child.id != move_to)
138                            .0;
139                        el.children.clear();
140                        el.children.push(move_to_cell.to_owned())
141                    }
142                }
143            }
144
145            return;
146        }
147
148        match &mut tree.cell_type {
149            CellType::Element(ref mut el) => el
150                .children
151                .iter_mut()
152                .for_each(|x| Self::move_children(x, cell, move_to)),
153            CellType::Root(ref mut el) => el
154                .children
155                .iter_mut()
156                .for_each(|x| Self::move_children(x, cell, move_to)),
157            _ => (),
158        }
159    }
160}
161
162impl Cell<&str> for ElementCell {
163    fn init_cell(id: usize, parent_id: usize, tag_name: &str) -> DataCell {
164        DataCell {
165            id,
166            parent_id,
167            cell_type: CellType::Element(ElementCell {
168                name: tag_name.to_string(),
169                ..Default::default()
170            }),
171        }
172    }
173
174    fn push_cell(parent: &mut DataCell, id: usize, tag_name: &str) {
175        match parent.cell_type {
176            CellType::Element(ref mut el) => {
177                el.children.push(Self::init_cell(id, parent.id, tag_name))
178            }
179            CellType::Root(ref mut el) => {
180                el.children.push(Self::init_cell(id, parent.id, tag_name))
181            }
182            _ => (),
183        }
184    }
185
186    fn add_cell(add_to: &mut DataCell, parent_id: usize, id: usize, tag_name: &str) {
187        if add_to.id == parent_id {
188            Self::push_cell(add_to, id, tag_name);
189            return;
190        }
191
192        match &mut add_to.cell_type {
193            CellType::Element(ref mut el) => el
194                .children
195                .iter_mut()
196                .for_each(|x| Self::add_cell(x, parent_id, id, tag_name)),
197
198            CellType::Root(ref mut el) => el
199                .children
200                .iter_mut()
201                .for_each(|x| Self::add_cell(x, parent_id, id, tag_name)),
202            _ => (),
203        }
204    }
205}