From cf1b609de20024952c35d42d6b0458d7f9c13ba3 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Sun, 19 Feb 2023 11:35:14 +0900 Subject: [PATCH] commit_templater: rename 'repo lifetime for clarity FWIW, I'm thinking of making the repo parameter generic over Arc and &MutableRepo. It will allow us cache a parsed commit_summary template. --- src/commit_templater.rs | 120 ++++++++++++++++++++-------------------- 1 file changed, 60 insertions(+), 60 deletions(-) diff --git a/src/commit_templater.rs b/src/commit_templater.rs index fcd0c2b0f..49f569ef5 100644 --- a/src/commit_templater.rs +++ b/src/commit_templater.rs @@ -34,43 +34,43 @@ use crate::templater::{ TemplatePropertyFn, }; -struct CommitTemplateLanguage<'a, 'b> { - repo: &'a dyn Repo, +struct CommitTemplateLanguage<'repo, 'b> { + repo: &'repo dyn Repo, workspace_id: &'b WorkspaceId, } -impl<'a> TemplateLanguage<'a> for CommitTemplateLanguage<'a, '_> { +impl<'repo> TemplateLanguage<'repo> for CommitTemplateLanguage<'repo, '_> { type Context = Commit; - type Property = CommitTemplatePropertyKind<'a>; + type Property = CommitTemplatePropertyKind<'repo>; // TODO: maybe generate wrap_() by macro? fn wrap_string( &self, - property: Box + 'a>, + property: Box + 'repo>, ) -> Self::Property { CommitTemplatePropertyKind::Core(CoreTemplatePropertyKind::String(property)) } fn wrap_boolean( &self, - property: Box + 'a>, + property: Box + 'repo>, ) -> Self::Property { CommitTemplatePropertyKind::Core(CoreTemplatePropertyKind::Boolean(property)) } fn wrap_integer( &self, - property: Box + 'a>, + property: Box + 'repo>, ) -> Self::Property { CommitTemplatePropertyKind::Core(CoreTemplatePropertyKind::Integer(property)) } fn wrap_signature( &self, - property: Box + 'a>, + property: Box + 'repo>, ) -> Self::Property { CommitTemplatePropertyKind::Core(CoreTemplatePropertyKind::Signature(property)) } fn wrap_timestamp( &self, - property: Box + 'a>, + property: Box + 'repo>, ) -> Self::Property { CommitTemplatePropertyKind::Core(CoreTemplatePropertyKind::Timestamp(property)) } @@ -100,54 +100,54 @@ impl<'a> TemplateLanguage<'a> for CommitTemplateLanguage<'a, '_> { // If we need to add multiple languages that support Commit types, this can be // turned into a trait which extends TemplateLanguage. -impl<'a> CommitTemplateLanguage<'a, '_> { +impl<'repo> CommitTemplateLanguage<'repo, '_> { fn wrap_commit_or_change_id( &self, - property: Box> + 'a>, - ) -> CommitTemplatePropertyKind<'a> { + property: Box> + 'repo>, + ) -> CommitTemplatePropertyKind<'repo> { CommitTemplatePropertyKind::CommitOrChangeId(property) } fn wrap_shortest_id_prefix( &self, - property: Box + 'a>, - ) -> CommitTemplatePropertyKind<'a> { + property: Box + 'repo>, + ) -> CommitTemplatePropertyKind<'repo> { CommitTemplatePropertyKind::ShortestIdPrefix(property) } } -enum CommitTemplatePropertyKind<'a> { - Core(CoreTemplatePropertyKind<'a, Commit>), - CommitOrChangeId(Box> + 'a>), - ShortestIdPrefix(Box + 'a>), +enum CommitTemplatePropertyKind<'repo> { + Core(CoreTemplatePropertyKind<'repo, Commit>), + CommitOrChangeId(Box> + 'repo>), + ShortestIdPrefix(Box + 'repo>), } -impl<'a> IntoTemplateProperty<'a, Commit> for CommitTemplatePropertyKind<'a> { - fn try_into_boolean(self) -> Option + 'a>> { +impl<'repo> IntoTemplateProperty<'repo, Commit> for CommitTemplatePropertyKind<'repo> { + fn try_into_boolean(self) -> Option + 'repo>> { match self { CommitTemplatePropertyKind::Core(property) => property.try_into_boolean(), _ => None, } } - fn try_into_integer(self) -> Option + 'a>> { + fn try_into_integer(self) -> Option + 'repo>> { match self { CommitTemplatePropertyKind::Core(property) => property.try_into_integer(), _ => None, } } - fn into_plain_text(self) -> Box + 'a> { + fn into_plain_text(self) -> Box + 'repo> { match self { CommitTemplatePropertyKind::Core(property) => property.into_plain_text(), _ => Box::new(PlainTextFormattedProperty::new(self.into_template())), } } - fn into_template(self) -> Box + 'a> { - fn wrap<'a, O: Template<()> + 'a>( - property: Box + 'a>, - ) -> Box + 'a> { + fn into_template(self) -> Box + 'repo> { + fn wrap<'repo, O: Template<()> + 'repo>( + property: Box + 'repo>, + ) -> Box + 'repo> { Box::new(FormattablePropertyTemplate::new(property)) } match self { @@ -158,14 +158,14 @@ impl<'a> IntoTemplateProperty<'a, Commit> for CommitTemplatePropertyKind<'a> { } } -fn build_commit_keyword<'a>( - language: &CommitTemplateLanguage<'a, '_>, +fn build_commit_keyword<'repo>( + language: &CommitTemplateLanguage<'repo, '_>, name: &str, span: pest::Span, -) -> TemplateParseResult> { - fn wrap_fn<'a, O>( - f: impl Fn(&Commit) -> O + 'a, - ) -> Box + 'a> { +) -> TemplateParseResult> { + fn wrap_fn<'repo, O>( + f: impl Fn(&Commit) -> O + 'repo, + ) -> Box + 'repo> { Box::new(TemplatePropertyFn(f)) } let repo = language.repo; @@ -206,8 +206,8 @@ fn build_commit_keyword<'a>( Ok(property) } -struct WorkingCopiesProperty<'a> { - pub repo: &'a dyn Repo, +struct WorkingCopiesProperty<'repo> { + pub repo: &'repo dyn Repo, } impl TemplateProperty for WorkingCopiesProperty<'_> { @@ -228,8 +228,8 @@ impl TemplateProperty for WorkingCopiesProperty<'_> { } } -struct BranchProperty<'a> { - pub repo: &'a dyn Repo, +struct BranchProperty<'repo> { + pub repo: &'repo dyn Repo, } impl TemplateProperty for BranchProperty<'_> { @@ -268,8 +268,8 @@ impl TemplateProperty for BranchProperty<'_> { } } -struct TagProperty<'a> { - pub repo: &'a dyn Repo, +struct TagProperty<'repo> { + pub repo: &'repo dyn Repo, } impl TemplateProperty for TagProperty<'_> { @@ -290,8 +290,8 @@ impl TemplateProperty for TagProperty<'_> { } } -struct GitRefsProperty<'a> { - pub repo: &'a dyn Repo, +struct GitRefsProperty<'repo> { + pub repo: &'repo dyn Repo, } impl TemplateProperty for GitRefsProperty<'_> { @@ -314,12 +314,12 @@ impl TemplateProperty for GitRefsProperty<'_> { } } -struct GitHeadProperty<'a> { - repo: &'a dyn Repo, +struct GitHeadProperty<'repo> { + repo: &'repo dyn Repo, } -impl<'a> GitHeadProperty<'a> { - pub fn new(repo: &'a dyn Repo) -> Self { +impl<'repo> GitHeadProperty<'repo> { + pub fn new(repo: &'repo dyn Repo) -> Self { Self { repo } } } @@ -343,14 +343,14 @@ impl TemplateProperty for GitHeadProperty<'_> { /// Type-erased `CommitId`/`ChangeId`. #[derive(Clone)] -struct CommitOrChangeId<'a> { - repo: &'a dyn Repo, +struct CommitOrChangeId<'repo> { + repo: &'repo dyn Repo, id_bytes: Vec, is_commit_id: bool, } -impl<'a> CommitOrChangeId<'a> { - pub fn commit_id(repo: &'a dyn Repo, id: &CommitId) -> Self { +impl<'repo> CommitOrChangeId<'repo> { + pub fn commit_id(repo: &'repo dyn Repo, id: &CommitId) -> Self { CommitOrChangeId { repo, id_bytes: id.to_bytes(), @@ -358,7 +358,7 @@ impl<'a> CommitOrChangeId<'a> { } } - pub fn change_id(repo: &'a dyn Repo, id: &ChangeId) -> Self { + pub fn change_id(repo: &'repo dyn Repo, id: &ChangeId) -> Self { CommitOrChangeId { repo, id_bytes: id.to_bytes(), @@ -410,11 +410,11 @@ impl Template<()> for CommitOrChangeId<'_> { } } -fn build_commit_or_change_id_method<'a>( - language: &CommitTemplateLanguage<'a, '_>, - self_property: impl TemplateProperty> + 'a, +fn build_commit_or_change_id_method<'repo>( + language: &CommitTemplateLanguage<'repo, '_>, + self_property: impl TemplateProperty> + 'repo, function: &FunctionCallNode, -) -> TemplateParseResult> { +) -> TemplateParseResult> { let parse_optional_integer = |function| -> Result, TemplateParseError> { let ([], [len_node]) = template_parser::expect_arguments(function)?; len_node @@ -472,11 +472,11 @@ impl Template<()> for ShortestIdPrefix { } } -fn build_shortest_id_prefix_method<'a>( - language: &CommitTemplateLanguage<'a, '_>, - self_property: impl TemplateProperty + 'a, +fn build_shortest_id_prefix_method<'repo>( + language: &CommitTemplateLanguage<'repo, '_>, + self_property: impl TemplateProperty + 'repo, function: &FunctionCallNode, -) -> TemplateParseResult> { +) -> TemplateParseResult> { let property = match function.name { "prefix" => { template_parser::expect_no_arguments(function)?; @@ -502,12 +502,12 @@ fn build_shortest_id_prefix_method<'a>( Ok(property) } -pub fn parse<'a>( - repo: &'a dyn Repo, +pub fn parse<'repo>( + repo: &'repo dyn Repo, workspace_id: &WorkspaceId, template_text: &str, aliases_map: &TemplateAliasesMap, -) -> TemplateParseResult + 'a>> { +) -> TemplateParseResult + 'repo>> { let language = CommitTemplateLanguage { repo, workspace_id }; let node = template_parser::parse_template(template_text)?; let node = template_parser::expand_aliases(node, aliases_map)?;