Skip to content

Commit 07101f4

Browse files
committed
primitives - eval - fix in & nin
1 parent 6c03ab3 commit 07101f4

File tree

2 files changed

+63
-59
lines changed

2 files changed

+63
-59
lines changed

primitives/src/targeting/eval.rs

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -372,12 +372,12 @@ impl Function {
372372
Self::Intersects(Box::new(lhs.into()), Box::new(rhs.into()))
373373
}
374374

375-
pub fn new_in(lhs: impl Into<Rule>, rhs: impl Into<Rule>) -> Self {
376-
Self::In(Box::new(lhs.into()), Box::new(rhs.into()))
375+
pub fn new_in(array: impl Into<Rule>, value: impl Into<Rule>) -> Self {
376+
Self::In(Box::new(array.into()), Box::new(value.into()))
377377
}
378378

379-
pub fn new_nin(lhs: impl Into<Rule>, rhs: impl Into<Rule>) -> Self {
380-
Self::Nin(Box::new(lhs.into()), Box::new(rhs.into()))
379+
pub fn new_nin(array: impl Into<Rule>, value: impl Into<Rule>) -> Self {
380+
Self::Nin(Box::new(array.into()), Box::new(value.into()))
381381
}
382382

383383
pub fn new_between(
@@ -888,101 +888,105 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
888888

889889
Some(Value::Bool(a.iter().any(|x| b.contains(x))))
890890
}
891-
Function::In(first_rule, second_rule) => {
892-
let a = eval(input, output, first_rule)?.ok_or(Error::TypeError)?;
893-
let b = eval(input, output, second_rule)?
894-
.ok_or(Error::TypeError)?
895-
.try_array()?;
891+
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)?;
896896

897-
Some(Value::Bool(b.contains(&a)))
897+
Some(Value::Bool(a.contains(&b)))
898898
}
899-
Function::Nin(first_rule, second_rule) => {
900-
let is_in = Function::In(first_rule.clone(), second_rule.clone())
899+
Function::Nin(array_value, search_value) => {
900+
let is_in = Function::In(array_value.clone(), search_value.clone())
901901
.eval(input, output)?
902902
.ok_or(Error::TypeError)?
903903
.try_bool()?;
904904
Some(Value::Bool(!is_in))
905905
}
906-
Function::Between(first_rule, second_rule, third_rule) => {
907-
let is_gte_start = Function::Gte(first_rule.clone(), second_rule.clone())
906+
Function::Between(value_rule, min_rule, max_rule) => {
907+
let is_gte_start = Function::Gte(value_rule.clone(), min_rule.clone())
908908
.eval(input, output)?
909909
.ok_or(Error::TypeError)?
910910
.try_bool()?;
911911

912-
let is_lte_end = Function::Lte(first_rule.clone(), third_rule.clone())
912+
let is_lte_end = Function::Lte(value_rule.clone(), max_rule.clone())
913913
.eval(input, output)?
914914
.ok_or(Error::TypeError)?
915915
.try_bool()?;
916+
916917
Some(Value::Bool(is_gte_start && is_lte_end))
917918
}
918-
Function::At(first_rule, second_rule) => {
919-
let mut first_eval = first_rule
919+
Function::At(array_rule, index_rule) => {
920+
let mut array_value = array_rule
920921
.eval(input, output)?
921922
.ok_or(Error::TypeError)?
922923
.try_array()?;
923-
let second_eval = second_rule
924+
let index_value = index_rule
924925
.eval(input, output)?
925926
.ok_or(Error::TypeError)?
926927
.try_number()?
927928
.as_u64()
928929
.ok_or(Error::TypeError)?;
929-
let index = usize::try_from(second_eval).map_err(|_| Error::TypeError)?;
930-
if first_eval.get(index).is_none() {
930+
let index = usize::try_from(index_value).map_err(|_| Error::TypeError)?;
931+
932+
if array_value.get(index).is_none() {
931933
return Err(Error::TypeError);
932934
} else {
933-
Some(first_eval.swap_remove(index))
935+
Some(array_value.swap_remove(index))
934936
}
935937
}
936-
Function::Split(first_rule, second_rule) => {
937-
let first_eval = first_rule
938+
Function::Split(string_rule, pattern_rule) => {
939+
let string_value = string_rule
938940
.eval(input, output)?
939941
.ok_or(Error::TypeError)?
940942
.try_string()?;
941-
let second_eval = second_rule
943+
let pattern_value = pattern_rule
942944
.eval(input, output)?
943945
.ok_or(Error::TypeError)?
944946
.try_string()?;
945947

946-
let after_split = first_eval
947-
.split(&second_eval)
948+
let after_split = string_value
949+
.split(&pattern_value)
948950
.map(Value::new_string)
949951
.collect();
952+
950953
Some(Value::Array(after_split))
951954
}
952-
Function::StartsWith(first_rule, second_rule) => {
953-
let first_eval = first_rule
955+
Function::StartsWith(string_rule, starts_with_rule) => {
956+
let string_value = string_rule
954957
.eval(input, output)?
955958
.ok_or(Error::TypeError)?
956959
.try_string()?;
957-
let second_eval = second_rule
960+
let starts_with_value = starts_with_rule
958961
.eval(input, output)?
959962
.ok_or(Error::TypeError)?
960963
.try_string()?;
961964

962-
Some(Value::Bool(first_eval.starts_with(&second_eval)))
965+
Some(Value::Bool(string_value.starts_with(&starts_with_value)))
963966
}
964-
Function::EndsWith(first_rule, second_rule) => {
965-
let first_eval = first_rule
967+
Function::EndsWith(string_rule, ends_with_rule) => {
968+
let string_value = string_rule
966969
.eval(input, output)?
967970
.ok_or(Error::TypeError)?
968971
.try_string()?;
969-
let second_eval = second_rule
972+
let ends_with_value = ends_with_rule
970973
.eval(input, output)?
971974
.ok_or(Error::TypeError)?
972975
.try_string()?;
973976

974-
Some(Value::Bool(first_eval.ends_with(&second_eval)))
977+
Some(Value::Bool(string_value.ends_with(&ends_with_value)))
975978
}
976-
Function::OnlyShowIf(first_rule) => {
977-
let first_eval = first_rule
979+
Function::OnlyShowIf(rule) => {
980+
let eval = rule
978981
.eval(input, output)?
979982
.ok_or(Error::TypeError)?
980983
.try_bool()?;
981-
let new_rule = Box::new(Rule::Value(Value::Bool(first_eval)));
984+
let new_rule = Box::new(Rule::Value(Value::Bool(eval)));
985+
982986
Function::Set(String::from("show"), new_rule).eval(input, output)?
983987
}
984-
Function::GetPriceInUsd(first_rule) => {
985-
let amount = first_rule
988+
Function::GetPriceInUsd(amount_rule) => {
989+
let amount = amount_rule
986990
.eval(input, output)?
987991
.ok_or(Error::TypeError)?
988992
.try_bignum()?;

primitives/src/targeting/eval_test.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,29 +1168,29 @@ mod string_and_array {
11681168

11691169
let cases = vec![
11701170
(
1171-
Value::BigNum(1.into()),
1172-
Value::Array(vec![
1171+
vec![
11731172
Value::BigNum(1.into()),
11741173
Value::BigNum(2.into()),
11751174
Value::BigNum(3.into()),
1176-
]),
1175+
],
1176+
Value::BigNum(1.into()),
11771177
true,
11781178
),
11791179
(
1180-
Value::BigNum(0.into()),
1181-
Value::Array(vec![
1180+
vec![
11821181
Value::BigNum(1.into()),
11831182
Value::BigNum(2.into()),
11841183
Value::BigNum(3.into()),
1185-
]),
1184+
],
1185+
Value::BigNum(0.into()),
11861186
false,
11871187
),
11881188
];
11891189

1190-
for (value, arr, expected) in cases.into_iter() {
1190+
for (arr, value, expected) in cases.into_iter() {
11911191
let rule = Rule::Function(Function::new_in(
1192-
Rule::Value(value.clone()),
1193-
Rule::Value(arr.clone()),
1192+
Value::Array(arr),
1193+
value,
11941194
));
11951195
let expected = Some(Value::Bool(expected));
11961196

@@ -1206,33 +1206,33 @@ mod string_and_array {
12061206
price: Default::default(),
12071207
};
12081208

1209-
let cases = [
1209+
let cases = vec![
12101210
(
1211-
Value::new_number(1),
1212-
Value::Array(vec![
1211+
vec![
12131212
Value::new_number(1),
12141213
Value::new_number(2),
12151214
Value::new_number(3),
1216-
]),
1215+
],
1216+
Value::new_number(1),
12171217
false,
12181218
),
12191219
(
1220-
Value::new_number(0),
1221-
Value::Array(vec![
1220+
vec![
12221221
Value::new_number(1),
12231222
Value::new_number(2),
12241223
Value::new_number(3),
1225-
]),
1224+
],
1225+
Value::new_number(0),
12261226
true,
12271227
),
12281228
];
12291229

1230-
for (value, arr, expected) in cases.iter() {
1230+
for (arr, value, expected) in cases.into_iter() {
12311231
let rule = Rule::Function(Function::new_nin(
1232-
Rule::Value(value.clone()),
1233-
Rule::Value(arr.clone()),
1232+
Value::Array(arr),
1233+
value,
12341234
));
1235-
let expected = Some(Value::Bool(*expected));
1235+
let expected = Some(Value::Bool(expected));
12361236

12371237
assert_eq!(Ok(expected), rule.eval(&input, &mut output));
12381238
}

0 commit comments

Comments
 (0)