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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use std::fmt::{Write};

use crate::{HtmlEnv, Empty, Sum};


/// Represents a content that can be written to a `Write` as HTML.
pub trait Html {
    /// Tells whether `self` is a unit value, meaning that it is not written.
    fn is_unit(&self) -> bool {
        false
    }
    /// Writes the HTML representation of `self` to `w`.
    ///
    /// # Arguments
    /// * `env` - The environment to write to.
    ///
    /// # Example
    /// ```
    /// use write_html::{Html, HtmlEnv, AsHtml};
    /// use std::fmt::Write;
    /// 
    /// let mut s = String::new();
    /// "<h1>H1</h1>".as_html().write_html(&mut s).unwrap();
    /// assert_eq!(s, "<h1>H1</h1>");
    /// ```
    fn write_html(self, env: &mut impl HtmlEnv) -> std::fmt::Result;
}

impl Html for Empty {
    fn is_unit(&self) -> bool {
        true
    }
    fn write_html(self, _env: &mut impl HtmlEnv) -> std::fmt::Result {
        Ok(())
    }
}

impl<A: Html, B: Html> Html for Sum<A, B> {
    fn write_html(self, env: &mut impl HtmlEnv) -> std::fmt::Result {
        self.0.write_html(env)?;
        self.1.write_html(env)?;
        Ok(())
    }
}

impl<I: IntoIterator> Html for I
where
    I::Item: Html,
{
    fn write_html(self, env: &mut impl HtmlEnv) -> std::fmt::Result {
        for h in self {
            h.write_html(env)?;
        }
        Ok(())
    }
}

/// Html string
///
/// TODO better doc
pub struct HtmlStr<S>(pub S);

impl<S> Html for HtmlStr<S>
where
    S: AsRef<str>,
{
    fn write_html(self, env: &mut impl HtmlEnv) -> std::fmt::Result {
        env.write_str(self.0.as_ref())
    }
}

/// Html text string
///
/// TODO better doc
pub struct HtmlTextStr<S>(pub S);

impl<S> Html for HtmlTextStr<S>
where
    S: AsRef<str>,
{
    fn write_html(self, env: &mut impl HtmlEnv) -> std::fmt::Result {
        env.text().write_str(self.0.as_ref())
    }
}

/// Something that can be converted into HTML.
///
/// TODO better doc
pub trait AsHtml {
    /// The HTML type.
    type Html: Html;
    /// The HTML text type.
    type HtmlText: Html;
    /// Converts `self` into HTML.
    fn as_html(self) -> Self::Html;
    /// Converts `self` into HTML text.
    fn as_html_text(self) -> Self::HtmlText;
}

impl<'a> AsHtml for &'a str {
    type Html = HtmlStr<&'a str>;
    type HtmlText = HtmlTextStr<&'a str>;
    fn as_html(self) -> Self::Html {
        HtmlStr(self)
    }
    fn as_html_text(self) -> Self::HtmlText {
        HtmlTextStr(self)
    }
}

impl AsHtml for String {
    type Html = HtmlStr<String>;
    type HtmlText = HtmlTextStr<String>;
    fn as_html(self) -> Self::Html {
        HtmlStr(self)
    }
    fn as_html_text(self) -> Self::HtmlText {
        HtmlTextStr(self)
    }
}