@@ -87,6 +87,9 @@ pub enum Function {
87
87
And ( Box < Rule > , Box < Rule > ) ,
88
88
Intersects ( Box < Rule > , Box < Rule > ) ,
89
89
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 > ) ,
90
93
/// Bn(Value) function.
91
94
Bn ( Value ) ,
92
95
}
@@ -143,6 +146,13 @@ impl Value {
143
146
pub fn try_bignum ( self ) -> Result < BigNum , Error > {
144
147
BigNum :: try_from ( self )
145
148
}
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
+ }
146
156
}
147
157
148
158
impl TryFrom < Value > for BigNum {
@@ -248,6 +258,49 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
248
258
249
259
Some ( Value :: Bool ( a. iter ( ) . any ( |x| b. contains ( x) ) ) )
250
260
}
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
+ }
251
304
Function :: Get ( key) => Some ( input. try_get ( key) ?) ,
252
305
Function :: Bn ( value) => {
253
306
let big_num = value. clone ( ) . try_bignum ( ) ?;
0 commit comments