templater: extract formatting function from LabelTemplate

This commit is contained in:
Yuya Nishihara 2024-04-13 20:35:19 +09:00
parent 4474577ceb
commit 610e310112

View file

@ -169,18 +169,10 @@ where
L: TemplateProperty<Output = Vec<String>>,
{
fn format(&self, formatter: &mut TemplateFormatter) -> io::Result<()> {
let labels = match self.labels.extract() {
Ok(labels) => labels,
Err(err) => return formatter.handle_error(err),
};
for label in &labels {
formatter.push_label(label)?;
match self.labels.extract() {
Ok(labels) => format_labeled(formatter, &self.content, &labels),
Err(err) => formatter.handle_error(err),
}
self.content.format(formatter)?;
for _label in &labels {
formatter.pop_label()?;
}
Ok(())
}
}
@ -748,6 +740,21 @@ where
Ok(())
}
fn format_labeled<T: Template + ?Sized>(
formatter: &mut TemplateFormatter,
content: &T,
labels: &[String],
) -> io::Result<()> {
for label in labels {
formatter.push_label(label)?;
}
content.format(formatter)?;
for _label in labels {
formatter.pop_label()?;
}
Ok(())
}
type PropertyErrorHandler = fn(&mut dyn Formatter, TemplatePropertyError) -> io::Result<()>;
/// Prints property evaluation error as inline template output.