elm_parser/datacell/
Datacell.rs

1use super::{
2    BlockCell::BlockCell,
3    ElementCell::{ElementCell, Prop},
4};
5use serde::{Deserialize, Serialize};
6
7#[derive(Serialize, Deserialize, Debug, Default, Clone)]
8#[serde(tag = "type")]
9pub enum CellType {
10    #[default]
11    Default,
12    Root(Root),
13    #[serde(rename = "~element~")]
14    Element(ElementCell),
15    #[serde(rename = "~block~")]
16    Block(BlockCell),
17}
18
19#[derive(Serialize, Deserialize, Default, Debug, Clone)]
20pub struct Root {
21    #[serde(rename = "attributes")]
22    pub children: Vec<DataCell>,
23}
24
25#[derive(Serialize, Deserialize, Debug, Default, Clone)]
26pub struct DataCell {
27    pub id: usize,
28    pub parent_id: usize,
29    #[serde(flatten)]
30    pub cell_type: CellType,
31}
32
33pub trait FlatElement {
34    fn id(&self) -> usize;
35    fn parent_id(&self) -> usize;
36    fn children(&self) -> Option<&Vec<DataCell>>;
37    fn props(&self) -> Option<&Vec<Prop>>;
38    fn name(&self) -> Option<&String>;
39
40    fn update_children(&mut self, new: Vec<DataCell>) -> Result<(), &str>;
41}
42
43impl FlatElement for DataCell {
44    fn id(&self) -> usize {
45        self.id
46    }
47
48    fn parent_id(&self) -> usize {
49        self.parent_id
50    }
51
52    fn children(&self) -> Option<&Vec<DataCell>> {
53        match &self.cell_type {
54            CellType::Element(el) => Some(&el.children),
55            CellType::Root(el) => Some(&el.children),
56            _ => None
57        }
58    }
59
60    fn update_children(&mut self, new: Vec<DataCell>) -> Result<(), &str> {
61         match &mut self.cell_type {
62            CellType::Element(el) => {
63                el.children = new;
64                Ok(())
65            },
66            CellType::Root(el) => {
67                el.children = new;
68                Ok(())
69            },
70            _ => Err("Cannot update children of this type of cell")
71        }
72    }
73
74    fn name(&self) -> Option<&String> {
75        if let CellType::Element(el) = &self.cell_type{
76            return Some(&el.name)
77        }
78        None
79    }
80
81    fn props(&self) -> Option<&Vec<Prop>> {
82         if let CellType::Element(el) = &self.cell_type{
83            return Some(&el.props)
84        }
85        None
86    }
87}
88
89
90impl DataCell {
91    pub fn get_cell_by_id(cell: &mut DataCell, id: usize) -> Option<&mut DataCell> {
92        if cell.id == id {
93            return Some(cell);
94        };
95
96        match &mut cell.cell_type {
97            CellType::Element(ref mut el) => el.children.iter_mut().find_map(|x| {
98                if let Some(child) = Self::get_cell_by_id(x, id) {
99                    Some(child)
100                } else {
101                    None
102                }
103            }),
104
105            CellType::Root(ref mut el) => {
106                let res = el.children.iter_mut().find_map(|x| {
107                    if let Some(child) = Self::get_cell_by_id(x, id) {
108                        Some(child)
109                    } else {
110                        None
111                    }
112                });
113                res
114            }
115            _ => None,
116        }
117    }
118
119    pub fn get_cell_by_id_immut(cell: &DataCell, id: usize) -> Option<&DataCell> {
120        if cell.id == id {
121            return Some(cell);
122        };
123
124        match &cell.cell_type {
125            CellType::Element(el) => el.children.iter().find_map(|x| {
126                if let Some(child) = Self::get_cell_by_id_immut(x, id) {
127                    Some(child)
128                } else {
129                    None
130                }
131            }),
132
133            CellType::Root(el) => {
134                let res = el.children.iter().find_map(|x| {
135                    if let Some(child) = Self::get_cell_by_id_immut(x, id) {
136                        Some(child)
137                    } else {
138                        None
139                    }
140                });
141                res
142            }
143            _ => None,
144        }
145    }
146
147    pub fn into_el(&self) -> Option<&ElementCell> {
148        if let CellType::Element(el) = &self.cell_type {
149            return Some(el);
150        }
151        None
152    }
153
154    pub fn get_prev_sibling(&self, parent: &DataCell) -> Option<DataCell> {
155        if let CellType::Element(parent) = &parent.cell_type {
156            let index = parent.children.iter().position(|x| x.id == self.id)?;
157            if index > 0 {
158                return parent.children.iter().nth(index - 1).cloned();
159            }
160        }
161
162        None
163    }
164
165    pub fn get_next_sibling(&self, parent: &DataCell) -> Option<DataCell> {
166        if let CellType::Element(parent) = &parent.cell_type {
167            let index = parent.children.iter().position(|x| x.id == self.id)?;
168            return parent.children.iter().nth(index + 1).cloned();
169        }
170
171        None
172    }
173}