From c434046211544ee8d81b095cf4a4067201acd22a Mon Sep 17 00:00:00 2001 From: Benjamin Tan Date: Tue, 10 Dec 2024 14:28:23 +0800 Subject: [PATCH] templater: add `IntoTemplateProperty::try_into_{gt,lt}` methods These methods will be used to add `>`, `>=`, `<`, and `<=` operators to the template language. --- cli/src/commit_templater.rs | 36 +++++++++++++++++++++++ cli/src/generic_templater.rs | 20 +++++++++++++ cli/src/operation_templater.rs | 24 ++++++++++++++++ cli/src/template_builder.rs | 52 ++++++++++++++++++++++++++++++++++ 4 files changed, 132 insertions(+) diff --git a/cli/src/commit_templater.rs b/cli/src/commit_templater.rs index 7b6b542382..0a19d8b10b 100644 --- a/cli/src/commit_templater.rs +++ b/cli/src/commit_templater.rs @@ -427,6 +427,42 @@ impl<'repo> IntoTemplateProperty<'repo> for CommitTemplatePropertyKind<'repo> { (CommitTemplatePropertyKind::TreeDiff(_), _) => None, } } + + fn try_into_gt(self, other: Self) -> Option + 'repo>> { + match (self, other) { + (CommitTemplatePropertyKind::Core(lhs), CommitTemplatePropertyKind::Core(rhs)) => { + lhs.try_into_gt(rhs) + } + (CommitTemplatePropertyKind::Core(_), _) => None, + (CommitTemplatePropertyKind::Commit(_), _) => None, + (CommitTemplatePropertyKind::CommitOpt(_), _) => None, + (CommitTemplatePropertyKind::CommitList(_), _) => None, + (CommitTemplatePropertyKind::RefName(_), _) => None, + (CommitTemplatePropertyKind::RefNameOpt(_), _) => None, + (CommitTemplatePropertyKind::RefNameList(_), _) => None, + (CommitTemplatePropertyKind::CommitOrChangeId(_), _) => None, + (CommitTemplatePropertyKind::ShortestIdPrefix(_), _) => None, + (CommitTemplatePropertyKind::TreeDiff(_), _) => None, + } + } + + fn try_into_lt(self, other: Self) -> Option + 'repo>> { + match (self, other) { + (CommitTemplatePropertyKind::Core(lhs), CommitTemplatePropertyKind::Core(rhs)) => { + lhs.try_into_lt(rhs) + } + (CommitTemplatePropertyKind::Core(_), _) => None, + (CommitTemplatePropertyKind::Commit(_), _) => None, + (CommitTemplatePropertyKind::CommitOpt(_), _) => None, + (CommitTemplatePropertyKind::CommitList(_), _) => None, + (CommitTemplatePropertyKind::RefName(_), _) => None, + (CommitTemplatePropertyKind::RefNameOpt(_), _) => None, + (CommitTemplatePropertyKind::RefNameList(_), _) => None, + (CommitTemplatePropertyKind::CommitOrChangeId(_), _) => None, + (CommitTemplatePropertyKind::ShortestIdPrefix(_), _) => None, + (CommitTemplatePropertyKind::TreeDiff(_), _) => None, + } + } } /// Table of functions that translate method call node of self type `T`. diff --git a/cli/src/generic_templater.rs b/cli/src/generic_templater.rs index 7c40dca978..fba59763f4 100644 --- a/cli/src/generic_templater.rs +++ b/cli/src/generic_templater.rs @@ -177,6 +177,26 @@ impl<'a, C: 'a> IntoTemplateProperty<'a> for GenericTemplatePropertyKind<'a, C> (GenericTemplatePropertyKind::Self_(_), _) => None, } } + + fn try_into_gt(self, other: Self) -> Option + 'a>> { + match (self, other) { + (GenericTemplatePropertyKind::Core(lhs), GenericTemplatePropertyKind::Core(rhs)) => { + lhs.try_into_gt(rhs) + } + (GenericTemplatePropertyKind::Core(_), _) => None, + (GenericTemplatePropertyKind::Self_(_), _) => None, + } + } + + fn try_into_lt(self, other: Self) -> Option + 'a>> { + match (self, other) { + (GenericTemplatePropertyKind::Core(lhs), GenericTemplatePropertyKind::Core(rhs)) => { + lhs.try_into_lt(rhs) + } + (GenericTemplatePropertyKind::Core(_), _) => None, + (GenericTemplatePropertyKind::Self_(_), _) => None, + } + } } /// Function that translates keyword (or 0-ary method call node of the self type diff --git a/cli/src/operation_templater.rs b/cli/src/operation_templater.rs index 8e23c7d57e..7c264f6b46 100644 --- a/cli/src/operation_templater.rs +++ b/cli/src/operation_templater.rs @@ -200,6 +200,30 @@ impl IntoTemplateProperty<'static> for OperationTemplatePropertyKind { (OperationTemplatePropertyKind::OperationId(_), _) => None, } } + + fn try_into_gt(self, other: Self) -> Option>> { + match (self, other) { + ( + OperationTemplatePropertyKind::Core(lhs), + OperationTemplatePropertyKind::Core(rhs), + ) => lhs.try_into_gt(rhs), + (OperationTemplatePropertyKind::Core(_), _) => None, + (OperationTemplatePropertyKind::Operation(_), _) => None, + (OperationTemplatePropertyKind::OperationId(_), _) => None, + } + } + + fn try_into_lt(self, other: Self) -> Option>> { + match (self, other) { + ( + OperationTemplatePropertyKind::Core(lhs), + OperationTemplatePropertyKind::Core(rhs), + ) => lhs.try_into_lt(rhs), + (OperationTemplatePropertyKind::Core(_), _) => None, + (OperationTemplatePropertyKind::Operation(_), _) => None, + (OperationTemplatePropertyKind::OperationId(_), _) => None, + } + } } /// Table of functions that translate method call node of self type `T`. diff --git a/cli/src/template_builder.rs b/cli/src/template_builder.rs index 0b0246de8f..1178e9ac67 100644 --- a/cli/src/template_builder.rs +++ b/cli/src/template_builder.rs @@ -165,6 +165,12 @@ pub trait IntoTemplateProperty<'a> { /// Transforms into a property that will evaluate to `self == other`. fn try_into_eq(self, other: Self) -> Option + 'a>>; + + /// Transforms into a property that will evaluate to `self > other`. + fn try_into_gt(self, other: Self) -> Option + 'a>>; + + /// Transforms into a property that will evaluate to `self < other`. + fn try_into_lt(self, other: Self) -> Option + 'a>>; } pub enum CoreTemplatePropertyKind<'a> { @@ -294,6 +300,44 @@ impl<'a> IntoTemplateProperty<'a> for CoreTemplatePropertyKind<'a> { (CoreTemplatePropertyKind::ListTemplate(_), _) => None, } } + + fn try_into_gt(self, other: Self) -> Option + 'a>> { + match (self, other) { + (CoreTemplatePropertyKind::Integer(lhs), CoreTemplatePropertyKind::Integer(rhs)) => { + Some(Box::new((lhs, rhs).map(|(l, r)| l > r))) + } + (CoreTemplatePropertyKind::String(_), _) => None, + (CoreTemplatePropertyKind::StringList(_), _) => None, + (CoreTemplatePropertyKind::Boolean(_), _) => None, + (CoreTemplatePropertyKind::Integer(_), _) => None, + (CoreTemplatePropertyKind::IntegerOpt(_), _) => None, + (CoreTemplatePropertyKind::Signature(_), _) => None, + (CoreTemplatePropertyKind::SizeHint(_), _) => None, + (CoreTemplatePropertyKind::Timestamp(_), _) => None, + (CoreTemplatePropertyKind::TimestampRange(_), _) => None, + (CoreTemplatePropertyKind::Template(_), _) => None, + (CoreTemplatePropertyKind::ListTemplate(_), _) => None, + } + } + + fn try_into_lt(self, other: Self) -> Option + 'a>> { + match (self, other) { + (CoreTemplatePropertyKind::Integer(lhs), CoreTemplatePropertyKind::Integer(rhs)) => { + Some(Box::new((lhs, rhs).map(|(l, r)| l < r))) + } + (CoreTemplatePropertyKind::String(_), _) => None, + (CoreTemplatePropertyKind::StringList(_), _) => None, + (CoreTemplatePropertyKind::Boolean(_), _) => None, + (CoreTemplatePropertyKind::Integer(_), _) => None, + (CoreTemplatePropertyKind::IntegerOpt(_), _) => None, + (CoreTemplatePropertyKind::Signature(_), _) => None, + (CoreTemplatePropertyKind::SizeHint(_), _) => None, + (CoreTemplatePropertyKind::Timestamp(_), _) => None, + (CoreTemplatePropertyKind::TimestampRange(_), _) => None, + (CoreTemplatePropertyKind::Template(_), _) => None, + (CoreTemplatePropertyKind::ListTemplate(_), _) => None, + } + } } /// Function that translates global function call node. @@ -540,6 +584,14 @@ impl<'a, P: IntoTemplateProperty<'a>> Expression

{ pub fn try_into_eq(self, other: Self) -> Option + 'a>> { self.property.try_into_eq(other.property) } + + pub fn try_into_gt(self, other: Self) -> Option + 'a>> { + self.property.try_into_gt(other.property) + } + + pub fn try_into_lt(self, other: Self) -> Option + 'a>> { + self.property.try_into_lt(other.property) + } } pub struct BuildContext<'i, P> {