diff --git a/src/commit_templater.rs b/src/commit_templater.rs index ca05985ca..848896434 100644 --- a/src/commit_templater.rs +++ b/src/commit_templater.rs @@ -76,23 +76,23 @@ impl<'repo> TemplateLanguage<'repo> for CommitTemplateLanguage<'repo, '_> { impl<'repo> CommitTemplateLanguage<'repo, '_> { fn wrap_commit_or_change_id( &self, - property: Box> + 'repo>, + property: impl TemplateProperty> + 'repo, ) -> CommitTemplatePropertyKind<'repo> { - CommitTemplatePropertyKind::CommitOrChangeId(property) + CommitTemplatePropertyKind::CommitOrChangeId(Box::new(property)) } fn wrap_commit_or_change_id_list( &self, - property: Box>> + 'repo>, + property: impl TemplateProperty>> + 'repo, ) -> CommitTemplatePropertyKind<'repo> { - CommitTemplatePropertyKind::CommitOrChangeIdList(property) + CommitTemplatePropertyKind::CommitOrChangeIdList(Box::new(property)) } fn wrap_shortest_id_prefix( &self, - property: Box + 'repo>, + property: impl TemplateProperty + 'repo, ) -> CommitTemplatePropertyKind<'repo> { - CommitTemplatePropertyKind::ShortestIdPrefix(property) + CommitTemplatePropertyKind::ShortestIdPrefix(Box::new(property)) } } @@ -145,15 +145,13 @@ fn build_commit_keyword<'repo>( name: &str, span: pest::Span, ) -> TemplateParseResult> { - fn wrap_fn<'repo, O>( - f: impl Fn(&Commit) -> O + 'repo, - ) -> Box + 'repo> { - Box::new(TemplatePropertyFn(f)) + fn wrap_fn O>(f: F) -> TemplatePropertyFn { + TemplatePropertyFn(f) } fn wrap_repo_fn<'repo, O>( repo: &'repo dyn Repo, f: impl Fn(&dyn Repo, &Commit) -> O + 'repo, - ) -> Box + 'repo> { + ) -> impl TemplateProperty + 'repo { wrap_fn(move |commit| f(repo, commit)) } @@ -370,17 +368,17 @@ fn build_commit_or_change_id_method<'repo>( let property = match function.name { "short" => { let len_property = parse_optional_integer(function)?; - language.wrap_string(Box::new(TemplateFunction::new( + language.wrap_string(TemplateFunction::new( (self_property, len_property), |(id, len)| id.short(len.and_then(|l| l.try_into().ok()).unwrap_or(12)), - ))) + )) } "shortest" => { let len_property = parse_optional_integer(function)?; - language.wrap_shortest_id_prefix(Box::new(TemplateFunction::new( + language.wrap_shortest_id_prefix(TemplateFunction::new( (self_property, len_property), |(id, len)| id.shortest(len.and_then(|l| l.try_into().ok()).unwrap_or(0)), - ))) + )) } _ => { return Err(TemplateParseError::no_such_method( @@ -427,25 +425,21 @@ fn build_shortest_id_prefix_method<'repo>( let property = match function.name { "prefix" => { template_parser::expect_no_arguments(function)?; - language.wrap_string(Box::new(TemplateFunction::new(self_property, |id| { - id.prefix - }))) + language.wrap_string(TemplateFunction::new(self_property, |id| id.prefix)) } "rest" => { template_parser::expect_no_arguments(function)?; - language.wrap_string(Box::new(TemplateFunction::new(self_property, |id| id.rest))) + language.wrap_string(TemplateFunction::new(self_property, |id| id.rest)) } "upper" => { template_parser::expect_no_arguments(function)?; - language.wrap_shortest_id_prefix(Box::new(TemplateFunction::new(self_property, |id| { - id.to_upper() - }))) + language + .wrap_shortest_id_prefix(TemplateFunction::new(self_property, |id| id.to_upper())) } "lower" => { template_parser::expect_no_arguments(function)?; - language.wrap_shortest_id_prefix(Box::new(TemplateFunction::new(self_property, |id| { - id.to_lower() - }))) + language + .wrap_shortest_id_prefix(TemplateFunction::new(self_property, |id| id.to_lower())) } _ => { return Err(TemplateParseError::no_such_method( diff --git a/src/operation_templater.rs b/src/operation_templater.rs index 37b39b12e..24f56185a 100644 --- a/src/operation_templater.rs +++ b/src/operation_templater.rs @@ -62,9 +62,9 @@ impl TemplateLanguage<'static> for OperationTemplateLanguage<'_> { impl OperationTemplateLanguage<'_> { fn wrap_operation_id( &self, - property: Box>, + property: impl TemplateProperty + 'static, ) -> OperationTemplatePropertyKind { - OperationTemplatePropertyKind::OperationId(property) + OperationTemplatePropertyKind::OperationId(Box::new(property)) } } @@ -110,14 +110,12 @@ fn build_operation_keyword( name: &str, span: pest::Span, ) -> TemplateParseResult { - fn wrap_fn( - f: impl Fn(&Operation) -> O + 'static, - ) -> Box> { - Box::new(TemplatePropertyFn(f)) + fn wrap_fn O>(f: F) -> TemplatePropertyFn { + TemplatePropertyFn(f) } fn wrap_metadata_fn( f: impl Fn(&OperationMetadata) -> O + 'static, - ) -> Box> { + ) -> impl TemplateProperty { wrap_fn(move |op| f(&op.store_operation().metadata)) } @@ -168,14 +166,14 @@ fn build_operation_id_method( let len_property = len_node .map(|node| template_parser::expect_integer_expression(language, node)) .transpose()?; - language.wrap_string(Box::new(TemplateFunction::new( + language.wrap_string(TemplateFunction::new( (self_property, len_property), |(id, len)| { let mut hex = id.hex(); hex.truncate(len.and_then(|l| l.try_into().ok()).unwrap_or(12)); hex }, - ))) + )) } _ => return Err(TemplateParseError::no_such_method("OperationId", function)), }; diff --git a/src/template_parser.rs b/src/template_parser.rs index 7b44b425b..673fd7c9d 100644 --- a/src/template_parser.rs +++ b/src/template_parser.rs @@ -594,27 +594,27 @@ pub trait TemplateLanguage<'a> { fn wrap_string( &self, - property: Box + 'a>, + property: impl TemplateProperty + 'a, ) -> Self::Property; fn wrap_boolean( &self, - property: Box + 'a>, + property: impl TemplateProperty + 'a, ) -> Self::Property; fn wrap_integer( &self, - property: Box + 'a>, + property: impl TemplateProperty + 'a, ) -> Self::Property; fn wrap_signature( &self, - property: Box + 'a>, + property: impl TemplateProperty + 'a, ) -> Self::Property; fn wrap_timestamp( &self, - property: Box + 'a>, + property: impl TemplateProperty + 'a, ) -> Self::Property; fn wrap_timestamp_range( &self, - property: Box + 'a>, + property: impl TemplateProperty + 'a, ) -> Self::Property; fn build_keyword(&self, name: &str, span: pest::Span) -> TemplateParseResult; @@ -652,12 +652,11 @@ macro_rules! impl_wrap_property_fns { $( fn $func( &self, - property: Box< - dyn $crate::templater::TemplateProperty + $a - >, + property: impl $crate::templater::TemplateProperty< + Self::Context, Output = $ty> + $a, ) -> Self::Property { use $kind as Kind; // https://github.com/rust-lang/rust/issues/48067 - $outer(Kind::$var(property)) + $outer(Kind::$var(Box::new(property))) } )+ }; @@ -885,28 +884,24 @@ fn build_string_method<'a, L: TemplateLanguage<'a>>( let [needle_node] = expect_exact_arguments(function)?; // TODO: or .try_into_string() to disable implicit type cast? let needle_property = build_expression(language, needle_node)?.into_plain_text(); - language.wrap_boolean(Box::new(TemplateFunction::new( + language.wrap_boolean(TemplateFunction::new( (self_property, needle_property), |(haystack, needle)| haystack.contains(&needle), - ))) + )) } "first_line" => { expect_no_arguments(function)?; - language.wrap_string(Box::new(TemplateFunction::new(self_property, |s| { + language.wrap_string(TemplateFunction::new(self_property, |s| { s.lines().next().unwrap_or_default().to_string() - }))) + })) } "upper" => { expect_no_arguments(function)?; - language.wrap_string(Box::new(TemplateFunction::new(self_property, |s| { - s.to_uppercase() - }))) + language.wrap_string(TemplateFunction::new(self_property, |s| s.to_uppercase())) } "lower" => { expect_no_arguments(function)?; - language.wrap_string(Box::new(TemplateFunction::new(self_property, |s| { - s.to_lowercase() - }))) + language.wrap_string(TemplateFunction::new(self_property, |s| s.to_lowercase())) } _ => return Err(TemplateParseError::no_such_method("String", function)), }; @@ -937,34 +932,28 @@ fn build_signature_method<'a, L: TemplateLanguage<'a>>( let property = match function.name { "name" => { expect_no_arguments(function)?; - language.wrap_string(Box::new(TemplateFunction::new( - self_property, - |signature| signature.name, - ))) + language.wrap_string(TemplateFunction::new(self_property, |signature| { + signature.name + })) } "email" => { expect_no_arguments(function)?; - language.wrap_string(Box::new(TemplateFunction::new( - self_property, - |signature| signature.email, - ))) + language.wrap_string(TemplateFunction::new(self_property, |signature| { + signature.email + })) } "username" => { expect_no_arguments(function)?; - language.wrap_string(Box::new(TemplateFunction::new( - self_property, - |signature| { - let (username, _) = split_email(&signature.email); - username.to_owned() - }, - ))) + language.wrap_string(TemplateFunction::new(self_property, |signature| { + let (username, _) = split_email(&signature.email); + username.to_owned() + })) } "timestamp" => { expect_no_arguments(function)?; - language.wrap_timestamp(Box::new(TemplateFunction::new( - self_property, - |signature| signature.timestamp, - ))) + language.wrap_timestamp(TemplateFunction::new(self_property, |signature| { + signature.timestamp + })) } _ => return Err(TemplateParseError::no_such_method("Signature", function)), }; @@ -979,10 +968,9 @@ fn build_timestamp_method<'a, L: TemplateLanguage<'a>>( let property = match function.name { "ago" => { expect_no_arguments(function)?; - language.wrap_string(Box::new(TemplateFunction::new( - self_property, - |timestamp| time_util::format_timestamp_relative_to_now(×tamp), - ))) + language.wrap_string(TemplateFunction::new(self_property, |timestamp| { + time_util::format_timestamp_relative_to_now(×tamp) + })) } _ => return Err(TemplateParseError::no_such_method("Timestamp", function)), }; @@ -997,24 +985,21 @@ fn build_timestamp_range_method<'a, L: TemplateLanguage<'a>>( let property = match function.name { "start" => { expect_no_arguments(function)?; - language.wrap_timestamp(Box::new(TemplateFunction::new( - self_property, - |time_range| time_range.start, - ))) + language.wrap_timestamp(TemplateFunction::new(self_property, |time_range| { + time_range.start + })) } "end" => { expect_no_arguments(function)?; - language.wrap_timestamp(Box::new(TemplateFunction::new( - self_property, - |time_range| time_range.end, - ))) + language.wrap_timestamp(TemplateFunction::new(self_property, |time_range| { + time_range.end + })) } "duration" => { expect_no_arguments(function)?; - language.wrap_string(Box::new(TemplateFunction::new( - self_property, - |time_range| time_range.duration(), - ))) + language.wrap_string(TemplateFunction::new(self_property, |time_range| { + time_range.duration() + })) } _ => { return Err(TemplateParseError::no_such_method( @@ -1108,11 +1093,11 @@ pub fn build_expression<'a, L: TemplateLanguage<'a>>( Ok(Expression::Property(property, labels)) } ExpressionKind::Integer(value) => { - let property = language.wrap_integer(Box::new(Literal(*value))); + let property = language.wrap_integer(Literal(*value)); Ok(Expression::Property(property, vec![])) } ExpressionKind::String(value) => { - let property = language.wrap_string(Box::new(Literal(value.clone()))); + let property = language.wrap_string(Literal(value.clone())); Ok(Expression::Property(property, vec![])) } ExpressionKind::List(nodes) => {