use std::fmt::{Write};
use crate::{HtmlEnv, Empty, Sum};
pub trait Html {
    fn is_unit(&self) -> bool {
        false
    }
    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(())
    }
}
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())
    }
}
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())
    }
}
pub trait AsHtml {
    type Html: Html;
    type HtmlText: Html;
    fn as_html(self) -> Self::Html;
    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)
    }
}