mirror of
https://github.com/martinvonz/jj.git
synced 2025-01-18 18:27:38 +00:00
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:
parent
7da0994d58
commit
7b206e6fb5
1 changed files with 28 additions and 0 deletions
|
@ -27,6 +27,12 @@ pub trait IntoTemplate<'a, C> {
|
||||||
fn into_template(self) -> Box<dyn Template<C> + 'a>;
|
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> {
|
impl<C, T: Template<C> + ?Sized> Template<C> for Box<T> {
|
||||||
fn format(&self, context: &C, formatter: &mut dyn Formatter) -> io::Result<()> {
|
fn format(&self, context: &C, formatter: &mut dyn Formatter) -> io::Result<()> {
|
||||||
<T as Template<C>>::format(self, context, formatter)
|
<T as Template<C>>::format(self, context, formatter)
|
||||||
|
@ -425,3 +431,25 @@ where
|
||||||
(self.function)(self.property.extract(context))
|
(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(())
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue