From 7b641c0236604b21ca76c6d06ad3fa9b57cd6b3e Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Tue, 19 Mar 2024 20:10:59 +0900 Subject: [PATCH] templater: add Template adapter that evaluates Template<()> with value of C The idea is basically the same as list.map(|x| ...) template. It compiles the inner template with a placeholder variable of type 'C', and evaluate it for each instance variable by injecting the variable through RefCell. --- cli/src/templater.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/cli/src/templater.rs b/cli/src/templater.rs index e8a193418..43bb1f6c4 100644 --- a/cli/src/templater.rs +++ b/cli/src/templater.rs @@ -617,6 +617,28 @@ impl TemplateProperty for PropertyPlaceholder { } } +/// Adapter that renders compiled `template` with the `placeholder` value set. +pub struct PlaceholderTemplate { + template: T, + placeholder: PropertyPlaceholder, +} + +impl> PlaceholderTemplate { + pub fn new(template: T, placeholder: PropertyPlaceholder) -> Self { + PlaceholderTemplate { + template, + placeholder, + } + } +} + +impl> Template for PlaceholderTemplate { + fn format(&self, context: &C, formatter: &mut dyn Formatter) -> io::Result<()> { + self.placeholder + .with_value(context.clone(), || self.template.format(&(), formatter)) + } +} + pub fn format_joined( context: &C, formatter: &mut dyn Formatter,