Skip to content

Commit 64f91a4

Browse files
committed
Fixes from code review + refactoring to reduce code repetition + formatting
1 parent 55b23de commit 64f91a4

File tree

1 file changed

+71
-97
lines changed

1 file changed

+71
-97
lines changed

primitives/src/targeting/eval.rs

Lines changed: 71 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ impl TryFrom<SerdeValue> for Value {
8888
#[serde(rename_all = "camelCase")]
8989
// TODO: https://github.com/AdExNetwork/adex-validator-stack-rust/issues/296
9090
pub enum Function {
91+
/// Multiplies first two values and then divides product by third value
9192
MulDiv(Box<Rule>, Box<Rule>, Box<Rule>),
9293
Div(Box<Rule>, Box<Rule>),
9394
Mul(Box<Rule>, Box<Rule>),
@@ -103,14 +104,23 @@ pub enum Function {
103104
Or(Box<Rule>, Box<Rule>),
104105
Xor(Box<Rule>, Box<Rule>),
105106
Not(Box<Rule>),
107+
/// Is the first value Lesser than second value
106108
Lt(Box<Rule>, Box<Rule>),
109+
/// Is the first value Lesser than or equal to the second value
107110
Lte(Box<Rule>, Box<Rule>),
111+
/// Is the first value Greater than second value
108112
Gt(Box<Rule>, Box<Rule>),
113+
/// Is the first value Greater than or equal to the second value
109114
Gte(Box<Rule>, Box<Rule>),
110-
Equals(Box<Rule>, Box<Rule>),
111-
NotEquals(Box<Rule>, Box<Rule>),
115+
/// Are values equal
116+
Eq(Box<Rule>, Box<Rule>),
117+
/// Are values NOT equal
118+
Neq(Box<Rule>, Box<Rule>),
119+
/// Is first value included in an array (second value)
112120
In(Box<Rule>, Box<Rule>),
121+
/// Is first value NOT included in an array (second value)
113122
NotIn(Box<Rule>, Box<Rule>),
123+
/// Gets the element at a certain position (second value) of an array (first value)
114124
At(Box<Rule>, Box<Rule>),
115125
/// Note: this is inclusive of the start and end value
116126
Between(Box<Rule>, Box<Rule>, Box<Rule>),
@@ -119,6 +129,7 @@ pub enum Function {
119129
EndsWith(Box<Rule>, Box<Rule>),
120130
OnlyShowIf(Box<Rule>),
121131
Intersects(Box<Rule>, Box<Rule>),
132+
/// Evaluates rule
122133
Do(Box<Rule>),
123134
Get(String),
124135
/// Output variables can be set any number of times by different rules, except `show`
@@ -537,7 +548,7 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
537548
.ok_or(Error::TypeError)?
538549
.try_bool()?;
539550

540-
Some(Value::Bool((a && !b) || (!a && b)))
551+
Some(Value::Bool(a ^ b))
541552
}
542553
Function::Not(first_rule) => {
543554
let a = eval(input, output, first_rule)?
@@ -638,7 +649,7 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
638649

639650
Some(value)
640651
}
641-
Function::Equals(first_rule, second_rule) => {
652+
Function::Eq(first_rule, second_rule) => {
642653
let first_eval = first_rule.eval(input, output)?.ok_or(Error::TypeError)?;
643654
let second_eval = second_rule.eval(input, output)?.ok_or(Error::TypeError)?;
644655

@@ -654,7 +665,7 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
654665
Value::Bool(lhs_bignum.eq(&rhs_bignum))
655666
}
656667
(Value::Number(lhs), Value::Number(rhs)) => {
657-
Value::Bool(compare_numbers(lhs, rhs, ComparisonOperator::Equals)?)
668+
Value::Bool(compare_numbers(lhs, rhs, ComparisonOperator::Eq)?)
658669
}
659670
(Value::Bool(lhs), Value::Bool(rhs)) => Value::Bool(lhs == rhs),
660671
(Value::String(lhs), Value::String(rhs)) => Value::Bool(lhs == rhs),
@@ -671,38 +682,15 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
671682

672683
Some(value)
673684
}
674-
Function::NotEquals(first_rule, second_rule) => {
675-
let first_eval = first_rule.eval(input, output)?.ok_or(Error::TypeError)?;
676-
let second_eval = second_rule.eval(input, output)?.ok_or(Error::TypeError)?;
677-
678-
let value = match (first_eval, second_eval) {
679-
(Value::BigNum(bignum), rhs_value) => {
680-
let rhs_bignum = BigNum::try_from(rhs_value)?;
681-
682-
Value::Bool(bignum.ne(&rhs_bignum))
683-
}
684-
(lhs_value, Value::BigNum(rhs_bignum)) => {
685-
let lhs_bignum = BigNum::try_from(lhs_value)?;
686-
687-
Value::Bool(lhs_bignum.ne(&rhs_bignum))
688-
}
689-
(Value::Number(lhs), Value::Number(rhs)) => {
690-
Value::Bool(compare_numbers(lhs, rhs, ComparisonOperator::NotEquals)?)
691-
}
692-
(Value::Bool(lhs), Value::Bool(rhs)) => Value::Bool(lhs != rhs),
693-
(Value::String(lhs), Value::String(rhs)) => Value::Bool(lhs != rhs),
694-
(Value::Array(lhs), Value::Array(rhs)) => {
695-
if lhs.len() != rhs.len() {
696-
Value::Bool(true)
697-
} else {
698-
let are_same = lhs.iter().zip(rhs.iter()).all(|(a, b)| a == b);
699-
Value::Bool(!are_same)
700-
}
701-
}
702-
_ => return Err(Error::TypeError),
703-
};
704-
705-
Some(value)
685+
Function::Neq(first_rule, second_rule) => {
686+
let is_equal = eval(
687+
input,
688+
output,
689+
&Rule::Function(Function::Eq(first_rule.clone(), second_rule.clone())),
690+
)?
691+
.ok_or(Error::TypeError)?
692+
.try_bool()?;
693+
Some(Value::Bool(!is_equal))
706694
}
707695
Function::Intersects(first_rule, second_rule) => {
708696
let a = eval(input, output, first_rule)?
@@ -723,23 +711,31 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
723711
Some(Value::Bool(b.contains(&a)))
724712
}
725713
Function::NotIn(first_rule, second_rule) => {
726-
let a = eval(input, output, first_rule)?.ok_or(Error::TypeError)?;
727-
let b = eval(input, output, second_rule)?
728-
.ok_or(Error::TypeError)?
729-
.try_array()?;
730-
731-
Some(Value::Bool(!b.contains(&a)))
714+
let is_in = eval(
715+
input,
716+
output,
717+
&Rule::Function(Function::In(first_rule.clone(), second_rule.clone())),
718+
)?
719+
.ok_or(Error::TypeError)?
720+
.try_bool()?;
721+
Some(Value::Bool(!is_in))
732722
}
733723
Function::Between(first_rule, second_rule, third_rule) => {
734-
let first_eval = first_rule.eval(input, output)?.ok_or(Error::TypeError)?;
735-
let second_eval = second_rule.eval(input, output)?.ok_or(Error::TypeError)?;
736-
let third_eval = third_rule.eval(input, output)?.ok_or(Error::TypeError)?;
737-
738-
let value = BigNum::try_from(first_eval)?;
739-
let start = BigNum::try_from(second_eval)?;
740-
let end = BigNum::try_from(third_eval)?;
741-
742-
Some(Value::Bool(value.ge(&start) && value.le(&end)))
724+
let is_gte_start = eval(
725+
input,
726+
output,
727+
&Rule::Function(Function::Gte(first_rule.clone(), second_rule.clone())),
728+
)?
729+
.ok_or(Error::TypeError)?
730+
.try_bool()?;
731+
let is_lte_end = eval(
732+
input,
733+
output,
734+
&Rule::Function(Function::Lte(first_rule.clone(), third_rule.clone())),
735+
)?
736+
.ok_or(Error::TypeError)?
737+
.try_bool()?;
738+
Some(Value::Bool(is_gte_start && is_lte_end))
743739
}
744740
Function::At(first_rule, second_rule) => {
745741
let mut first_eval = first_rule
@@ -752,14 +748,12 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
752748
.try_number()?
753749
.as_u64()
754750
.ok_or(Error::TypeError)?;
755-
let index = usize::try_from(second_eval).unwrap();
756-
let value = if first_eval.get(index).is_none() {
757-
None
751+
let index = usize::try_from(second_eval).map_err(|_| Error::TypeError)?;
752+
if first_eval.get(index).is_none() {
753+
return Err(Error::TypeError);
758754
} else {
759755
Some(first_eval.swap_remove(index))
760-
};
761-
let value = value.ok_or(Error::TypeError)?;
762-
Some(value)
756+
}
763757
}
764758
Function::Split(first_rule, second_rule) => {
765759
let first_eval = first_rule
@@ -771,12 +765,11 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
771765
.ok_or(Error::TypeError)?
772766
.try_string()?;
773767

774-
let after_split: Vec<&str> = first_eval.split(&second_eval).collect();
775-
let mapped_to_value = after_split
776-
.into_iter()
777-
.map(|x| Value::new_string(x))
768+
let after_split = first_eval
769+
.split(&second_eval)
770+
.map(Value::new_string)
778771
.collect();
779-
Some(Value::Array(mapped_to_value))
772+
Some(Value::Array(after_split))
780773
}
781774
Function::StartsWith(first_rule, second_rule) => {
782775
let first_eval = first_rule
@@ -880,57 +873,38 @@ enum MathOperator {
880873
}
881874

882875
enum ComparisonOperator {
876+
/// First value is greater than second value
883877
Gt,
878+
/// First value is greater than or equal to second value
884879
Gte,
880+
/// First value is lesser than second value
885881
Lt,
882+
/// First value is lesser than or equal to second value
886883
Lte,
887-
Equals,
888-
NotEquals,
884+
/// Values are equal
885+
Eq,
889886
}
890887

891888
fn compare_numbers(lhs: Number, rhs: Number, ops: ComparisonOperator) -> Result<bool, Error> {
892889
match (lhs.as_u64(), rhs.as_u64()) {
893-
(Some(lhs), Some(rhs)) => handle_comparisons(lhs, rhs, ops),
890+
(Some(lhs), Some(rhs)) => Ok(handle_comparisons(lhs, rhs, ops)),
894891
_ => match (lhs.as_i64(), rhs.as_i64()) {
895-
(Some(lhs), Some(rhs)) => handle_comparisons(lhs, rhs, ops),
892+
(Some(lhs), Some(rhs)) => Ok(handle_comparisons(lhs, rhs, ops)),
896893
_ => match (lhs.as_f64(), rhs.as_f64()) {
897-
(Some(lhs), Some(rhs)) => handle_comparisons(lhs, rhs, ops),
894+
(Some(lhs), Some(rhs)) => Ok(handle_comparisons(lhs, rhs, ops)),
898895
_ => Err(Error::TypeError),
899896
},
900897
},
901898
}
902899
}
903900

904-
fn handle_comparisons<T: PartialOrd>(
905-
lhs: T,
906-
rhs: T,
907-
ops: ComparisonOperator,
908-
) -> Result<bool, Error> {
901+
fn handle_comparisons<T: PartialOrd>(lhs: T, rhs: T, ops: ComparisonOperator) -> bool {
909902
match ops {
910-
ComparisonOperator::Lt => {
911-
let is_lt = lhs < rhs;
912-
Ok(is_lt)
913-
}
914-
ComparisonOperator::Lte => {
915-
let is_lte = lhs <= rhs;
916-
Ok(is_lte)
917-
}
918-
ComparisonOperator::Gt => {
919-
let is_gt = lhs > rhs;
920-
Ok(is_gt)
921-
}
922-
ComparisonOperator::Gte => {
923-
let is_gte = lhs >= rhs;
924-
Ok(is_gte)
925-
}
926-
ComparisonOperator::Equals => {
927-
let is_equal = lhs == rhs;
928-
Ok(is_equal)
929-
}
930-
ComparisonOperator::NotEquals => {
931-
let is_not_equal = lhs != rhs;
932-
Ok(is_not_equal)
933-
}
903+
ComparisonOperator::Lt => lhs.lt(&rhs),
904+
ComparisonOperator::Lte => lhs.le(&rhs),
905+
ComparisonOperator::Gt => lhs.gt(&rhs),
906+
ComparisonOperator::Gte => lhs.ge(&rhs),
907+
ComparisonOperator::Eq => lhs.eq(&rhs),
934908
}
935909
}
936910

0 commit comments

Comments
 (0)