From 854a3e64fab1b421cf2b31958f618f9a2f5ca98e Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Sun, 19 Feb 2023 20:19:53 +0900 Subject: [PATCH] templater: split IntoTemplate trait The next commit will implement .into_template() on Box, so I want a trait for this particular method. --- src/commit_templater.rs | 6 ++++-- src/template_parser.rs | 15 +++++++++------ src/templater.rs | 4 ++++ 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/commit_templater.rs b/src/commit_templater.rs index 5b6e1e088..b78b52dc1 100644 --- a/src/commit_templater.rs +++ b/src/commit_templater.rs @@ -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 + 'repo> { fn wrap<'repo, O: Template<()> + 'repo>( property: Box + 'repo>, diff --git a/src/template_parser.rs b/src/template_parser.rs index 9122aa174..5f2ab61c7 100644 --- a/src/template_parser.rs +++ b/src/template_parser.rs @@ -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 + 'a>>; fn try_into_integer(self) -> Option + 'a>>; fn into_plain_text(self) -> Box + 'a>; - fn into_template(self) -> Box + '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 + 'a> { fn wrap<'a, I: 'a, O: Template<()> + 'a>( property: Box + '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 + 'a> { +impl<'a, C: 'a, P: IntoTemplate<'a, C>> IntoTemplate<'a, C> for Expression<'a, C, P> { + fn into_template(self) -> Box + 'a> { match self { Expression::Property(property, labels) => { let template = property.into_template(); diff --git a/src/templater.rs b/src/templater.rs index afc05cdeb..78d250646 100644 --- a/src/templater.rs +++ b/src/templater.rs @@ -25,6 +25,10 @@ pub trait Template { fn has_content(&self, context: &C) -> bool; } +pub trait IntoTemplate<'a, C> { + fn into_template(self) -> Box + 'a>; +} + impl + ?Sized> Template for Box { fn format(&self, context: &C, formatter: &mut dyn Formatter) -> io::Result<()> { >::format(self, context, formatter)