templater: add helper to format template properties with separator

contents will be an Iterator<Item = &P> where P is a Template<()>.
This commit is contained in:
Yuya Nishihara 2023-03-06 21:14:07 +09:00
parent 7da0994d58
commit 7b206e6fb5

View file

@ -27,6 +27,12 @@ pub trait IntoTemplate<'a, C> {
fn into_template(self) -> Box<dyn Template<C> + 'a>;
}
impl<C, T: Template<C> + ?Sized> Template<C> for &T {
fn format(&self, context: &C, formatter: &mut dyn Formatter) -> io::Result<()> {
<T as Template<C>>::format(self, context, formatter)
}
}
impl<C, T: Template<C> + ?Sized> Template<C> for Box<T> {
fn format(&self, context: &C, formatter: &mut dyn Formatter) -> io::Result<()> {
<T as Template<C>>::format(self, context, formatter)
@ -425,3 +431,25 @@ where
(self.function)(self.property.extract(context))
}
}
pub fn format_joined<C, I, S>(
context: &C,
formatter: &mut dyn Formatter,
contents: I,
separator: S,
) -> io::Result<()>
where
I: IntoIterator,
I::Item: Template<C>,
S: Template<C>,
{
let mut contents_iter = contents.into_iter().fuse();
if let Some(content) = contents_iter.next() {
content.format(context, formatter)?;
}
for content in contents_iter {
separator.format(context, formatter)?;
content.format(context, formatter)?;
}
Ok(())
}