elm_parser/datacell/
BlockChildType.rs

1use super::{
2    BlockCell::BlockCell,
3    Datacell::{CellType, DataCell},
4};
5use serde::{Deserialize, Serialize};
6
7#[derive(Serialize, Deserialize, Debug, Clone)]
8#[serde(tag = "type")]
9pub enum BlockChildType {
10    #[serde(rename = "~text~")]
11    Text(TextCell),
12    #[serde(rename = "~delimited~")]
13    Delimited(DelimitedCell),
14}
15
16#[derive(Serialize, Deserialize, Debug, Clone, Default)]
17pub struct TextCell {
18    pub content: String,
19    pub wrapped_with: Option<String>,
20}
21
22#[derive(Serialize, Deserialize, Debug, Clone, Default)]
23pub struct DelimitedCell {
24    pub open_delimeter: String,
25    pub close_delimeter: String,
26    pub terminal: String,
27    pub display_type: DelimitedDisplayType,
28    pub wrapped_with: Option<String>,
29}
30
31#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
32pub enum DelimitedDisplayType {
33    #[default]
34    Default,
35    INLINE,
36    BLOCK,
37}
38
39pub trait BlockChild<T> {
40    fn push_cell(parent: &mut BlockCell, s: T);
41    fn insert_block_child(parent: &mut DataCell, s: T);
42    fn add_block_at_first(
43        add_to: &mut DataCell,
44        parent_id: usize,
45        block: &T,
46        block_options: Option<&BlockCell>,
47    );
48    fn insert_text_to_first_block_child_text(add_to: &mut DataCell, parent_id: usize, text: &str);
49}
50
51impl BlockChild<BlockChildType> for BlockChildType {
52    fn push_cell(parent: &mut BlockCell, block: BlockChildType) {
53        parent.children.push(block)
54    }
55
56    fn insert_block_child(parent: &mut DataCell, block_child: BlockChildType) {
57        if let CellType::Element(el) = &mut parent.cell_type {
58            if let CellType::Block(block) = &mut el.children.first_mut().unwrap().cell_type {
59                block.children.insert(0, block_child);
60            }
61        }
62    }
63
64    fn add_block_at_first(
65        add_to: &mut DataCell,
66        block_id: usize,
67        block_child: &BlockChildType,
68        block_options: Option<&BlockCell>,
69    ) {
70        if add_to.id == block_id {
71            if let CellType::Element(el) = &mut add_to.cell_type {
72                if let CellType::Block(block) = &mut el.children.first_mut().unwrap().cell_type {
73                    block.children.insert(0, block_child.to_owned());
74                    if let Some(options) = block_options {
75                        block.has_counter_commands = options.has_counter_commands;
76                        block.has_handle_insert = options.has_handle_insert;
77                    }
78                }
79            }
80            return;
81        }
82
83        match &mut add_to.cell_type {
84            CellType::Element(ref mut el) => el
85                .children
86                .iter_mut()
87                .for_each(|x| Self::add_block_at_first(x, block_id, block_child, block_options)),
88            CellType::Root(ref mut el) => el
89                .children
90                .iter_mut()
91                .for_each(|x| Self::add_block_at_first(x, block_id, block_child, block_options)),
92            _ => (),
93        }
94    }
95
96    fn insert_text_to_first_block_child_text(
97        add_to: &mut DataCell,
98        block_id: usize,
99        text_to_insert: &str,
100    ) {
101        if add_to.id == block_id {
102            if let CellType::Element(el) = &mut add_to.cell_type {
103                if let CellType::Block(block) = &mut el.children.first_mut().unwrap().cell_type {
104                    if let BlockChildType::Text(text) = &mut block.children.first_mut().unwrap() {
105                        text.content = text_to_insert.to_owned() + &text.content;
106                    }
107                }
108            }
109        }
110
111        match &mut add_to.cell_type {
112            CellType::Element(ref mut el) => el.children.iter_mut().for_each(|x| {
113                Self::insert_text_to_first_block_child_text(x, block_id, text_to_insert)
114            }),
115            CellType::Root(ref mut el) => el.children.iter_mut().for_each(|x| {
116                Self::insert_text_to_first_block_child_text(x, block_id, text_to_insert)
117            }),
118            _ => (),
119        }
120    }
121}