html_site_generator/html/
paragraph.rs

1use std::io::Write;
2
3use anyhow::Result;
4use html_site_generator_macro::{add_attributes_field, DeriveSetHtmlAttributes};
5
6use crate::html::{IntoHtmlNode, IsParagraph};
7
8#[add_attributes_field]
9#[derive(Debug, DeriveSetHtmlAttributes)]
10pub struct Paragraph {
11    elements: Vec<Box<dyn IsParagraph>>,
12}
13
14impl Paragraph {
15    pub fn new() -> Self {
16        Paragraph {
17            _attributes: Default::default(),
18            elements: Vec::new(),
19        }
20    }
21
22    pub fn add_element(&mut self, item: impl IsParagraph + 'static) {
23        self.elements.push(Box::new(item))
24    }
25}
26
27impl IsParagraph for Paragraph {
28    fn to_raw(&self) -> String {
29        let mut s = String::new();
30
31        // TODO maybe use something like https://crates.io/crates/string-builder, if a this (or another crate) is faster than this method.
32        for element in &self.elements {
33            s.push_str(&element.to_raw());
34        }
35
36        s
37    }
38}
39
40impl IntoHtmlNode for Paragraph {
41    fn transform_into_html_node(&self, buffer: &mut dyn Write) -> Result<()> {
42        writeln!(buffer, "<p>")?;
43
44        writeln!(buffer, "{}", self.to_raw())?;
45
46        writeln!(buffer, "</p>")?;
47
48        Ok(())
49    }
50}