Skip to content

Commit 1b62ed3

Browse files
committed
primitives - targeting - eval - impl Function::Set
1 parent a545bc0 commit 1b62ed3

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

primitives/src/targeting/eval.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ pub enum Function {
8787
And(Box<Rule>, Box<Rule>),
8888
Intersects(Box<Rule>, Box<Rule>),
8989
Get(String),
90+
/// Output variables can be set any number of times by different rules, except `show`
91+
/// if `show` is at any point set to `false`, we stop executing rules and don't show the ad.
92+
Set(String, Box<Rule>),
9093
/// Bn(Value) function.
9194
Bn(Value),
9295
}
@@ -143,6 +146,13 @@ impl Value {
143146
pub fn try_bignum(self) -> Result<BigNum, Error> {
144147
BigNum::try_from(self)
145148
}
149+
150+
pub fn try_number(self) -> Result<Number, Error> {
151+
match self {
152+
Value::Number(number) => Ok(number),
153+
_ => Err(Error::TypeError),
154+
}
155+
}
146156
}
147157

148158
impl TryFrom<Value> for BigNum {
@@ -248,6 +258,49 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
248258

249259
Some(Value::Bool(a.iter().any(|x| b.contains(x))))
250260
}
261+
Function::Set(key, rule) => {
262+
// Output variables can be set any number of times by different rules, except `show`
263+
// if `show` is at any point set to `false`, we stop executing rules and don't show the ad.
264+
match key.as_str() {
265+
"boost" => {
266+
let boost_num = rule
267+
.eval(input, output)?
268+
.ok_or(Error::TypeError)?
269+
.try_number()?;
270+
271+
output.boost = boost_num.as_f64().ok_or(Error::TypeError)?;
272+
}
273+
"show" => {
274+
let show_value = rule
275+
.eval(input, output)?
276+
.ok_or(Error::TypeError)?
277+
.try_bool()?;
278+
279+
output.show = show_value;
280+
}
281+
"price.IMPRESSION" => {
282+
let price = rule
283+
.eval(input, output)?
284+
.ok_or(Error::TypeError)?
285+
.try_bignum()?;
286+
287+
// we do not care about any other old value
288+
output.price.insert("IMPRESSION".to_string(), price);
289+
}
290+
"price.CLICK" => {
291+
let price = rule
292+
.eval(input, output)?
293+
.ok_or(Error::TypeError)?
294+
.try_bignum()?;
295+
296+
// we do not care about any other old value
297+
output.price.insert("CLICK".to_string(), price);
298+
}
299+
_ => return Err(Error::UnknownVariable),
300+
}
301+
302+
return Ok(None);
303+
}
251304
Function::Get(key) => Some(input.try_get(key)?),
252305
Function::Bn(value) => {
253306
let big_num = value.clone().try_bignum()?;

0 commit comments

Comments
 (0)