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
use std::io::Write;
use std::{collections::VecDeque, fs::File};
use Line::{AgdaBlock, Content};

pub(crate) enum Line {
    AgdaBlock,
    Content(String),
}

pub(crate) struct Tree {
    lines: Vec<Line>,
}

impl Tree {
    pub(crate) fn new() -> Tree {
        Tree { lines: vec![] }
    }

    pub(crate) fn push(&mut self, content: String) {
        self.lines.push(Line::Content(content));
    }
    pub(crate) fn push_agda(&mut self) {
        self.lines.push(Line::AgdaBlock);
    }

    pub fn merge(&self, mut agda_blocks: VecDeque<String>) -> Tree {
        let mut new_tree = Tree::new();
        for l in &self.lines {
            match l {
                Content(s) => new_tree.push(s.clone()),
                AgdaBlock => {
                    let k = agda_blocks.pop_front().expect("agda blocks is not enough");
                    new_tree.push(k);
                }
            }
        }
        new_tree
    }

    pub fn write(&self, mut output: File) {
        output
            .write("\\xmlns:html{http://www.w3.org/1999/xhtml}\n".as_bytes())
            .unwrap();
        for l in &self.lines {
            match l {
                Content(s) => {
                    let mut content = s.clone();
                    content.push('\n');
                    let _ = output.write(content.as_bytes()).unwrap();
                }
                AgdaBlock => {}
            }
        }
    }
}