Skip to content

Commit

Permalink
templater: add IntoTemplateProperty::try_into_cmp
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 12, 2024
1 parent 2504703 commit 48233a1
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
22 changes: 22 additions & 0 deletions cli/src/commit_templater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use std::any::Any;
use std::cmp::max;
use std::cmp::Ordering;
use std::collections::HashMap;
use std::io;
use std::rc::Rc;
Expand Down Expand Up @@ -427,6 +428,27 @@ impl<'repo> IntoTemplateProperty<'repo> for CommitTemplatePropertyKind<'repo> {
(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`.
Expand Down
14 changes: 14 additions & 0 deletions cli/src/generic_templater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::cmp::Ordering;
use std::collections::HashMap;

use crate::template_builder;
Expand Down Expand Up @@ -177,6 +178,19 @@ impl<'a, C: 'a> IntoTemplateProperty<'a> for GenericTemplatePropertyKind<'a, C>
(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
Expand Down
13 changes: 13 additions & 0 deletions cli/src/operation_templater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

use std::any::Any;
use std::cmp::Ordering;
use std::collections::HashMap;
use std::io;

Expand Down Expand Up @@ -200,6 +201,18 @@ impl IntoTemplateProperty<'static> for OperationTemplatePropertyKind {
(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`.
Expand Down
34 changes: 34 additions & 0 deletions cli/src/template_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::cmp::Ordering;
use std::collections::HashMap;
use std::io;

Expand Down Expand Up @@ -165,6 +166,10 @@ 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 an [`Ordering`].
fn try_into_cmp(self, other: Self)
-> Option<Box<dyn TemplateProperty<Output = Ordering> + 'a>>;
}

pub enum CoreTemplatePropertyKind<'a> {
Expand Down Expand Up @@ -294,6 +299,28 @@ impl<'a> IntoTemplateProperty<'a> for CoreTemplatePropertyKind<'a> {
(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.
Expand Down Expand Up @@ -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>> {
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> {
Expand Down

0 comments on commit 48233a1

Please sign in to comment.