From 12df1f4b9b1dea8671cedb388d74d52e3a55fab7 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Wed, 25 Oct 2023 09:47:27 -0400 Subject: [PATCH] Create an UnorderedList HTML container --- file-service/src/html.rs | 51 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/file-service/src/html.rs b/file-service/src/html.rs index d38fb21..1ad0760 100644 --- a/file-service/src/html.rs +++ b/file-service/src/html.rs @@ -274,3 +274,54 @@ impl Html for Image { ) } } + +#[derive(Debug)] +pub struct UnorderedList { + children: Vec, + attributes: Attributes, +} + +impl UnorderedList { + pub fn new() -> Self { + Self { + children: vec![], + attributes: Attributes::default(), + } + } + + pub fn with_attributes<'a>( + mut self, + values: impl IntoIterator, + ) -> Self { + self.attributes = Attributes( + values + .into_iter() + .map(|(a, b)| (a.to_owned(), b.to_owned())) + .collect::>(), + ); + self + } +} + +impl Html for UnorderedList { + fn to_html_string(&self) -> String { + let children = self + .children + .iter() + .map(|item| format!("
  • {}
  • ", item.to_html_string())) + .collect::>(); + format!( + "
      + {children} +
    ", + attrs = self.attributes.to_string(), + children = children.join("\n") + ) + } +} + +impl HtmlContainer for UnorderedList { + fn add_html(&mut self, html: H) { + self.children.push(html.to_html_string()) + } +}