use crate::{
element::{Element, HtmlElement, HtmlElementConfig},
tags::TagType,
};
pub trait AsTable {
fn as_table_head(&self) -> Option<Element>;
fn as_table_row(&self) -> Option<Element>;
fn as_table_foot(&self) -> Option<Element>;
}
pub fn from_iterator<T>(
collection: &Vec<T>,
table_config: HtmlElementConfig,
table_body_config: HtmlElementConfig,
) -> Option<Element>
where
T: AsTable,
{
if collection.is_empty() {
return None;
}
let mut table = Element::Element(HtmlElement::new(TagType::Table, table_config));
let mut body = Element::Element(HtmlElement::new(TagType::Tbody, table_body_config));
let head = collection.get(0).unwrap().as_table_head();
let foot = collection.get(0).unwrap().as_table_foot();
if let Some(head) = head {
table += head;
};
for item in collection {
if let Some(row) = item.as_table_row() {
body += row;
}
}
table += body;
if let Some(foot) = foot {
table += foot;
};
Some(table)
}