Skip to content

Commit

Permalink
templater: add IntoTemplateProperty::try_into_{gt,lt} methods
Browse files Browse the repository at this point in the history
These methods will be used to add `>`, `>=`, `<`, and `<=` operators to
the template language.
  • Loading branch information
bnjmnt4n committed Dec 11, 2024
1 parent e1936a2 commit c434046
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 0 deletions.
36 changes: 36 additions & 0 deletions cli/src/commit_templater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,42 @@ impl<'repo> IntoTemplateProperty<'repo> for CommitTemplatePropertyKind<'repo> {
(CommitTemplatePropertyKind::TreeDiff(_), _) => None,
}
}

fn try_into_gt(self, other: Self) -> Option<Box<dyn TemplateProperty<Output = bool> + '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<Box<dyn TemplateProperty<Output = bool> + '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`.
Expand Down
20 changes: 20 additions & 0 deletions cli/src/generic_templater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Box<dyn TemplateProperty<Output = bool> + '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<Box<dyn TemplateProperty<Output = bool> + '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
Expand Down
24 changes: 24 additions & 0 deletions cli/src/operation_templater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,30 @@ impl IntoTemplateProperty<'static> for OperationTemplatePropertyKind {
(OperationTemplatePropertyKind::OperationId(_), _) => None,
}
}

fn try_into_gt(self, other: Self) -> Option<Box<dyn TemplateProperty<Output = bool>>> {
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<Box<dyn TemplateProperty<Output = bool>>> {
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`.
Expand Down
52 changes: 52 additions & 0 deletions cli/src/template_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Box<dyn TemplateProperty<Output = bool> + 'a>>;

/// Transforms into a property that will evaluate to `self > other`.
fn try_into_gt(self, other: Self) -> Option<Box<dyn TemplateProperty<Output = bool> + 'a>>;

/// Transforms into a property that will evaluate to `self < other`.
fn try_into_lt(self, other: Self) -> Option<Box<dyn TemplateProperty<Output = bool> + 'a>>;
}

pub enum CoreTemplatePropertyKind<'a> {
Expand Down Expand Up @@ -294,6 +300,44 @@ impl<'a> IntoTemplateProperty<'a> for CoreTemplatePropertyKind<'a> {
(CoreTemplatePropertyKind::ListTemplate(_), _) => None,
}
}

fn try_into_gt(self, other: Self) -> Option<Box<dyn TemplateProperty<Output = bool> + '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<Box<dyn TemplateProperty<Output = bool> + '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.
Expand Down Expand Up @@ -540,6 +584,14 @@ impl<'a, P: IntoTemplateProperty<'a>> Expression<P> {
pub fn try_into_eq(self, other: Self) -> Option<Box<dyn TemplateProperty<Output = bool> + 'a>> {
self.property.try_into_eq(other.property)
}

pub fn try_into_gt(self, other: Self) -> Option<Box<dyn TemplateProperty<Output = bool> + 'a>> {
self.property.try_into_gt(other.property)
}

pub fn try_into_lt(self, other: Self) -> Option<Box<dyn TemplateProperty<Output = bool> + 'a>> {
self.property.try_into_lt(other.property)
}
}

pub struct BuildContext<'i, P> {
Expand Down

0 comments on commit c434046

Please sign in to comment.