1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use crate::{HtmlComponent, HtmlContent, HtmlFormatter};
use std::fmt;

pub struct For<I, F>
where
	I: IntoIterator,
	F: FnMut(&mut HtmlFormatter, <I as IntoIterator>::Item) -> fmt::Result,
{
	pub items: I,
	pub children: F,
}

impl<I, F> HtmlComponent for For<I, F>
where
	I: IntoIterator,
	F: FnMut(&mut HtmlFormatter, <I as IntoIterator>::Item) -> fmt::Result,
{
	type Content = Self;

	fn into_content(self) -> Self::Content {
		self
	}
}

impl<I, F> HtmlContent for For<I, F>
where
	I: IntoIterator,
	F: FnMut(&mut HtmlFormatter, <I as IntoIterator>::Item) -> fmt::Result,
{
	fn fmt(self, formatter: &mut HtmlFormatter) -> std::fmt::Result {
		let For {
			items,
			children: mut template,
		} = self;

		for item in items {
			template(formatter, item)?;
		}

		Ok(())
	}
}