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
pub mod nodes;
pub mod parser;
pub mod tokenizer;

mod tests;

use tokenizer::*;
use parser::*;
use nodes::*;

pub struct Document {
    root: Element,
}

impl Document {
    pub fn new() -> Document {
        Document {
            root: Element::new("root"),
        }
    }

    pub fn from_element(e: Element) -> Document {
        Document {
            root: e,
        }
    }

    pub fn from_string(s: &str) -> Result<Document, String> {
        let tokens = match tokenize(&s) {
            Ok(tokens) => tokens,
            Err(e) => return Err(e),
        };

        let element = match parse(tokens) {
            Ok(element) => element,
            Err(e) => return Err(e),
        };

        Ok(Document::from_element(element))
    }

    pub fn from_file(p: &str) -> Result<Document, String> {
        let string = match string_from_file(p) {
            Some(string) => string,
            None => return Err("Couldn't make String from file".into()),
        };
            
        match Document::from_string(&string) {
            Ok(document) => Ok(document),
            Err(e) => Err(e),
        }
    }

    pub fn get_root(&self) -> &Element {
        match self.root.get_first_child() {
            Some(c) => c,
            None => panic!("Document has no root element!"),
        }
    }

    pub fn print(&self) {
        self.root.print(0);
    }
}