mirror of
https://github.com/martinvonz/jj.git
synced 2024-10-24 07:32:54 +00:00
templater: add Template<C> 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.
This commit is contained in:
parent
017026148b
commit
7b641c0236
1 changed files with 22 additions and 0 deletions
|
@ -617,6 +617,28 @@ impl<C, O: Clone> TemplateProperty<C> for PropertyPlaceholder<O> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Adapter that renders compiled `template` with the `placeholder` value set.
|
||||
pub struct PlaceholderTemplate<C, T> {
|
||||
template: T,
|
||||
placeholder: PropertyPlaceholder<C>,
|
||||
}
|
||||
|
||||
impl<C: Clone, T: Template<()>> PlaceholderTemplate<C, T> {
|
||||
pub fn new(template: T, placeholder: PropertyPlaceholder<C>) -> Self {
|
||||
PlaceholderTemplate {
|
||||
template,
|
||||
placeholder,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<C: Clone, T: Template<()>> Template<C> for PlaceholderTemplate<C, T> {
|
||||
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<C, I, S>(
|
||||
context: &C,
|
||||
formatter: &mut dyn Formatter,
|
||||
|
|
Loading…
Reference in a new issue