Skip to content

Commit a3a2e70

Browse files
authored
Merge pull request #300 from AdExNetwork/separate-math-types-in-handling
Added separate math operation function for each number type
2 parents a3d43fd + 5df9a1d commit a3a2e70

File tree

1 file changed

+30
-29
lines changed

1 file changed

+30
-29
lines changed

primitives/src/targeting/eval.rs

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -306,42 +306,43 @@ enum MathOperator {
306306
Division
307307
}
308308

309-
fn math_operator(lhs: Number, rhs: Number, ops: MathOperator) -> Result<Number, Error> {
310-
match (lhs.as_u64(), rhs.as_u64()) {
311-
(Some(lhs), Some(rhs)) => {
312-
match ops {
313-
MathOperator::Division => {
314-
let divided = lhs.checked_div(rhs).ok_or(Error::TypeError)?;
309+
fn handle_u64(lhs: u64, rhs: u64, ops: MathOperator) -> Result<Number, Error> {
310+
match ops {
311+
MathOperator::Division => {
312+
let divided = lhs.checked_div(rhs).ok_or(Error::TypeError)?;
313+
Ok(divided.into())
314+
}
315+
}
316+
}
315317

316-
Ok(divided.into())
317-
}
318+
fn handle_i64(lhs: i64, rhs: i64, ops: MathOperator) -> Result<Number, Error> {
319+
match ops {
320+
MathOperator::Division => {
321+
let divided = lhs.checked_div(rhs).ok_or(Error::TypeError)?;
322+
Ok(divided.into())
323+
}
324+
}
325+
}
326+
327+
fn handle_f64(lhs: f64, rhs: f64, ops:MathOperator) -> Result<Number, Error> {
328+
match ops {
329+
MathOperator::Division => {
330+
let divided = lhs.div(rhs);
331+
Ok(Number::from_f64(divided).ok_or(Error::TypeError)?)
318332
}
319333
}
334+
}
335+
336+
fn math_operator(lhs: Number, rhs: Number, ops: MathOperator) -> Result<Number, Error> {
337+
match (lhs.as_u64(), rhs.as_u64()) {
338+
(Some(lhs), Some(rhs)) => handle_u64(lhs, rhs, ops),
320339
_ => {
321-
// i64s
322340
match (lhs.as_i64(), rhs.as_i64()) {
323-
(Some(lhs), Some(rhs)) => {
324-
match ops {
325-
MathOperator::Division => {
326-
let divided = lhs.checked_div(rhs).ok_or(Error::TypeError)?;
327-
328-
Ok(divided.into())
329-
}
330-
}
331-
}
341+
(Some(lhs), Some(rhs)) => handle_i64(lhs, rhs, ops),
332342
_ => {
333-
// f64s
334343
match (lhs.as_f64(), rhs.as_f64()) {
335-
(Some(lhs), Some(rhs)) => {
336-
match ops {
337-
MathOperator::Division => {
338-
let divided = lhs.div(rhs);
339-
340-
Ok(Number::from_f64(divided).ok_or(Error::TypeError)?)
341-
}
342-
}
343-
}
344-
_ => return Err(Error::TypeError)
344+
(Some(lhs), Some(rhs)) => handle_f64(lhs, rhs, ops),
345+
_ => Err(Error::TypeError)
345346
}
346347
}
347348
}

0 commit comments

Comments
 (0)