Create an UnorderedList HTML container

This commit is contained in:
Savanni D'Gerinel 2023-10-25 09:47:27 -04:00
parent c2e34db79c
commit 12df1f4b9b
1 changed files with 51 additions and 0 deletions

View File

@ -274,3 +274,54 @@ impl Html for Image {
)
}
}
#[derive(Debug)]
pub struct UnorderedList {
children: Vec<String>,
attributes: Attributes,
}
impl UnorderedList {
pub fn new() -> Self {
Self {
children: vec![],
attributes: Attributes::default(),
}
}
pub fn with_attributes<'a>(
mut self,
values: impl IntoIterator<Item = (&'a str, &'a str)>,
) -> Self {
self.attributes = Attributes(
values
.into_iter()
.map(|(a, b)| (a.to_owned(), b.to_owned()))
.collect::<Vec<(String, String)>>(),
);
self
}
}
impl Html for UnorderedList {
fn to_html_string(&self) -> String {
let children = self
.children
.iter()
.map(|item| format!("<li>{}</li>", item.to_html_string()))
.collect::<Vec<String>>();
format!(
"<ul {attrs}>
{children}
</ul>",
attrs = self.attributes.to_string(),
children = children.join("\n")
)
}
}
impl HtmlContainer for UnorderedList {
fn add_html<H: Html>(&mut self, html: H) {
self.children.push(html.to_html_string())
}
}