Skip to content

Commit f6cec56

Browse files
committed
eval - fix between
1 parent 20ac5e7 commit f6cec56

File tree

3 files changed

+32
-30
lines changed

3 files changed

+32
-30
lines changed

primitives/src/targeting/eval.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,9 @@ pub enum Function {
258258
/// Gets the element at a certain position (second value) of an array (first value)
259259
At(Box<Rule>, Box<Rule>),
260260
/// Note: this is inclusive of the start and end value
261+
/// 0 - start
262+
/// 1 - end
263+
/// 2 - value
261264
Between(Box<Rule>, Box<Rule>, Box<Rule>),
262265
Split(Box<Rule>, Box<Rule>),
263266
StartsWith(Box<Rule>, Box<Rule>),
@@ -381,14 +384,14 @@ impl Function {
381384
}
382385

383386
pub fn new_between(
384-
value: impl Into<Rule>,
385387
start: impl Into<Rule>,
386388
end: impl Into<Rule>,
389+
value: impl Into<Rule>,
387390
) -> Self {
388391
Self::Between(
389-
Box::new(value.into()),
390392
Box::new(start.into()),
391393
Box::new(end.into()),
394+
Box::new(value.into()),
392395
)
393396
}
394397

@@ -687,26 +690,26 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
687690
None
688691
}
689692
}
690-
Function::IfNot(first_rule, second_rule) => {
691-
let eval_if = eval(input, output, first_rule)?
693+
Function::IfNot(if_rule, else_rule) => {
694+
let eval_if = eval(input, output, if_rule)?
692695
.ok_or(Error::TypeError)?
693696
.try_bool()?;
694697

695698
if !eval_if {
696-
eval(input, output, second_rule)?
699+
eval(input, output, else_rule)?
697700
} else {
698701
None
699702
}
700703
}
701-
Function::IfElse(first_rule, second_rule, third_rule) => {
702-
let eval_if = eval(input, output, first_rule)?
704+
Function::IfElse(if_rule, then_rule, else_rule) => {
705+
let eval_if = eval(input, output, if_rule)?
703706
.ok_or(Error::TypeError)?
704707
.try_bool()?;
705708

706709
if eval_if {
707-
eval(input, output, second_rule)?
710+
eval(input, output, then_rule)?
708711
} else {
709-
eval(input, output, third_rule)?
712+
eval(input, output, else_rule)?
710713
}
711714
}
712715
Function::And(first_rule, second_rule) => {
@@ -889,10 +892,10 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
889892
Some(Value::Bool(a.iter().any(|x| b.contains(x))))
890893
}
891894
Function::In(array_value, search_value) => {
892-
let a = eval(input, output, array_value)?.ok_or(Error::TypeError)?
893-
.try_array()?;
894-
let b = eval(input, output, search_value)?
895-
.ok_or(Error::TypeError)?;
895+
let a = eval(input, output, array_value)?
896+
.ok_or(Error::TypeError)?
897+
.try_array()?;
898+
let b = eval(input, output, search_value)?.ok_or(Error::TypeError)?;
896899

897900
Some(Value::Bool(a.contains(&b)))
898901
}
@@ -903,7 +906,7 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
903906
.try_bool()?;
904907
Some(Value::Bool(!is_in))
905908
}
906-
Function::Between(value_rule, min_rule, max_rule) => {
909+
Function::Between(min_rule, max_rule, value_rule) => {
907910
let is_gte_start = Function::Gte(value_rule.clone(), min_rule.clone())
908911
.eval(input, output)?
909912
.ok_or(Error::TypeError)?

primitives/src/targeting/eval_test.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -810,39 +810,39 @@ mod math_functions {
810810

811811
let cases = vec![
812812
(
813-
Value::BigNum(1.into()),
814813
Value::BigNum(10.into()),
815814
Value::BigNum(100.into()),
815+
Value::BigNum(1.into()),
816816
Value::Bool(false),
817817
),
818818
(
819-
Value::BigNum(10.into()),
820819
Value::BigNum(10.into()),
821820
Value::BigNum(100.into()),
821+
Value::BigNum(10.into()),
822822
Value::Bool(true),
823823
),
824824
(
825-
Value::BigNum(50.into()),
826825
Value::BigNum(10.into()),
827826
Value::BigNum(100.into()),
827+
Value::BigNum(50.into()),
828828
Value::Bool(true),
829829
),
830830
(
831-
Value::BigNum(100.into()),
832831
Value::BigNum(10.into()),
833832
Value::BigNum(100.into()),
833+
Value::BigNum(100.into()),
834834
Value::Bool(true),
835835
),
836836
(
837-
Value::BigNum(1000.into()),
838837
Value::BigNum(10.into()),
839838
Value::BigNum(100.into()),
839+
Value::BigNum(1000.into()),
840840
Value::Bool(false),
841841
),
842842
];
843843

844-
for (value, start, end, expected) in cases.into_iter() {
845-
let rule = Rule::Function(Function::new_between(value, start, end));
844+
for (start, end, value, expected) in cases.into_iter() {
845+
let rule = Rule::Function(Function::new_between(start, end, value));
846846

847847
assert_eq!(Ok(Some(expected)), rule.eval(&input, &mut output));
848848
}
@@ -1188,10 +1188,7 @@ mod string_and_array {
11881188
];
11891189

11901190
for (arr, value, expected) in cases.into_iter() {
1191-
let rule = Rule::Function(Function::new_in(
1192-
Value::Array(arr),
1193-
value,
1194-
));
1191+
let rule = Rule::Function(Function::new_in(Value::Array(arr), value));
11951192
let expected = Some(Value::Bool(expected));
11961193

11971194
assert_eq!(Ok(expected), rule.eval(&input, &mut output));
@@ -1228,10 +1225,7 @@ mod string_and_array {
12281225
];
12291226

12301227
for (arr, value, expected) in cases.into_iter() {
1231-
let rule = Rule::Function(Function::new_nin(
1232-
Value::Array(arr),
1233-
value,
1234-
));
1228+
let rule = Rule::Function(Function::new_nin(Value::Array(arr), value));
12351229
let expected = Some(Value::Bool(expected));
12361230

12371231
assert_eq!(Ok(expected), rule.eval(&input, &mut output));

primitives/src/util/tests/prep_db.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
use crate::{AdUnit, BigNum, Channel, ChannelId, ChannelSpec, EventSubmission, IPFS, SpecValidators, ValidatorDesc, ValidatorId, channel::{Pricing, PricingBounds}, targeting::Rules};
1+
use crate::{
2+
channel::{Pricing, PricingBounds},
3+
targeting::Rules,
4+
AdUnit, BigNum, Channel, ChannelId, ChannelSpec, EventSubmission, SpecValidators,
5+
ValidatorDesc, ValidatorId, IPFS,
6+
};
27
use chrono::{TimeZone, Utc};
38
use fake::faker::{Faker, Number};
49
use hex::FromHex;

0 commit comments

Comments
 (0)