html_site_generator/html/
body.rs

1use std::io::Write;
2
3use anyhow::Result;
4
5use crate::html::IntoHtmlNode;
6
7#[derive(Debug)]
8pub struct Body {
9    elements: Vec<Box<dyn IntoHtmlNode>>,
10}
11
12impl Body {
13    pub fn new() -> Self {
14        Body {
15            elements: Vec::new(),
16        }
17    }
18
19    pub fn add_element(&mut self, item: impl IntoHtmlNode + 'static) {
20        self.elements.push(Box::new(item))
21    }
22}
23
24impl IntoHtmlNode for Body {
25    fn transform_into_html_node(&self, buffer: &mut dyn Write) -> Result<()> {
26        writeln!(buffer, "<body>")?;
27
28        for element in &self.elements {
29            element.transform_into_html_node(buffer)?;
30        }
31
32        writeln!(buffer, "</body>")?;
33
34        Ok(())
35    }
36}