mirror of
https://github.com/martinvonz/jj.git
synced 2025-01-18 10:07:28 +00:00
templater: move Box::new() wrapping to language.wrap_() functions
Now all callers do wrap_<type>(Box::new(...)), so its responsibility can be moved to the callee without extra boxing.
This commit is contained in:
parent
96f4d8798c
commit
6af265a388
3 changed files with 68 additions and 91 deletions
|
@ -76,23 +76,23 @@ impl<'repo> TemplateLanguage<'repo> for CommitTemplateLanguage<'repo, '_> {
|
|||
impl<'repo> CommitTemplateLanguage<'repo, '_> {
|
||||
fn wrap_commit_or_change_id(
|
||||
&self,
|
||||
property: Box<dyn TemplateProperty<Commit, Output = CommitOrChangeId<'repo>> + 'repo>,
|
||||
property: impl TemplateProperty<Commit, Output = CommitOrChangeId<'repo>> + 'repo,
|
||||
) -> CommitTemplatePropertyKind<'repo> {
|
||||
CommitTemplatePropertyKind::CommitOrChangeId(property)
|
||||
CommitTemplatePropertyKind::CommitOrChangeId(Box::new(property))
|
||||
}
|
||||
|
||||
fn wrap_commit_or_change_id_list(
|
||||
&self,
|
||||
property: Box<dyn TemplateProperty<Commit, Output = Vec<CommitOrChangeId<'repo>>> + 'repo>,
|
||||
property: impl TemplateProperty<Commit, Output = Vec<CommitOrChangeId<'repo>>> + 'repo,
|
||||
) -> CommitTemplatePropertyKind<'repo> {
|
||||
CommitTemplatePropertyKind::CommitOrChangeIdList(property)
|
||||
CommitTemplatePropertyKind::CommitOrChangeIdList(Box::new(property))
|
||||
}
|
||||
|
||||
fn wrap_shortest_id_prefix(
|
||||
&self,
|
||||
property: Box<dyn TemplateProperty<Commit, Output = ShortestIdPrefix> + 'repo>,
|
||||
property: impl TemplateProperty<Commit, Output = ShortestIdPrefix> + '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<CommitTemplatePropertyKind<'repo>> {
|
||||
fn wrap_fn<'repo, O>(
|
||||
f: impl Fn(&Commit) -> O + 'repo,
|
||||
) -> Box<dyn TemplateProperty<Commit, Output = O> + 'repo> {
|
||||
Box::new(TemplatePropertyFn(f))
|
||||
fn wrap_fn<O, F: Fn(&Commit) -> O>(f: F) -> TemplatePropertyFn<F> {
|
||||
TemplatePropertyFn(f)
|
||||
}
|
||||
fn wrap_repo_fn<'repo, O>(
|
||||
repo: &'repo dyn Repo,
|
||||
f: impl Fn(&dyn Repo, &Commit) -> O + 'repo,
|
||||
) -> Box<dyn TemplateProperty<Commit, Output = O> + 'repo> {
|
||||
) -> impl TemplateProperty<Commit, Output = O> + '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(
|
||||
|
|
|
@ -62,9 +62,9 @@ impl TemplateLanguage<'static> for OperationTemplateLanguage<'_> {
|
|||
impl OperationTemplateLanguage<'_> {
|
||||
fn wrap_operation_id(
|
||||
&self,
|
||||
property: Box<dyn TemplateProperty<Operation, Output = OperationId>>,
|
||||
property: impl TemplateProperty<Operation, Output = OperationId> + 'static,
|
||||
) -> OperationTemplatePropertyKind {
|
||||
OperationTemplatePropertyKind::OperationId(property)
|
||||
OperationTemplatePropertyKind::OperationId(Box::new(property))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -110,14 +110,12 @@ fn build_operation_keyword(
|
|||
name: &str,
|
||||
span: pest::Span,
|
||||
) -> TemplateParseResult<OperationTemplatePropertyKind> {
|
||||
fn wrap_fn<O>(
|
||||
f: impl Fn(&Operation) -> O + 'static,
|
||||
) -> Box<dyn TemplateProperty<Operation, Output = O>> {
|
||||
Box::new(TemplatePropertyFn(f))
|
||||
fn wrap_fn<O, F: Fn(&Operation) -> O>(f: F) -> TemplatePropertyFn<F> {
|
||||
TemplatePropertyFn(f)
|
||||
}
|
||||
fn wrap_metadata_fn<O>(
|
||||
f: impl Fn(&OperationMetadata) -> O + 'static,
|
||||
) -> Box<dyn TemplateProperty<Operation, Output = O>> {
|
||||
) -> impl TemplateProperty<Operation, Output = O> {
|
||||
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)),
|
||||
};
|
||||
|
|
|
@ -594,27 +594,27 @@ pub trait TemplateLanguage<'a> {
|
|||
|
||||
fn wrap_string(
|
||||
&self,
|
||||
property: Box<dyn TemplateProperty<Self::Context, Output = String> + 'a>,
|
||||
property: impl TemplateProperty<Self::Context, Output = String> + 'a,
|
||||
) -> Self::Property;
|
||||
fn wrap_boolean(
|
||||
&self,
|
||||
property: Box<dyn TemplateProperty<Self::Context, Output = bool> + 'a>,
|
||||
property: impl TemplateProperty<Self::Context, Output = bool> + 'a,
|
||||
) -> Self::Property;
|
||||
fn wrap_integer(
|
||||
&self,
|
||||
property: Box<dyn TemplateProperty<Self::Context, Output = i64> + 'a>,
|
||||
property: impl TemplateProperty<Self::Context, Output = i64> + 'a,
|
||||
) -> Self::Property;
|
||||
fn wrap_signature(
|
||||
&self,
|
||||
property: Box<dyn TemplateProperty<Self::Context, Output = Signature> + 'a>,
|
||||
property: impl TemplateProperty<Self::Context, Output = Signature> + 'a,
|
||||
) -> Self::Property;
|
||||
fn wrap_timestamp(
|
||||
&self,
|
||||
property: Box<dyn TemplateProperty<Self::Context, Output = Timestamp> + 'a>,
|
||||
property: impl TemplateProperty<Self::Context, Output = Timestamp> + 'a,
|
||||
) -> Self::Property;
|
||||
fn wrap_timestamp_range(
|
||||
&self,
|
||||
property: Box<dyn TemplateProperty<Self::Context, Output = TimestampRange> + 'a>,
|
||||
property: impl TemplateProperty<Self::Context, Output = TimestampRange> + 'a,
|
||||
) -> Self::Property;
|
||||
|
||||
fn build_keyword(&self, name: &str, span: pest::Span) -> TemplateParseResult<Self::Property>;
|
||||
|
@ -652,12 +652,11 @@ macro_rules! impl_wrap_property_fns {
|
|||
$(
|
||||
fn $func(
|
||||
&self,
|
||||
property: Box<
|
||||
dyn $crate::templater::TemplateProperty<Self::Context, Output = $ty> + $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) => {
|
||||
|
|
Loading…
Reference in a new issue