Implement more frontend ops (#111)
* middleware operation output statement? * small refactor to op() on frontend * Implement op() * cargo fmt * Clippy * Code review --------- Co-authored-by: Ahmad <root@ahmadafuni.com>
This commit is contained in:
parent
6627b46819
commit
2864ef22d4
5 changed files with 561 additions and 61 deletions
|
|
@ -2,8 +2,8 @@ use std::fmt;
|
|||
|
||||
use anyhow::{anyhow, Result};
|
||||
|
||||
use super::{CustomPredicateRef, Statement};
|
||||
use crate::middleware::{AnchoredKey, Params, Value, SELF};
|
||||
use super::{CustomPredicateRef, NativePredicate, Statement, StatementArg};
|
||||
use crate::middleware::{AnchoredKey, Params, Predicate, Value, SELF};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum OperationType {
|
||||
|
|
@ -25,12 +25,49 @@ pub enum NativeOperation {
|
|||
LtToNotEqual = 9,
|
||||
ContainsFromEntries = 10,
|
||||
NotContainsFromEntries = 11,
|
||||
RenameContainedBy = 12,
|
||||
SumOf = 13,
|
||||
ProductOf = 14,
|
||||
MaxOf = 15,
|
||||
}
|
||||
|
||||
impl OperationType {
|
||||
/// Gives the type of predicate that the operation will output, if known.
|
||||
/// CopyStatement may output any predicate (it will match the statement copied),
|
||||
/// so output_predicate returns None on CopyStatement.
|
||||
pub fn output_predicate(&self) -> Option<Predicate> {
|
||||
match self {
|
||||
OperationType::Native(native_op) => match native_op {
|
||||
NativeOperation::None => Some(Predicate::Native(NativePredicate::None)),
|
||||
NativeOperation::NewEntry => Some(Predicate::Native(NativePredicate::ValueOf)),
|
||||
NativeOperation::CopyStatement => None,
|
||||
NativeOperation::EqualFromEntries => {
|
||||
Some(Predicate::Native(NativePredicate::Equal))
|
||||
}
|
||||
NativeOperation::NotEqualFromEntries => {
|
||||
Some(Predicate::Native(NativePredicate::NotEqual))
|
||||
}
|
||||
NativeOperation::GtFromEntries => Some(Predicate::Native(NativePredicate::Gt)),
|
||||
NativeOperation::LtFromEntries => Some(Predicate::Native(NativePredicate::Lt)),
|
||||
NativeOperation::TransitiveEqualFromStatements => {
|
||||
Some(Predicate::Native(NativePredicate::Equal))
|
||||
}
|
||||
NativeOperation::GtToNotEqual => Some(Predicate::Native(NativePredicate::NotEqual)),
|
||||
NativeOperation::LtToNotEqual => Some(Predicate::Native(NativePredicate::NotEqual)),
|
||||
NativeOperation::ContainsFromEntries => {
|
||||
Some(Predicate::Native(NativePredicate::Contains))
|
||||
}
|
||||
NativeOperation::NotContainsFromEntries => {
|
||||
Some(Predicate::Native(NativePredicate::NotContains))
|
||||
}
|
||||
NativeOperation::SumOf => Some(Predicate::Native(NativePredicate::SumOf)),
|
||||
NativeOperation::ProductOf => Some(Predicate::Native(NativePredicate::ProductOf)),
|
||||
NativeOperation::MaxOf => Some(Predicate::Native(NativePredicate::MaxOf)),
|
||||
},
|
||||
OperationType::Custom(cpr) => Some(Predicate::Custom(cpr.clone())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Refine this enum.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum Operation {
|
||||
|
|
@ -46,7 +83,6 @@ pub enum Operation {
|
|||
LtToNotEqual(Statement),
|
||||
ContainsFromEntries(Statement, Statement),
|
||||
NotContainsFromEntries(Statement, Statement),
|
||||
RenameContainedBy(Statement, Statement),
|
||||
SumOf(Statement, Statement, Statement),
|
||||
ProductOf(Statement, Statement, Statement),
|
||||
MaxOf(Statement, Statement, Statement),
|
||||
|
|
@ -70,7 +106,6 @@ impl Operation {
|
|||
Self::LtToNotEqual(_) => OT::Native(LtToNotEqual),
|
||||
Self::ContainsFromEntries(_, _) => OT::Native(ContainsFromEntries),
|
||||
Self::NotContainsFromEntries(_, _) => OT::Native(NotContainsFromEntries),
|
||||
Self::RenameContainedBy(_, _) => OT::Native(RenameContainedBy),
|
||||
Self::SumOf(_, _, _) => OT::Native(SumOf),
|
||||
Self::ProductOf(_, _, _) => OT::Native(ProductOf),
|
||||
Self::MaxOf(_, _, _) => OT::Native(MaxOf),
|
||||
|
|
@ -92,7 +127,6 @@ impl Operation {
|
|||
Self::LtToNotEqual(s) => vec![s],
|
||||
Self::ContainsFromEntries(s1, s2) => vec![s1, s2],
|
||||
Self::NotContainsFromEntries(s1, s2) => vec![s1, s2],
|
||||
Self::RenameContainedBy(s1, s2) => vec![s1, s2],
|
||||
Self::SumOf(s1, s2, s3) => vec![s1, s2, s3],
|
||||
Self::ProductOf(s1, s2, s3) => vec![s1, s2, s3],
|
||||
Self::MaxOf(s1, s2, s3) => vec![s1, s2, s3],
|
||||
|
|
@ -126,9 +160,6 @@ impl Operation {
|
|||
(NO::NotContainsFromEntries, (Some(s1), Some(s2), None), 2) => {
|
||||
Self::NotContainsFromEntries(s1, s2)
|
||||
}
|
||||
(NO::RenameContainedBy, (Some(s1), Some(s2), None), 2) => {
|
||||
Self::RenameContainedBy(s1, s2)
|
||||
}
|
||||
(NO::SumOf, (Some(s1), Some(s2), Some(s3)), 3) => Self::SumOf(s1, s2, s3),
|
||||
(NO::ProductOf, (Some(s1), Some(s2), Some(s3)), 3) => Self::ProductOf(s1, s2, s3),
|
||||
(NO::MaxOf, (Some(s1), Some(s2), Some(s3)), 3) => Self::MaxOf(s1, s2, s3),
|
||||
|
|
@ -141,6 +172,142 @@ impl Operation {
|
|||
OperationType::Custom(cpr) => Self::Custom(cpr, args.to_vec()),
|
||||
})
|
||||
}
|
||||
/// Gives the output statement of the given operation, where determined
|
||||
/// A ValueOf statement is not determined by the NewEntry operation, so returns Ok(None)
|
||||
/// The outer Result is error handling
|
||||
pub fn output_statement(&self) -> Result<Option<Statement>> {
|
||||
use Statement::*;
|
||||
let pred: Option<Predicate> = self.code().output_predicate();
|
||||
|
||||
let st_args: Option<Vec<StatementArg>> = match self {
|
||||
Self::None => Some(vec![]),
|
||||
Self::NewEntry => Option::None,
|
||||
Self::CopyStatement(s1) => Some(s1.args()),
|
||||
Self::EqualFromEntries(ValueOf(ak1, v1), ValueOf(ak2, v2)) => {
|
||||
if v1 == v2 {
|
||||
Some(vec![StatementArg::Key(*ak1), StatementArg::Key(*ak2)])
|
||||
} else {
|
||||
return Err(anyhow!("Invalid operation"));
|
||||
}
|
||||
}
|
||||
Self::EqualFromEntries(_, _) => {
|
||||
return Err(anyhow!("Invalid operation"));
|
||||
}
|
||||
Self::NotEqualFromEntries(ValueOf(ak1, v1), ValueOf(ak2, v2)) => {
|
||||
if v1 != v2 {
|
||||
Some(vec![StatementArg::Key(*ak1), StatementArg::Key(*ak2)])
|
||||
} else {
|
||||
return Err(anyhow!("Invalid operation"));
|
||||
}
|
||||
}
|
||||
Self::NotEqualFromEntries(_, _) => {
|
||||
return Err(anyhow!("Invalid operation"));
|
||||
}
|
||||
Self::GtFromEntries(ValueOf(ak1, v1), ValueOf(ak2, v2)) => {
|
||||
if v1 > v2 {
|
||||
Some(vec![StatementArg::Key(*ak1), StatementArg::Key(*ak2)])
|
||||
} else {
|
||||
return Err(anyhow!("Invalid operation"));
|
||||
}
|
||||
}
|
||||
Self::GtFromEntries(_, _) => {
|
||||
return Err(anyhow!("Invalid operation"));
|
||||
}
|
||||
Self::LtFromEntries(ValueOf(ak1, v1), ValueOf(ak2, v2)) => {
|
||||
if v1 < v2 {
|
||||
Some(vec![StatementArg::Key(*ak1), StatementArg::Key(*ak2)])
|
||||
} else {
|
||||
return Err(anyhow!("Invalid operation"));
|
||||
}
|
||||
}
|
||||
Self::LtFromEntries(_, _) => {
|
||||
return Err(anyhow!("Invalid operation"));
|
||||
}
|
||||
Self::TransitiveEqualFromStatements(Equal(ak1, ak2), Equal(ak3, ak4)) => {
|
||||
if ak2 == ak3 {
|
||||
Some(vec![StatementArg::Key(*ak1), StatementArg::Key(*ak3)])
|
||||
} else {
|
||||
return Err(anyhow!("Invalid operation"));
|
||||
}
|
||||
}
|
||||
Self::TransitiveEqualFromStatements(_, _) => {
|
||||
return Err(anyhow!("Invalid operation"));
|
||||
}
|
||||
Self::GtToNotEqual(Gt(ak1, ak2)) => {
|
||||
Some(vec![StatementArg::Key(*ak1), StatementArg::Key(*ak2)])
|
||||
}
|
||||
Self::GtToNotEqual(_) => {
|
||||
return Err(anyhow!("Invalid operation"));
|
||||
}
|
||||
Self::LtToNotEqual(Gt(ak1, ak2)) => {
|
||||
Some(vec![StatementArg::Key(*ak1), StatementArg::Key(*ak2)])
|
||||
}
|
||||
Self::LtToNotEqual(_) => {
|
||||
return Err(anyhow!("Invalid operation"));
|
||||
}
|
||||
Self::ContainsFromEntries(ValueOf(ak1, v1), ValueOf(ak2, v2)) =>
|
||||
/* TODO */
|
||||
{
|
||||
Some(vec![StatementArg::Key(*ak1), StatementArg::Key(*ak2)])
|
||||
}
|
||||
Self::ContainsFromEntries(_, _) => {
|
||||
return Err(anyhow!("Invalid operation"));
|
||||
}
|
||||
Self::NotContainsFromEntries(ValueOf(ak1, v1), ValueOf(ak2, v2)) =>
|
||||
/* TODO */
|
||||
{
|
||||
Some(vec![StatementArg::Key(*ak1), StatementArg::Key(*ak2)])
|
||||
}
|
||||
Self::NotContainsFromEntries(_, _) => {
|
||||
return Err(anyhow!("Invalid operation"));
|
||||
}
|
||||
Self::SumOf(ValueOf(ak1, v1), ValueOf(ak2, v2), ValueOf(ak3, v3)) => {
|
||||
let v1: i64 = (*v1).try_into()?;
|
||||
let v2: i64 = (*v2).try_into()?;
|
||||
let v3: i64 = (*v3).try_into()?;
|
||||
if v1 == v2 + v3 {
|
||||
Some(vec![StatementArg::Key(*ak1), StatementArg::Key(*ak2)])
|
||||
} else {
|
||||
return Err(anyhow!("Invalid operation"));
|
||||
}
|
||||
}
|
||||
Self::SumOf(_, _, _) => {
|
||||
return Err(anyhow!("Invalid operation"));
|
||||
}
|
||||
Self::ProductOf(ValueOf(ak1, v1), ValueOf(ak2, v2), ValueOf(ak3, v3)) => {
|
||||
let v1: i64 = (*v1).try_into()?;
|
||||
let v2: i64 = (*v2).try_into()?;
|
||||
let v3: i64 = (*v3).try_into()?;
|
||||
if v1 == v2 * v3 {
|
||||
Some(vec![StatementArg::Key(*ak1), StatementArg::Key(*ak2)])
|
||||
} else {
|
||||
return Err(anyhow!("Invalid operation"));
|
||||
}
|
||||
}
|
||||
Self::ProductOf(_, _, _) => {
|
||||
return Err(anyhow!("Invalid operation"));
|
||||
}
|
||||
Self::MaxOf(ValueOf(ak1, v1), ValueOf(ak2, v2), ValueOf(ak3, v3)) => {
|
||||
let v1: i64 = (*v1).try_into()?;
|
||||
let v2: i64 = (*v2).try_into()?;
|
||||
let v3: i64 = (*v3).try_into()?;
|
||||
if v1 == std::cmp::max(v2, v3) {
|
||||
Some(vec![StatementArg::Key(*ak1), StatementArg::Key(*ak2)])
|
||||
} else {
|
||||
return Err(anyhow!("Invalid operation"));
|
||||
}
|
||||
}
|
||||
Self::MaxOf(_, _, _) => {
|
||||
return Err(anyhow!("Invalid operation"));
|
||||
}
|
||||
Self::Custom(_, _) => todo!(),
|
||||
};
|
||||
|
||||
let x: Option<Result<Statement>> = pred
|
||||
.zip(st_args)
|
||||
.map(|(pred, st_args)| Statement::from_args(pred, st_args));
|
||||
x.transpose()
|
||||
}
|
||||
/// Checks the given operation against a statement.
|
||||
pub fn check(&self, _params: &Params, output_statement: &Statement) -> Result<bool> {
|
||||
use Statement::*;
|
||||
|
|
@ -176,9 +343,6 @@ impl Operation {
|
|||
) => Ok(ak2 == ak3 && ak5 == ak1 && ak6 == ak4),
|
||||
(Self::GtToNotEqual(Gt(ak1, ak2)), NotEqual(ak3, ak4)) => Ok(ak1 == ak3 && ak2 == ak4),
|
||||
(Self::LtToNotEqual(Lt(ak1, ak2)), NotEqual(ak3, ak4)) => Ok(ak1 == ak3 && ak2 == ak4),
|
||||
(Self::RenameContainedBy(Contains(ak1, ak2), Equal(ak3, ak4)), Contains(ak5, ak6)) => {
|
||||
Ok(ak1 == ak3 && ak4 == ak5 && ak2 == ak6)
|
||||
}
|
||||
(
|
||||
Self::SumOf(ValueOf(ak1, v1), ValueOf(ak2, v2), ValueOf(ak3, v3)),
|
||||
SumOf(ak4, ak5, ak6),
|
||||
|
|
@ -231,7 +395,7 @@ impl fmt::Display for Operation {
|
|||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
writeln!(f, "middleware::Operation:")?;
|
||||
writeln!(f, " {:?} ", self.code())?;
|
||||
for (_, arg) in self.args().iter().enumerate() {
|
||||
for arg in self.args().iter() {
|
||||
writeln!(f, " {}", arg)?;
|
||||
}
|
||||
Ok(())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue