From c2e0ebca12142abe8392f84d257428080591ad24 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Sun, 19 Feb 2023 19:56:06 +0900 Subject: [PATCH] templater: implement .into_template() helper on formattable property So that I can blindly write property.into_template() to convert any property type to template. --- src/commit_templater.rs | 12 +++--------- src/template_parser.rs | 21 ++++++++------------- src/templater.rs | 9 +++++++++ 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/src/commit_templater.rs b/src/commit_templater.rs index b78b52dc1..2014d30e8 100644 --- a/src/commit_templater.rs +++ b/src/commit_templater.rs @@ -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 + 'repo> { - fn wrap<'repo, O: Template<()> + 'repo>( - property: Box + 'repo>, - ) -> Box + '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(), } } } diff --git a/src/template_parser.rs b/src/template_parser.rs index 5f2ab61c7..d53815107 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, 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 + 'a> { - fn wrap<'a, I: 'a, O: Template<()> + 'a>( - property: Box + 'a>, - ) -> Box + '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(), } } } diff --git a/src/templater.rs b/src/templater.rs index 78d250646..0a79a86ab 100644 --- a/src/templater.rs +++ b/src/templater.rs @@ -303,6 +303,15 @@ where } } +impl<'a, C: 'a, O> IntoTemplate<'a, C> for Box + 'a> +where + O: Template<()> + 'a, +{ + fn into_template(self) -> Box + 'a> { + Box::new(FormattablePropertyTemplate::new(self)) + } +} + /// Adapter to turn template back to string property. pub struct PlainTextFormattedProperty { template: T,