Skip to main content

composable_html/
render.rs

1/// The trait that composable HTML elements should implement.
2///
3/// Implementers of this trait can also implement [core::fmt::Display] to use directly a Render in
4/// a format macro call.
5///
6/// An example of an HTML paragraph element:
7/// ```
8/// # use composable_html::render::Render;
9/// struct PlainParagraph {
10///     content: String,
11/// }
12///
13/// impl Render for PlainParagraph {
14///     fn render(&self) -> String {
15///         format!("<p>{}</p>", self.content)
16///     }
17/// }
18/// ```
19pub trait Render {
20    /// Returns a String representation of the implementer.
21    fn render(&self) -> String;
22}
23
24/// A String returns a copy of itself when rendered
25impl Render for String {
26    fn render(&self) -> String {
27        self.clone()
28    }
29}
30
31#[cfg(test)]
32mod test {
33    use crate::render::Render;
34
35    #[test]
36    fn test_string_render() {
37        let s = String::from("test");
38        assert_eq!(s, s.render());
39    }
40}