templater: add IntoTemplateProperty::try_into_cmp

These methods will be used to add `>`, `>=`, `<`, and `<=` operators to
the template language.
This commit is contained in:
Benjamin Tan 2024-12-10 14:28:23 +08:00
parent 2504703b0d
commit 48233a1cb7
4 changed files with 83 additions and 0 deletions

View file

@ -14,6 +14,7 @@
use std::any::Any; use std::any::Any;
use std::cmp::max; use std::cmp::max;
use std::cmp::Ordering;
use std::collections::HashMap; use std::collections::HashMap;
use std::io; use std::io;
use std::rc::Rc; use std::rc::Rc;
@ -427,6 +428,27 @@ impl<'repo> IntoTemplateProperty<'repo> for CommitTemplatePropertyKind<'repo> {
(CommitTemplatePropertyKind::TreeDiff(_), _) => None, (CommitTemplatePropertyKind::TreeDiff(_), _) => None,
} }
} }
fn try_into_cmp(
self,
other: Self,
) -> Option<Box<dyn TemplateProperty<Output = Ordering> + 'repo>> {
match (self, other) {
(CommitTemplatePropertyKind::Core(lhs), CommitTemplatePropertyKind::Core(rhs)) => {
lhs.try_into_cmp(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`. /// Table of functions that translate method call node of self type `T`.

View file

@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use std::cmp::Ordering;
use std::collections::HashMap; use std::collections::HashMap;
use crate::template_builder; use crate::template_builder;
@ -177,6 +178,19 @@ impl<'a, C: 'a> IntoTemplateProperty<'a> for GenericTemplatePropertyKind<'a, C>
(GenericTemplatePropertyKind::Self_(_), _) => None, (GenericTemplatePropertyKind::Self_(_), _) => None,
} }
} }
fn try_into_cmp(
self,
other: Self,
) -> Option<Box<dyn TemplateProperty<Output = Ordering> + 'a>> {
match (self, other) {
(GenericTemplatePropertyKind::Core(lhs), GenericTemplatePropertyKind::Core(rhs)) => {
lhs.try_into_cmp(rhs)
}
(GenericTemplatePropertyKind::Core(_), _) => None,
(GenericTemplatePropertyKind::Self_(_), _) => None,
}
}
} }
/// Function that translates keyword (or 0-ary method call node of the self type /// Function that translates keyword (or 0-ary method call node of the self type

View file

@ -13,6 +13,7 @@
// limitations under the License. // limitations under the License.
use std::any::Any; use std::any::Any;
use std::cmp::Ordering;
use std::collections::HashMap; use std::collections::HashMap;
use std::io; use std::io;
@ -200,6 +201,18 @@ impl IntoTemplateProperty<'static> for OperationTemplatePropertyKind {
(OperationTemplatePropertyKind::OperationId(_), _) => None, (OperationTemplatePropertyKind::OperationId(_), _) => None,
} }
} }
fn try_into_cmp(self, other: Self) -> Option<Box<dyn TemplateProperty<Output = Ordering>>> {
match (self, other) {
(
OperationTemplatePropertyKind::Core(lhs),
OperationTemplatePropertyKind::Core(rhs),
) => lhs.try_into_cmp(rhs),
(OperationTemplatePropertyKind::Core(_), _) => None,
(OperationTemplatePropertyKind::Operation(_), _) => None,
(OperationTemplatePropertyKind::OperationId(_), _) => None,
}
}
} }
/// Table of functions that translate method call node of self type `T`. /// Table of functions that translate method call node of self type `T`.

View file

@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use std::cmp::Ordering;
use std::collections::HashMap; use std::collections::HashMap;
use std::io; use std::io;
@ -165,6 +166,10 @@ pub trait IntoTemplateProperty<'a> {
/// Transforms into a property that will evaluate to `self == other`. /// Transforms into a property that will evaluate to `self == other`.
fn try_into_eq(self, other: Self) -> Option<Box<dyn TemplateProperty<Output = bool> + 'a>>; fn try_into_eq(self, other: Self) -> Option<Box<dyn TemplateProperty<Output = bool> + 'a>>;
/// Transforms into a property that will evaluate to an [`Ordering`].
fn try_into_cmp(self, other: Self)
-> Option<Box<dyn TemplateProperty<Output = Ordering> + 'a>>;
} }
pub enum CoreTemplatePropertyKind<'a> { pub enum CoreTemplatePropertyKind<'a> {
@ -294,6 +299,28 @@ impl<'a> IntoTemplateProperty<'a> for CoreTemplatePropertyKind<'a> {
(CoreTemplatePropertyKind::ListTemplate(_), _) => None, (CoreTemplatePropertyKind::ListTemplate(_), _) => None,
} }
} }
fn try_into_cmp(
self,
other: Self,
) -> Option<Box<dyn TemplateProperty<Output = Ordering> + 'a>> {
match (self, other) {
(CoreTemplatePropertyKind::Integer(lhs), CoreTemplatePropertyKind::Integer(rhs)) => {
Some(Box::new((lhs, rhs).map(|(l, r)| l.cmp(&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. /// Function that translates global function call node.
@ -540,6 +567,13 @@ impl<'a, P: IntoTemplateProperty<'a>> Expression<P> {
pub fn try_into_eq(self, other: Self) -> Option<Box<dyn TemplateProperty<Output = bool> + 'a>> { pub fn try_into_eq(self, other: Self) -> Option<Box<dyn TemplateProperty<Output = bool> + 'a>> {
self.property.try_into_eq(other.property) self.property.try_into_eq(other.property)
} }
pub fn try_into_cmp(
self,
other: Self,
) -> Option<Box<dyn TemplateProperty<Output = Ordering> + 'a>> {
self.property.try_into_cmp(other.property)
}
} }
pub struct BuildContext<'i, P> { pub struct BuildContext<'i, P> {