#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Operation {
Add,
Multiply
}
impl From<bool> for Operation {
fn from(value: bool) -> Self {
match value {
true => Operation::Add,
false => Operation::Multiply
}
}
}
#[macro_export]
macro_rules! safe_math {
($in_x:expr, $in_y:expr, $operation:expr) => {
{
let mut error = false;
let mut set_error = || -> i128 {
error = true;
1
};
if $in_x > 9223372036854775807 || $in_y > 9223372036854775807 {
_ = set_error();
}
let result_x: Result<i128, _> = i128::try_from($in_x);
let x = match result_x {
Ok(v) => v,
Err(_) => set_error()
};
let result_y: Result<i128, _> = i128::try_from($in_y);
let y = match result_y {
Ok(v) => v,
Err(_) => set_error()
};
let returned_value = match x < y {
true => y,
false => x
};
if !error {
match $operation {
Operation::Add => {
let sum: i128 = x+y;
let result_sum = i64::try_from(sum);
match result_sum {
Ok(v) => Result::Ok(v),
Err(_) => Result::Err(returned_value)
}
},
Operation::Multiply => {
let product: i128 = x*y;
let result_product = i64::try_from(product);
match result_product {
Ok(v) => Result::Ok(v),
Err(_) => Result::Err(returned_value)
}
}
}
} else {
Result::Err(returned_value)
}
}
}
}