elm_parser/datacell/
BlockCell.rs

1use super::{
2    BlockChildType::BlockChildType,
3    CellTrait::Cell,
4    Datacell::{CellType, DataCell},
5};
6use serde::{Deserialize, Serialize};
7
8#[inline]
9fn is_false(v: &bool) -> bool {
10    !(*v)
11}
12#[derive(Serialize, Deserialize, Debug, Clone, Default)]
13pub struct BlockCell {
14    pub children: Vec<BlockChildType>,
15    #[serde(skip_serializing_if = "is_false")]
16    #[serde(default)]
17    pub has_counter_commands: bool,
18    #[serde(skip_serializing_if = "is_false")]
19    #[serde(default)]
20    pub has_handle_insert: bool,
21}
22
23impl BlockCell {
24    pub fn new() -> BlockCell {
25        BlockCell {
26            children: Vec::new(),
27            ..Default::default()
28        }
29    }
30
31    fn insert_cell(parent: &mut DataCell, id: usize, block: &BlockCell) {
32        match parent.cell_type {
33            CellType::Element(ref mut el) => {
34                el.children.insert(0, Self::init_cell(id, parent.id, block))
35            }
36            _ => (),
37        }
38    }
39
40    pub fn add_cell_at_first(
41        add_to: &mut DataCell,
42        parent_id: usize,
43        id: usize,
44        block: &BlockCell,
45    ) {
46        if add_to.id == parent_id {
47            Self::insert_cell(add_to, id, block);
48            return;
49        }
50
51        match &mut add_to.cell_type {
52            CellType::Element(ref mut el) => el
53                .children
54                .iter_mut()
55                .for_each(|x| Self::add_cell_at_first(x, parent_id, id, block)),
56            CellType::Root(ref mut el) => el
57                .children
58                .iter_mut()
59                .for_each(|x| Self::add_cell_at_first(x, parent_id, id, block)),
60            _ => (),
61        }
62    }
63}
64
65impl Cell<&BlockCell> for BlockCell {
66    fn init_cell(id: usize, parent_id: usize, block: &BlockCell) -> DataCell {
67        DataCell {
68            id,
69            parent_id,
70            cell_type: CellType::Block(block.to_owned()),
71        }
72    }
73
74    fn push_cell(parent: &mut DataCell, id: usize, block: &BlockCell) {
75        match parent.cell_type {
76            CellType::Element(ref mut el) => {
77                el.children.push(Self::init_cell(id, parent.id, block))
78            }
79            _ => (),
80        }
81    }
82
83    fn add_cell(add_to: &mut DataCell, parent_id: usize, id: usize, block: &BlockCell) {
84        if add_to.id == parent_id {
85            Self::push_cell(add_to, id, block);
86            return;
87        }
88
89        match &mut add_to.cell_type {
90            CellType::Element(ref mut el) => el
91                .children
92                .iter_mut()
93                .for_each(|x| Self::add_cell(x, parent_id, id, block)),
94            CellType::Root(ref mut el) => el
95                .children
96                .iter_mut()
97                .for_each(|x| Self::add_cell(x, parent_id, id, block)),
98            _ => (),
99        }
100    }
101}