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
use super::Node;
use crate::prelude::print::{self, Print};
use std::fmt;

impl<T: Print> Print for Node<T> {
    fn print(&self, pretty: bool, level: usize, indent_size: usize) -> String {
        print::open(
            &self.tag,
            Some(&self.attributes),
            false,
            pretty,
            level,
            indent_size,
        ) + &self
            .children
            .iter()
            .map(|child| child.print(pretty, level + 1, indent_size))
            .collect::<String>()
            + &print::close(&self.tag, pretty, level, indent_size)
    }
}

impl<T: Print> fmt::Display for Node<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(self.dense_print().as_str())
    }
}

#[cfg(test)]
mod tests {
    use crate::mj_body::MJBodyChild;
    use crate::mj_raw::MJRawChild;
    use crate::prelude::print::Print;

    #[test]
    fn normal() {
        let item = crate::node::Node::<MJBodyChild>::from("span");
        assert_eq!("<span></span>", item.dense_print());
        let item = crate::node::Node::<MJRawChild>::from("span");
        assert_eq!("<span></span>", item.dense_print());
    }

    #[test]
    fn with_attributes() {
        let mut item = crate::node::Node::<MJBodyChild>::from("span");
        item.attributes
            .insert("color".to_string(), "red".to_string());
        item.children
            .push(crate::node::Node::from("b".to_string()).into());
        assert_eq!("<span color=\"red\"><b></b></span>", item.dense_print());
    }
}