use std::iter::empty;
use crate::{Container, Html, HtmlChild, HtmlElement, HtmlTag, Table};
pub trait HtmlContainer: Html + Sized {
fn add_html<H: Html>(&mut self, html: H);
#[inline]
fn with_html<H: Html>(mut self, html: H) -> Self {
self.add_html(html);
self
}
#[inline]
fn add_container(&mut self, container: Container) {
self.add_html(container)
}
#[inline]
fn with_container(self, container: Container) -> Self {
self.with_html(container)
}
fn add_table(&mut self, table: Table) {
self.add_html(table);
}
fn with_table(self, table: Table) -> Self {
self.with_html(table)
}
fn add_header(&mut self, level: u8, text: impl ToString) {
self.add_header_attr(level, text, empty::<(&str, &str)>());
}
fn with_header(self, level: u8, text: impl ToString) -> Self {
self.with_header_attr(level, text, empty::<(&str, &str)>())
}
fn add_header_attr<A, S>(&mut self, level: u8, text: impl ToString, attr: A)
where
A: IntoIterator<Item = (S, S)>,
S: ToString,
{
let tag = match level {
1 => HtmlTag::Heading1,
2 => HtmlTag::Heading2,
3 => HtmlTag::Heading3,
4 => HtmlTag::Heading4,
5 => HtmlTag::Heading5,
6 => HtmlTag::Heading6,
_ => panic!("'{}' is not a valid html heading level", level),
};
let mut element = HtmlElement::new(tag).with_child(HtmlChild::Raw(text.to_string()));
for (k, v) in attr {
element.add_attribute(k, v)
}
self.add_html(element);
}
fn with_header_attr<A, S>(mut self, level: u8, text: impl ToString, attr: A) -> Self
where
A: IntoIterator<Item = (S, S)>,
S: ToString,
{
self.add_header_attr(level, text, attr);
self
}
fn add_image(&mut self, src: impl ToString, alt: impl ToString) {
self.add_image_attr(src, alt, empty::<(&str, &str)>());
}
fn with_image(self, src: impl ToString, alt: impl ToString) -> Self {
self.with_image_attr(src, alt, empty::<(&str, &str)>())
}
fn add_image_attr<A, S>(&mut self, src: impl ToString, alt: impl ToString, attr: A)
where
A: IntoIterator<Item = (S, S)>,
S: ToString,
{
let mut element = HtmlElement::new(HtmlTag::Image)
.with_attribute("src", src)
.with_attribute("alt", alt);
for (k, v) in attr {
element.add_attribute(k, v);
}
self.add_html(element);
}
fn with_image_attr<A, S>(mut self, src: impl ToString, alt: impl ToString, attr: A) -> Self
where
A: IntoIterator<Item = (S, S)>,
S: ToString,
{
self.add_image_attr(src, alt, attr);
self
}
fn add_link(&mut self, href: impl ToString, text: impl ToString) {
self.add_link_attr(href, text, empty::<(&str, &str)>());
}
fn with_link(self, href: impl ToString, text: impl ToString) -> Self {
self.with_link_attr(href, text, empty::<(&str, &str)>())
}
fn add_link_attr<A, S>(&mut self, href: impl ToString, text: impl ToString, attr: A)
where
A: IntoIterator<Item = (S, S)>,
S: ToString,
{
let mut element = HtmlElement::new(HtmlTag::Link)
.with_attribute("href", href)
.with_child(HtmlChild::Raw(text.to_string()));
for (k, v) in attr {
element.add_attribute(k, v);
}
self.add_html(element);
}
fn with_link_attr<A, S>(mut self, href: impl ToString, text: impl ToString, attr: A) -> Self
where
A: IntoIterator<Item = (S, S)>,
S: ToString,
{
self.add_link_attr(href, text, attr);
self
}
fn add_paragraph(&mut self, text: impl ToString) {
self.add_paragraph_attr(text, empty::<(&str, &str)>());
}
fn with_paragraph(self, text: impl ToString) -> Self {
self.with_paragraph_attr(text, empty::<(&str, &str)>())
}
fn add_paragraph_attr<A, S>(&mut self, text: impl ToString, attr: A)
where
A: IntoIterator<Item = (S, S)>,
S: ToString,
{
let mut element =
HtmlElement::new(HtmlTag::ParagraphText).with_child(HtmlChild::Raw(text.to_string()));
for (k, v) in attr {
element.add_attribute(k, v);
}
self.add_html(element);
}
fn with_paragraph_attr<A, S>(mut self, text: impl ToString, attr: A) -> Self
where
A: IntoIterator<Item = (S, S)>,
S: ToString,
{
self.add_paragraph_attr(text, attr);
self
}
fn add_preformatted(&mut self, text: impl ToString) {
self.add_preformatted_attr(text, empty::<(&str, &str)>());
}
fn with_preformatted(self, text: impl ToString) -> Self {
self.with_preformatted_attr(text, empty::<(&str, &str)>())
}
fn add_preformatted_attr<A, S>(&mut self, text: impl ToString, attr: A)
where
A: IntoIterator<Item = (S, S)>,
S: ToString,
{
let mut element = HtmlElement::new(HtmlTag::PreformattedText)
.with_child(HtmlChild::Raw(text.to_string()));
for (k, v) in attr {
element.add_attribute(k, v);
}
self.add_html(element);
}
fn with_preformatted_attr<A, S>(mut self, text: impl ToString, attr: A) -> Self
where
A: IntoIterator<Item = (S, S)>,
S: ToString,
{
self.add_preformatted_attr(text, attr);
self
}
fn add_raw(&mut self, content: impl ToString) {
self.add_html(content.to_string());
}
fn with_raw(self, content: impl ToString) -> Self {
self.with_html(content.to_string())
}
}