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:
parent
9c51d74b2c
commit
854a3e64fa
3 changed files with 17 additions and 8 deletions
|
@ -30,8 +30,8 @@ use crate::template_parser::{
|
||||||
TemplateLanguage, TemplateParseError, TemplateParseResult,
|
TemplateLanguage, TemplateParseError, TemplateParseResult,
|
||||||
};
|
};
|
||||||
use crate::templater::{
|
use crate::templater::{
|
||||||
FormattablePropertyTemplate, PlainTextFormattedProperty, Template, TemplateProperty,
|
FormattablePropertyTemplate, IntoTemplate, PlainTextFormattedProperty, Template,
|
||||||
TemplatePropertyFn,
|
TemplateProperty, TemplatePropertyFn,
|
||||||
};
|
};
|
||||||
|
|
||||||
struct CommitTemplateLanguage<'repo, 'b> {
|
struct CommitTemplateLanguage<'repo, 'b> {
|
||||||
|
@ -113,7 +113,9 @@ impl<'repo> IntoTemplateProperty<'repo, Commit> for CommitTemplatePropertyKind<'
|
||||||
_ => Box::new(PlainTextFormattedProperty::new(self.into_template())),
|
_ => 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 into_template(self) -> Box<dyn Template<Commit> + 'repo> {
|
||||||
fn wrap<'repo, O: Template<()> + 'repo>(
|
fn wrap<'repo, O: Template<()> + 'repo>(
|
||||||
property: Box<dyn TemplateProperty<Commit, Output = O> + 'repo>,
|
property: Box<dyn TemplateProperty<Commit, Output = O> + 'repo>,
|
||||||
|
|
|
@ -25,9 +25,9 @@ use pest_derive::Parser;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
use crate::templater::{
|
use crate::templater::{
|
||||||
ConditionalTemplate, FormattablePropertyTemplate, LabelTemplate, ListTemplate, Literal,
|
ConditionalTemplate, FormattablePropertyTemplate, IntoTemplate, LabelTemplate, ListTemplate,
|
||||||
PlainTextFormattedProperty, SeparateTemplate, Template, TemplateFunction, TemplateProperty,
|
Literal, PlainTextFormattedProperty, SeparateTemplate, Template, TemplateFunction,
|
||||||
TemplatePropertyFn,
|
TemplateProperty, TemplatePropertyFn,
|
||||||
};
|
};
|
||||||
use crate::time_util;
|
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};
|
pub(crate) use {impl_core_wrap_property_fns, impl_wrap_property_fns};
|
||||||
|
|
||||||
/// Provides access to basic template property types.
|
/// 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_boolean(self) -> Option<Box<dyn TemplateProperty<C, Output = bool> + 'a>>;
|
||||||
fn try_into_integer(self) -> Option<Box<dyn TemplateProperty<C, Output = i64> + '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_plain_text(self) -> Box<dyn TemplateProperty<C, Output = String> + 'a>;
|
||||||
fn into_template(self) -> Box<dyn Template<C> + 'a>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum CoreTemplatePropertyKind<'a, I> {
|
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())),
|
_ => 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 into_template(self) -> Box<dyn Template<I> + 'a> {
|
||||||
fn wrap<'a, I: 'a, O: Template<()> + 'a>(
|
fn wrap<'a, I: 'a, O: Template<()> + 'a>(
|
||||||
property: Box<dyn TemplateProperty<I, Output = O> + '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)),
|
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 {
|
match self {
|
||||||
Expression::Property(property, labels) => {
|
Expression::Property(property, labels) => {
|
||||||
let template = property.into_template();
|
let template = property.into_template();
|
||||||
|
|
|
@ -25,6 +25,10 @@ pub trait Template<C> {
|
||||||
fn has_content(&self, context: &C) -> bool;
|
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> {
|
impl<C, T: Template<C> + ?Sized> Template<C> for Box<T> {
|
||||||
fn format(&self, context: &C, formatter: &mut dyn Formatter) -> io::Result<()> {
|
fn format(&self, context: &C, formatter: &mut dyn Formatter) -> io::Result<()> {
|
||||||
<T as Template<C>>::format(self, context, formatter)
|
<T as Template<C>>::format(self, context, formatter)
|
||||||
|
|
Loading…
Reference in a new issue