templater: implement .into_template() helper on formattable property

So that I can blindly write property.into_template() to convert any property
type to template.
This commit is contained in:
Yuya Nishihara 2023-02-19 19:56:06 +09:00
parent 854a3e64fa
commit c2e0ebca12
3 changed files with 20 additions and 22 deletions

View file

@ -30,8 +30,7 @@ use crate::template_parser::{
TemplateLanguage, TemplateParseError, TemplateParseResult,
};
use crate::templater::{
FormattablePropertyTemplate, IntoTemplate, PlainTextFormattedProperty, Template,
TemplateProperty, TemplatePropertyFn,
IntoTemplate, PlainTextFormattedProperty, Template, TemplateProperty, TemplatePropertyFn,
};
struct CommitTemplateLanguage<'repo, 'b> {
@ -117,15 +116,10 @@ impl<'repo> IntoTemplateProperty<'repo, Commit> for CommitTemplatePropertyKind<'
impl<'repo> IntoTemplate<'repo, Commit> for CommitTemplatePropertyKind<'repo> {
fn into_template(self) -> Box<dyn Template<Commit> + 'repo> {
fn wrap<'repo, O: Template<()> + 'repo>(
property: Box<dyn TemplateProperty<Commit, Output = O> + 'repo>,
) -> Box<dyn Template<Commit> + 'repo> {
Box::new(FormattablePropertyTemplate::new(property))
}
match self {
CommitTemplatePropertyKind::Core(property) => property.into_template(),
CommitTemplatePropertyKind::CommitOrChangeId(property) => wrap(property),
CommitTemplatePropertyKind::ShortestIdPrefix(property) => wrap(property),
CommitTemplatePropertyKind::CommitOrChangeId(property) => property.into_template(),
CommitTemplatePropertyKind::ShortestIdPrefix(property) => property.into_template(),
}
}
}

View file

@ -25,9 +25,9 @@ use pest_derive::Parser;
use thiserror::Error;
use crate::templater::{
ConditionalTemplate, FormattablePropertyTemplate, IntoTemplate, LabelTemplate, ListTemplate,
Literal, PlainTextFormattedProperty, SeparateTemplate, Template, TemplateFunction,
TemplateProperty, TemplatePropertyFn,
ConditionalTemplate, IntoTemplate, LabelTemplate, ListTemplate, Literal,
PlainTextFormattedProperty, SeparateTemplate, Template, TemplateFunction, TemplateProperty,
TemplatePropertyFn,
};
use crate::time_util;
@ -702,17 +702,12 @@ impl<'a, I: 'a> IntoTemplateProperty<'a, I> for CoreTemplatePropertyKind<'a, I>
impl<'a, I: 'a> IntoTemplate<'a, I> for CoreTemplatePropertyKind<'a, I> {
fn into_template(self) -> Box<dyn Template<I> + 'a> {
fn wrap<'a, I: 'a, O: Template<()> + 'a>(
property: Box<dyn TemplateProperty<I, Output = O> + 'a>,
) -> Box<dyn Template<I> + 'a> {
Box::new(FormattablePropertyTemplate::new(property))
}
match self {
CoreTemplatePropertyKind::String(property) => wrap(property),
CoreTemplatePropertyKind::Boolean(property) => wrap(property),
CoreTemplatePropertyKind::Integer(property) => wrap(property),
CoreTemplatePropertyKind::Signature(property) => wrap(property),
CoreTemplatePropertyKind::Timestamp(property) => wrap(property),
CoreTemplatePropertyKind::String(property) => property.into_template(),
CoreTemplatePropertyKind::Boolean(property) => property.into_template(),
CoreTemplatePropertyKind::Integer(property) => property.into_template(),
CoreTemplatePropertyKind::Signature(property) => property.into_template(),
CoreTemplatePropertyKind::Timestamp(property) => property.into_template(),
}
}
}

View file

@ -303,6 +303,15 @@ where
}
}
impl<'a, C: 'a, O> IntoTemplate<'a, C> for Box<dyn TemplateProperty<C, Output = O> + 'a>
where
O: Template<()> + 'a,
{
fn into_template(self) -> Box<dyn Template<C> + 'a> {
Box::new(FormattablePropertyTemplate::new(self))
}
}
/// Adapter to turn template back to string property.
pub struct PlainTextFormattedProperty<T> {
template: T,