templater: move non-core wrap_*() functions to property types, leverage macro

Since the return type is now Self, we can reuse impl_wrap_property_fns!() macro
for non-core types.
This commit is contained in:
Yuya Nishihara 2025-04-30 13:26:31 +09:00
parent 7643364a76
commit b611c313aa
4 changed files with 72 additions and 24 deletions

View File

@ -359,118 +359,119 @@ impl<'repo> CommitTemplateLanguage<'repo> {
self.cache_extensions.get::<T>() self.cache_extensions.get::<T>()
} }
// TODO: delete
pub fn wrap_commit( pub fn wrap_commit(
property: BoxedTemplateProperty<'repo, Commit>, property: BoxedTemplateProperty<'repo, Commit>,
) -> CommitTemplatePropertyKind<'repo> { ) -> CommitTemplatePropertyKind<'repo> {
CommitTemplatePropertyKind::Commit(property) CommitTemplatePropertyKind::wrap_commit(property)
} }
pub fn wrap_commit_opt( pub fn wrap_commit_opt(
property: BoxedTemplateProperty<'repo, Option<Commit>>, property: BoxedTemplateProperty<'repo, Option<Commit>>,
) -> CommitTemplatePropertyKind<'repo> { ) -> CommitTemplatePropertyKind<'repo> {
CommitTemplatePropertyKind::CommitOpt(property) CommitTemplatePropertyKind::wrap_commit_opt(property)
} }
pub fn wrap_commit_list( pub fn wrap_commit_list(
property: BoxedTemplateProperty<'repo, Vec<Commit>>, property: BoxedTemplateProperty<'repo, Vec<Commit>>,
) -> CommitTemplatePropertyKind<'repo> { ) -> CommitTemplatePropertyKind<'repo> {
CommitTemplatePropertyKind::CommitList(property) CommitTemplatePropertyKind::wrap_commit_list(property)
} }
pub fn wrap_commit_ref( pub fn wrap_commit_ref(
property: BoxedTemplateProperty<'repo, Rc<CommitRef>>, property: BoxedTemplateProperty<'repo, Rc<CommitRef>>,
) -> CommitTemplatePropertyKind<'repo> { ) -> CommitTemplatePropertyKind<'repo> {
CommitTemplatePropertyKind::CommitRef(property) CommitTemplatePropertyKind::wrap_commit_ref(property)
} }
pub fn wrap_commit_ref_opt( pub fn wrap_commit_ref_opt(
property: BoxedTemplateProperty<'repo, Option<Rc<CommitRef>>>, property: BoxedTemplateProperty<'repo, Option<Rc<CommitRef>>>,
) -> CommitTemplatePropertyKind<'repo> { ) -> CommitTemplatePropertyKind<'repo> {
CommitTemplatePropertyKind::CommitRefOpt(property) CommitTemplatePropertyKind::wrap_commit_ref_opt(property)
} }
pub fn wrap_commit_ref_list( pub fn wrap_commit_ref_list(
property: BoxedTemplateProperty<'repo, Vec<Rc<CommitRef>>>, property: BoxedTemplateProperty<'repo, Vec<Rc<CommitRef>>>,
) -> CommitTemplatePropertyKind<'repo> { ) -> CommitTemplatePropertyKind<'repo> {
CommitTemplatePropertyKind::CommitRefList(property) CommitTemplatePropertyKind::wrap_commit_ref_list(property)
} }
pub fn wrap_repo_path( pub fn wrap_repo_path(
property: BoxedTemplateProperty<'repo, RepoPathBuf>, property: BoxedTemplateProperty<'repo, RepoPathBuf>,
) -> CommitTemplatePropertyKind<'repo> { ) -> CommitTemplatePropertyKind<'repo> {
CommitTemplatePropertyKind::RepoPath(property) CommitTemplatePropertyKind::wrap_repo_path(property)
} }
pub fn wrap_repo_path_opt( pub fn wrap_repo_path_opt(
property: BoxedTemplateProperty<'repo, Option<RepoPathBuf>>, property: BoxedTemplateProperty<'repo, Option<RepoPathBuf>>,
) -> CommitTemplatePropertyKind<'repo> { ) -> CommitTemplatePropertyKind<'repo> {
CommitTemplatePropertyKind::RepoPathOpt(property) CommitTemplatePropertyKind::wrap_repo_path_opt(property)
} }
pub fn wrap_commit_or_change_id( pub fn wrap_commit_or_change_id(
property: BoxedTemplateProperty<'repo, CommitOrChangeId>, property: BoxedTemplateProperty<'repo, CommitOrChangeId>,
) -> CommitTemplatePropertyKind<'repo> { ) -> CommitTemplatePropertyKind<'repo> {
CommitTemplatePropertyKind::CommitOrChangeId(property) CommitTemplatePropertyKind::wrap_commit_or_change_id(property)
} }
pub fn wrap_shortest_id_prefix( pub fn wrap_shortest_id_prefix(
property: BoxedTemplateProperty<'repo, ShortestIdPrefix>, property: BoxedTemplateProperty<'repo, ShortestIdPrefix>,
) -> CommitTemplatePropertyKind<'repo> { ) -> CommitTemplatePropertyKind<'repo> {
CommitTemplatePropertyKind::ShortestIdPrefix(property) CommitTemplatePropertyKind::wrap_shortest_id_prefix(property)
} }
pub fn wrap_tree_diff( pub fn wrap_tree_diff(
property: BoxedTemplateProperty<'repo, TreeDiff>, property: BoxedTemplateProperty<'repo, TreeDiff>,
) -> CommitTemplatePropertyKind<'repo> { ) -> CommitTemplatePropertyKind<'repo> {
CommitTemplatePropertyKind::TreeDiff(property) CommitTemplatePropertyKind::wrap_tree_diff(property)
} }
pub fn wrap_tree_diff_entry( pub fn wrap_tree_diff_entry(
property: BoxedTemplateProperty<'repo, TreeDiffEntry>, property: BoxedTemplateProperty<'repo, TreeDiffEntry>,
) -> CommitTemplatePropertyKind<'repo> { ) -> CommitTemplatePropertyKind<'repo> {
CommitTemplatePropertyKind::TreeDiffEntry(property) CommitTemplatePropertyKind::wrap_tree_diff_entry(property)
} }
pub fn wrap_tree_diff_entry_list( pub fn wrap_tree_diff_entry_list(
property: BoxedTemplateProperty<'repo, Vec<TreeDiffEntry>>, property: BoxedTemplateProperty<'repo, Vec<TreeDiffEntry>>,
) -> CommitTemplatePropertyKind<'repo> { ) -> CommitTemplatePropertyKind<'repo> {
CommitTemplatePropertyKind::TreeDiffEntryList(property) CommitTemplatePropertyKind::wrap_tree_diff_entry_list(property)
} }
pub fn wrap_tree_entry( pub fn wrap_tree_entry(
property: BoxedTemplateProperty<'repo, TreeEntry>, property: BoxedTemplateProperty<'repo, TreeEntry>,
) -> CommitTemplatePropertyKind<'repo> { ) -> CommitTemplatePropertyKind<'repo> {
CommitTemplatePropertyKind::TreeEntry(property) CommitTemplatePropertyKind::wrap_tree_entry(property)
} }
pub fn wrap_diff_stats( pub fn wrap_diff_stats(
property: BoxedTemplateProperty<'repo, DiffStatsFormatted<'repo>>, property: BoxedTemplateProperty<'repo, DiffStatsFormatted<'repo>>,
) -> CommitTemplatePropertyKind<'repo> { ) -> CommitTemplatePropertyKind<'repo> {
CommitTemplatePropertyKind::DiffStats(property) CommitTemplatePropertyKind::wrap_diff_stats(property)
} }
fn wrap_cryptographic_signature_opt( fn wrap_cryptographic_signature_opt(
property: BoxedTemplateProperty<'repo, Option<CryptographicSignature>>, property: BoxedTemplateProperty<'repo, Option<CryptographicSignature>>,
) -> CommitTemplatePropertyKind<'repo> { ) -> CommitTemplatePropertyKind<'repo> {
CommitTemplatePropertyKind::CryptographicSignatureOpt(property) CommitTemplatePropertyKind::wrap_cryptographic_signature_opt(property)
} }
pub fn wrap_annotation_line( pub fn wrap_annotation_line(
property: BoxedTemplateProperty<'repo, AnnotationLine>, property: BoxedTemplateProperty<'repo, AnnotationLine>,
) -> CommitTemplatePropertyKind<'repo> { ) -> CommitTemplatePropertyKind<'repo> {
CommitTemplatePropertyKind::AnnotationLine(property) CommitTemplatePropertyKind::wrap_annotation_line(property)
} }
pub fn wrap_trailer( pub fn wrap_trailer(
property: BoxedTemplateProperty<'repo, Trailer>, property: BoxedTemplateProperty<'repo, Trailer>,
) -> CommitTemplatePropertyKind<'repo> { ) -> CommitTemplatePropertyKind<'repo> {
CommitTemplatePropertyKind::Trailer(property) CommitTemplatePropertyKind::wrap_trailer(property)
} }
pub fn wrap_trailer_list( pub fn wrap_trailer_list(
property: BoxedTemplateProperty<'repo, Vec<Trailer>>, property: BoxedTemplateProperty<'repo, Vec<Trailer>>,
) -> CommitTemplatePropertyKind<'repo> { ) -> CommitTemplatePropertyKind<'repo> {
CommitTemplatePropertyKind::TrailerList(property) CommitTemplatePropertyKind::wrap_trailer_list(property)
} }
} }
@ -497,6 +498,32 @@ pub enum CommitTemplatePropertyKind<'repo> {
TrailerList(BoxedTemplateProperty<'repo, Vec<Trailer>>), TrailerList(BoxedTemplateProperty<'repo, Vec<Trailer>>),
} }
impl<'repo> CommitTemplatePropertyKind<'repo> {
template_builder::impl_wrap_property_fns!('repo, CommitTemplatePropertyKind, {
pub wrap_commit(Commit) => Commit,
pub wrap_commit_opt(Option<Commit>) => CommitOpt,
pub wrap_commit_list(Vec<Commit>) => CommitList,
pub wrap_commit_ref(Rc<CommitRef>) => CommitRef,
pub wrap_commit_ref_opt(Option<Rc<CommitRef>>) => CommitRefOpt,
pub wrap_commit_ref_list(Vec<Rc<CommitRef>>) => CommitRefList,
pub wrap_repo_path(RepoPathBuf) => RepoPath,
pub wrap_repo_path_opt(Option<RepoPathBuf>) => RepoPathOpt,
pub wrap_commit_or_change_id(CommitOrChangeId) => CommitOrChangeId,
pub wrap_shortest_id_prefix(ShortestIdPrefix) => ShortestIdPrefix,
pub wrap_tree_diff(TreeDiff) => TreeDiff,
pub wrap_tree_diff_entry(TreeDiffEntry) => TreeDiffEntry,
pub wrap_tree_diff_entry_list(Vec<TreeDiffEntry>) => TreeDiffEntryList,
pub wrap_tree_entry(TreeEntry) => TreeEntry,
pub wrap_diff_stats(DiffStatsFormatted<'repo>) => DiffStats,
pub wrap_cryptographic_signature_opt(
Option<CryptographicSignature>
) => CryptographicSignatureOpt,
pub wrap_annotation_line(AnnotationLine) => AnnotationLine,
pub wrap_trailer(Trailer) => Trailer,
pub wrap_trailer_list(Vec<Trailer>) => TrailerList,
});
}
impl<'repo> CoreTemplatePropertyVar<'repo> for CommitTemplatePropertyKind<'repo> { impl<'repo> CoreTemplatePropertyVar<'repo> for CommitTemplatePropertyKind<'repo> {
template_builder::impl_core_wrap_property_fns!('repo, CommitTemplatePropertyKind::Core); template_builder::impl_core_wrap_property_fns!('repo, CommitTemplatePropertyKind::Core);
} }

View File

@ -130,8 +130,9 @@ impl<'a, C> TemplateLanguage<'a> for GenericTemplateLanguage<'a, C> {
} }
impl<'a, C> GenericTemplateLanguage<'a, C> { impl<'a, C> GenericTemplateLanguage<'a, C> {
// TODO: delete
pub fn wrap_self(property: BoxedTemplateProperty<'a, C>) -> GenericTemplatePropertyKind<'a, C> { pub fn wrap_self(property: BoxedTemplateProperty<'a, C>) -> GenericTemplatePropertyKind<'a, C> {
GenericTemplatePropertyKind::Self_(property) GenericTemplatePropertyKind::wrap_self(property)
} }
} }
@ -140,6 +141,12 @@ pub enum GenericTemplatePropertyKind<'a, C> {
Self_(BoxedTemplateProperty<'a, C>), Self_(BoxedTemplateProperty<'a, C>),
} }
impl<'a, C> GenericTemplatePropertyKind<'a, C> {
template_builder::impl_wrap_property_fns!('a, GenericTemplatePropertyKind, {
pub wrap_self(C) => Self_,
});
}
impl<'a, C> CoreTemplatePropertyVar<'a> for GenericTemplatePropertyKind<'a, C> { impl<'a, C> CoreTemplatePropertyVar<'a> for GenericTemplatePropertyKind<'a, C> {
template_builder::impl_core_wrap_property_fns!('a, GenericTemplatePropertyKind::Core); template_builder::impl_core_wrap_property_fns!('a, GenericTemplatePropertyKind::Core);
} }

View File

@ -135,16 +135,17 @@ impl OperationTemplateLanguage {
self.cache_extensions.get::<T>() self.cache_extensions.get::<T>()
} }
// TODO: delete
pub fn wrap_operation( pub fn wrap_operation(
property: BoxedTemplateProperty<'static, Operation>, property: BoxedTemplateProperty<'static, Operation>,
) -> OperationTemplatePropertyKind { ) -> OperationTemplatePropertyKind {
OperationTemplatePropertyKind::Operation(property) OperationTemplatePropertyKind::wrap_operation(property)
} }
pub fn wrap_operation_id( pub fn wrap_operation_id(
property: BoxedTemplateProperty<'static, OperationId>, property: BoxedTemplateProperty<'static, OperationId>,
) -> OperationTemplatePropertyKind { ) -> OperationTemplatePropertyKind {
OperationTemplatePropertyKind::OperationId(property) OperationTemplatePropertyKind::wrap_operation_id(property)
} }
} }
@ -154,6 +155,13 @@ pub enum OperationTemplatePropertyKind {
OperationId(BoxedTemplateProperty<'static, OperationId>), OperationId(BoxedTemplateProperty<'static, OperationId>),
} }
impl OperationTemplatePropertyKind {
template_builder::impl_wrap_property_fns!('static, OperationTemplatePropertyKind, {
pub wrap_operation(Operation) => Operation,
pub wrap_operation_id(OperationId) => OperationId,
});
}
impl CoreTemplatePropertyVar<'static> for OperationTemplatePropertyKind { impl CoreTemplatePropertyVar<'static> for OperationTemplatePropertyKind {
template_builder::impl_core_wrap_property_fns!('static, OperationTemplatePropertyKind::Core); template_builder::impl_core_wrap_property_fns!('static, OperationTemplatePropertyKind::Core);
} }

View File

@ -175,9 +175,15 @@ macro_rules! impl_core_wrap_property_fns {
} }
macro_rules! impl_wrap_property_fns { macro_rules! impl_wrap_property_fns {
($a:lifetime, $kind:path, $outer:path, { $( $func:ident($ty:ty) => $var:ident, )+ }) => { ($a:lifetime, $kind:path, { $($body:tt)* }) => {
$crate::template_builder::impl_wrap_property_fns!(
$a, $kind, std::convert::identity, { $($body)* });
};
($a:lifetime, $kind:path, $outer:path, {
$( $vis:vis $func:ident($ty:ty) => $var:ident, )+
}) => {
$( $(
fn $func( $vis fn $func(
property: $crate::templater::BoxedTemplateProperty<$a, $ty>, property: $crate::templater::BoxedTemplateProperty<$a, $ty>,
) -> Self { ) -> Self {
use $kind as Kind; // https://github.com/rust-lang/rust/issues/48067 use $kind as Kind; // https://github.com/rust-lang/rust/issues/48067