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
use super::{
    BlockCell::BlockCell,
    Datacell::{CellType, DataCell},
};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(tag = "type")]
pub enum BlockChildType {
    #[serde(rename = "~text~")]
    Text(TextCell),
    #[serde(rename = "~delimited~")]
    Delimited(DelimitedCell),
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct TextCell {
    pub content: String,
    pub wrapped_with: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct DelimitedCell {
    pub open_delimeter: String,
    pub close_delimeter: String,
    pub terminal: String,
    pub display_type: DelimitedDisplayType,
    pub wrapped_with: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
pub enum DelimitedDisplayType {
    #[default]
    Default,
    INLINE,
    BLOCK,
}

pub trait BlockChild<T> {
    fn push_cell(parent: &mut BlockCell, s: T);
    fn insert_block_child(parent: &mut DataCell, s: T);
    fn add_block_at_first(
        add_to: &mut DataCell,
        parent_id: usize,
        block: &T,
        block_options: Option<&BlockCell>,
    );
    fn insert_text_to_first_block_child_text(add_to: &mut DataCell, parent_id: usize, text: &str);
}

impl BlockChild<BlockChildType> for BlockChildType {
    fn push_cell(parent: &mut BlockCell, block: BlockChildType) {
        parent.children.push(block)
    }

    fn insert_block_child(parent: &mut DataCell, block_child: BlockChildType) {
        if let CellType::Element(el) = &mut parent.cell_type {
            if let CellType::Block(block) = &mut el.children.first_mut().unwrap().cell_type {
                block.children.insert(0, block_child);
            }
        }
    }

    fn add_block_at_first(
        add_to: &mut DataCell,
        block_id: usize,
        block_child: &BlockChildType,
        block_options: Option<&BlockCell>,
    ) {
        if add_to.id == block_id {
            if let CellType::Element(el) = &mut add_to.cell_type {
                if let CellType::Block(block) = &mut el.children.first_mut().unwrap().cell_type {
                    block.children.insert(0, block_child.to_owned());
                    if let Some(options) = block_options {
                        block.has_counter_commands = options.has_counter_commands;
                        block.has_handle_insert = options.has_handle_insert;
                    }
                }
            }
            return;
        }

        match &mut add_to.cell_type {
            CellType::Element(ref mut el) => el
                .children
                .iter_mut()
                .for_each(|x| Self::add_block_at_first(x, block_id, block_child, block_options)),
            CellType::Root(ref mut el) => el
                .children
                .iter_mut()
                .for_each(|x| Self::add_block_at_first(x, block_id, block_child, block_options)),
            _ => (),
        }
    }

    fn insert_text_to_first_block_child_text(
        add_to: &mut DataCell,
        block_id: usize,
        text_to_insert: &str,
    ) {
        if add_to.id == block_id {
            if let CellType::Element(el) = &mut add_to.cell_type {
                if let CellType::Block(block) = &mut el.children.first_mut().unwrap().cell_type {
                    if let BlockChildType::Text(text) = &mut block.children.first_mut().unwrap() {
                        text.content = text_to_insert.to_owned() + &text.content;
                    }
                }
            }
        }

        match &mut add_to.cell_type {
            CellType::Element(ref mut el) => el.children.iter_mut().for_each(|x| {
                Self::insert_text_to_first_block_child_text(x, block_id, text_to_insert)
            }),
            CellType::Root(ref mut el) => el.children.iter_mut().for_each(|x| {
                Self::insert_text_to_first_block_child_text(x, block_id, text_to_insert)
            }),
            _ => (),
        }
    }
}