Skip to content

Commit 9adc5e9

Browse files
committed
muldiv - in progress
1 parent 1cb09cb commit 9adc5e9

File tree

1 file changed

+105
-86
lines changed

1 file changed

+105
-86
lines changed

primitives/src/targeting/eval.rs

Lines changed: 105 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
use crate::BigNum;
22
use serde::{Deserialize, Serialize};
33
use serde_json::{value::Value as SerdeValue, Number};
4-
use std::{convert::TryFrom, fmt, ops::{Div, Mul, Rem, Add, Sub}, str::FromStr};
4+
use std::{
5+
convert::TryFrom,
6+
fmt,
7+
ops::{Add, Div, Mul, Rem, Sub},
8+
str::FromStr,
9+
};
510

611
pub type Map = serde_json::value::Map<String, SerdeValue>;
712

@@ -83,6 +88,7 @@ impl TryFrom<SerdeValue> for Value {
8388
#[serde(rename_all = "camelCase")]
8489
// TODO: https://github.com/AdExNetwork/adex-validator-stack-rust/issues/296
8590
pub enum Function {
91+
MulDiv(Box<Rule>, Box<Rule>, Box<Rule>),
8692
Div(Box<Rule>, Box<Rule>),
8793
Mul(Box<Rule>, Box<Rule>),
8894
Mod(Box<Rule>, Box<Rule>),
@@ -198,17 +204,34 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
198204

199205
// basic operators
200206
let value = match function {
207+
Function::MulDiv(first_rule, second_rule, third_rule) => {
208+
let product = eval(
209+
input,
210+
output,
211+
&Rule::Function(Function::Mul(*first_rule, *second_rule)),
212+
)?
213+
.ok_or(Error::TypeError);
214+
let value = match product {
215+
Ok(product) => {
216+
let product_rule = Rule::Value(product);
217+
let boxed_rule = Box::new(product_rule);
218+
eval(input, output, &Rule::Function(Function::Div(boxed_rule, *third_rule)))
219+
},
220+
_ => return Err(Error::TypeError),
221+
};
222+
223+
Some(value)
224+
}
201225
Function::Div(first_rule, second_rule) => {
202226
let first_eval = first_rule.eval(input, output)?.ok_or(Error::TypeError)?;
203227
let second_eval = second_rule.eval(input, output)?.ok_or(Error::TypeError)?;
204228

205-
206229
let value = match (first_eval, second_eval) {
207230
(Value::BigNum(bignum), second_value) => {
208231
let second_bignum = BigNum::try_from(second_value)?;
209232

210233
Value::BigNum(bignum.div(second_bignum))
211-
},
234+
}
212235
(lhs_value, Value::BigNum(rhs_bignum)) => {
213236
let lhs_bignum = BigNum::try_from(lhs_value)?;
214237

@@ -231,7 +254,7 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
231254
let rhs_bignum = BigNum::try_from(rhs_value)?;
232255

233256
Value::BigNum(bignum.mul(rhs_bignum))
234-
},
257+
}
235258
(lhs_value, Value::BigNum(rhs_bignum)) => {
236259
let lhs_bignum = BigNum::try_from(lhs_value)?;
237260

@@ -254,7 +277,7 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
254277
let rhs_bignum = BigNum::try_from(rhs_value)?;
255278

256279
Value::BigNum(bignum.rem(rhs_bignum))
257-
},
280+
}
258281
(lhs_value, Value::BigNum(rhs_bignum)) => {
259282
let lhs_bignum = BigNum::try_from(lhs_value)?;
260283

@@ -277,7 +300,7 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
277300
let rhs_bignum = BigNum::try_from(rhs_value)?;
278301

279302
Value::BigNum(bignum.add(rhs_bignum))
280-
},
303+
}
281304
(lhs_value, Value::BigNum(rhs_bignum)) => {
282305
let lhs_bignum = BigNum::try_from(lhs_value)?;
283306

@@ -300,7 +323,7 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
300323
let rhs_bignum = BigNum::try_from(rhs_value)?;
301324

302325
Value::BigNum(bignum.sub(rhs_bignum))
303-
},
326+
}
304327
(lhs_value, Value::BigNum(rhs_bignum)) => {
305328
let lhs_bignum = BigNum::try_from(lhs_value)?;
306329

@@ -323,7 +346,7 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
323346
let rhs_bignum = BigNum::try_from(rhs_value)?;
324347

325348
Value::BigNum(bignum.max(rhs_bignum))
326-
},
349+
}
327350
(lhs_value, Value::BigNum(rhs_bignum)) => {
328351
let lhs_bignum = BigNum::try_from(lhs_value)?;
329352

@@ -346,7 +369,7 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
346369
let rhs_bignum = BigNum::try_from(rhs_value)?;
347370

348371
Value::BigNum(bignum.min(rhs_bignum))
349-
},
372+
}
350373
(lhs_value, Value::BigNum(rhs_bignum)) => {
351374
let lhs_bignum = BigNum::try_from(lhs_value)?;
352375

@@ -456,118 +479,114 @@ enum MathOperator {
456479
}
457480

458481
fn handle_u64(lhs: u64, rhs: u64, ops: MathOperator) -> Result<Number, Error> {
459-
match ops {
460-
MathOperator::Division => {
461-
let divided = lhs.checked_div(rhs).ok_or(Error::TypeError)?;
462-
Ok(divided.into())
463-
},
464-
MathOperator::Multiplication => {
465-
let multiplied = lhs.checked_mul(rhs).ok_or(Error::TypeError)?;
466-
Ok(multiplied.into())
467-
},
468-
MathOperator::Modulus => {
469-
let modulus = lhs.checked_rem(rhs).ok_or(Error::TypeError)?;
470-
Ok(modulus.into())
471-
},
472-
MathOperator::Addition => {
473-
let added = lhs.checked_add(rhs).ok_or(Error::TypeError)?;
474-
Ok(added.into())
475-
},
476-
MathOperator::Subtraction => {
477-
let subtracted = lhs.checked_sub(rhs).ok_or(Error::TypeError)?;
478-
Ok(subtracted.into())
479-
},
480-
MathOperator::Max => {
481-
let max = lhs.max(rhs);
482-
Ok(max.into())
483-
},
484-
MathOperator::Min => {
485-
let min = lhs.min(rhs);
486-
Ok(min.into())
487-
},
482+
match ops {
483+
MathOperator::Division => {
484+
let divided = lhs.checked_div(rhs).ok_or(Error::TypeError)?;
485+
Ok(divided.into())
486+
}
487+
MathOperator::Multiplication => {
488+
let multiplied = lhs.checked_mul(rhs).ok_or(Error::TypeError)?;
489+
Ok(multiplied.into())
490+
}
491+
MathOperator::Modulus => {
492+
let modulus = lhs.checked_rem(rhs).ok_or(Error::TypeError)?;
493+
Ok(modulus.into())
494+
}
495+
MathOperator::Addition => {
496+
let added = lhs.checked_add(rhs).ok_or(Error::TypeError)?;
497+
Ok(added.into())
498+
}
499+
MathOperator::Subtraction => {
500+
let subtracted = lhs.checked_sub(rhs).ok_or(Error::TypeError)?;
501+
Ok(subtracted.into())
502+
}
503+
MathOperator::Max => {
504+
let max = lhs.max(rhs);
505+
Ok(max.into())
488506
}
507+
MathOperator::Min => {
508+
let min = lhs.min(rhs);
509+
Ok(min.into())
510+
}
511+
}
489512
}
490513

491514
fn handle_i64(lhs: i64, rhs: i64, ops: MathOperator) -> Result<Number, Error> {
492515
match ops {
493516
MathOperator::Division => {
494517
let divided = lhs.checked_div(rhs).ok_or(Error::TypeError)?;
495518
Ok(divided.into())
496-
},
519+
}
497520
MathOperator::Multiplication => {
498521
let multiplied = lhs.checked_mul(rhs).ok_or(Error::TypeError)?;
499522
Ok(multiplied.into())
500-
},
523+
}
501524
MathOperator::Modulus => {
502525
let modulus = lhs.checked_rem(rhs).ok_or(Error::TypeError)?;
503526
Ok(modulus.into())
504-
},
527+
}
505528
MathOperator::Addition => {
506529
let added = lhs.checked_add(rhs).ok_or(Error::TypeError)?;
507530
Ok(added.into())
508-
},
531+
}
509532
MathOperator::Subtraction => {
510533
let subtracted = lhs.checked_sub(rhs).ok_or(Error::TypeError)?;
511534
Ok(subtracted.into())
512-
},
535+
}
513536
MathOperator::Max => {
514537
let max = lhs.max(rhs);
515538
Ok(max.into())
516-
},
539+
}
517540
MathOperator::Min => {
518541
let min = lhs.min(rhs);
519542
Ok(min.into())
520-
},
543+
}
521544
}
522545
}
523546

524-
fn handle_f64(lhs: f64, rhs: f64, ops:MathOperator) -> Result<Number, Error> {
525-
match ops {
526-
MathOperator::Division => {
527-
let divided = lhs.div(rhs);
528-
Ok(Number::from_f64(divided).ok_or(Error::TypeError)?)
529-
},
530-
MathOperator::Multiplication => {
531-
let multiplied = lhs.mul(rhs);
532-
Ok(Number::from_f64(multiplied).ok_or(Error::TypeError)?)
533-
},
534-
MathOperator::Modulus => {
535-
let modulus = lhs.rem(rhs);
536-
Ok(Number::from_f64(modulus).ok_or(Error::TypeError)?)
537-
},
538-
MathOperator::Addition => {
539-
let added = lhs.add(rhs);
540-
Ok(Number::from_f64(added).ok_or(Error::TypeError)?)
541-
},
542-
MathOperator::Subtraction => {
543-
let subtracted = lhs.sub(rhs);
544-
Ok(Number::from_f64(subtracted).ok_or(Error::TypeError)?)
545-
},
546-
MathOperator::Max => {
547-
let max = lhs.max(rhs);
548-
Ok(Number::from_f64(max).ok_or(Error::TypeError)?)
549-
},
550-
MathOperator::Min => {
551-
let min = lhs.min(rhs);
552-
Ok(Number::from_f64(min).ok_or(Error::TypeError)?)
553-
},
547+
fn handle_f64(lhs: f64, rhs: f64, ops: MathOperator) -> Result<Number, Error> {
548+
match ops {
549+
MathOperator::Division => {
550+
let divided = lhs.div(rhs);
551+
Ok(Number::from_f64(divided).ok_or(Error::TypeError)?)
552+
}
553+
MathOperator::Multiplication => {
554+
let multiplied = lhs.mul(rhs);
555+
Ok(Number::from_f64(multiplied).ok_or(Error::TypeError)?)
556+
}
557+
MathOperator::Modulus => {
558+
let modulus = lhs.rem(rhs);
559+
Ok(Number::from_f64(modulus).ok_or(Error::TypeError)?)
560+
}
561+
MathOperator::Addition => {
562+
let added = lhs.add(rhs);
563+
Ok(Number::from_f64(added).ok_or(Error::TypeError)?)
564+
}
565+
MathOperator::Subtraction => {
566+
let subtracted = lhs.sub(rhs);
567+
Ok(Number::from_f64(subtracted).ok_or(Error::TypeError)?)
554568
}
569+
MathOperator::Max => {
570+
let max = lhs.max(rhs);
571+
Ok(Number::from_f64(max).ok_or(Error::TypeError)?)
572+
}
573+
MathOperator::Min => {
574+
let min = lhs.min(rhs);
575+
Ok(Number::from_f64(min).ok_or(Error::TypeError)?)
576+
}
577+
}
555578
}
556579

557580
fn math_operator(lhs: Number, rhs: Number, ops: MathOperator) -> Result<Number, Error> {
558581
match (lhs.as_u64(), rhs.as_u64()) {
559582
(Some(lhs), Some(rhs)) => handle_u64(lhs, rhs, ops),
560-
_ => {
561-
match (lhs.as_i64(), rhs.as_i64()) {
562-
(Some(lhs), Some(rhs)) => handle_i64(lhs, rhs, ops),
563-
_ => {
564-
match (lhs.as_f64(), rhs.as_f64()) {
565-
(Some(lhs), Some(rhs)) => handle_f64(lhs, rhs, ops),
566-
_ => Err(Error::TypeError)
567-
}
568-
}
569-
}
570-
}
583+
_ => match (lhs.as_i64(), rhs.as_i64()) {
584+
(Some(lhs), Some(rhs)) => handle_i64(lhs, rhs, ops),
585+
_ => match (lhs.as_f64(), rhs.as_f64()) {
586+
(Some(lhs), Some(rhs)) => handle_f64(lhs, rhs, ops),
587+
_ => Err(Error::TypeError),
588+
},
589+
},
571590
}
572591
}
573592

0 commit comments

Comments
 (0)