ok/jj
1
0
Fork 0
forked from mirrors/jj

templater: split IntoTemplate trait

The next commit will implement .into_template() on Box<dyn TemplateProperty>,
so I want a trait for this particular method.
This commit is contained in:
Yuya Nishihara 2023-02-19 20:19:53 +09:00
parent 9c51d74b2c
commit 854a3e64fa
3 changed files with 17 additions and 8 deletions

View file

@ -30,8 +30,8 @@ use crate::template_parser::{
TemplateLanguage, TemplateParseError, TemplateParseResult,
};
use crate::templater::{
FormattablePropertyTemplate, PlainTextFormattedProperty, Template, TemplateProperty,
TemplatePropertyFn,
FormattablePropertyTemplate, IntoTemplate, PlainTextFormattedProperty, Template,
TemplateProperty, TemplatePropertyFn,
};
struct CommitTemplateLanguage<'repo, 'b> {
@ -113,7 +113,9 @@ impl<'repo> IntoTemplateProperty<'repo, Commit> for CommitTemplatePropertyKind<'
_ => Box::new(PlainTextFormattedProperty::new(self.into_template())),
}
}
}
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>,

View file

@ -25,9 +25,9 @@ use pest_derive::Parser;
use thiserror::Error;
use crate::templater::{
ConditionalTemplate, FormattablePropertyTemplate, LabelTemplate, ListTemplate, Literal,
PlainTextFormattedProperty, SeparateTemplate, Template, TemplateFunction, TemplateProperty,
TemplatePropertyFn,
ConditionalTemplate, FormattablePropertyTemplate, IntoTemplate, LabelTemplate, ListTemplate,
Literal, PlainTextFormattedProperty, SeparateTemplate, Template, TemplateFunction,
TemplateProperty, TemplatePropertyFn,
};
use crate::time_util;
@ -659,12 +659,11 @@ macro_rules! impl_wrap_property_fns {
pub(crate) use {impl_core_wrap_property_fns, impl_wrap_property_fns};
/// Provides access to basic template property types.
pub trait IntoTemplateProperty<'a, C> {
pub trait IntoTemplateProperty<'a, C>: IntoTemplate<'a, C> {
fn try_into_boolean(self) -> Option<Box<dyn TemplateProperty<C, Output = bool> + 'a>>;
fn try_into_integer(self) -> Option<Box<dyn TemplateProperty<C, Output = i64> + 'a>>;
fn into_plain_text(self) -> Box<dyn TemplateProperty<C, Output = String> + 'a>;
fn into_template(self) -> Box<dyn Template<C> + 'a>;
}
pub enum CoreTemplatePropertyKind<'a, I> {
@ -699,7 +698,9 @@ impl<'a, I: 'a> IntoTemplateProperty<'a, I> for CoreTemplatePropertyKind<'a, I>
_ => Box::new(PlainTextFormattedProperty::new(self.into_template())),
}
}
}
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>,
@ -742,8 +743,10 @@ impl<'a, C: 'a, P: IntoTemplateProperty<'a, C>> Expression<'a, C, P> {
Expression::Template(template) => Box::new(PlainTextFormattedProperty::new(template)),
}
}
}
pub fn into_template(self) -> Box<dyn Template<C> + 'a> {
impl<'a, C: 'a, P: IntoTemplate<'a, C>> IntoTemplate<'a, C> for Expression<'a, C, P> {
fn into_template(self) -> Box<dyn Template<C> + 'a> {
match self {
Expression::Property(property, labels) => {
let template = property.into_template();

View file

@ -25,6 +25,10 @@ pub trait Template<C> {
fn has_content(&self, context: &C) -> bool;
}
pub trait IntoTemplate<'a, C> {
fn into_template(self) -> Box<dyn Template<C> + 'a>;
}
impl<C, T: Template<C> + ?Sized> Template<C> for Box<T> {
fn format(&self, context: &C, formatter: &mut dyn Formatter) -> io::Result<()> {
<T as Template<C>>::format(self, context, formatter)