#[derive(Debug)]
pub enum Operation {
Add,
Subtract,
Multiply,
Divide,
Exponent,
Modulo,
}
impl Operation {
pub fn run(&self, lhs: f64, rhs: f64) -> f64 {
match self {
Self::Add => lhs + rhs,
Self::Subtract => lhs - rhs,
Self::Multiply => lhs * rhs,
Self::Divide => lhs / rhs,
Self::Exponent => lhs.powf(rhs.try_into().unwrap()),
Self::Modulo => lhs % rhs,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works_with_addition() {
let operation = Operation::Add;
assert_eq!(operation.run(10.0, 2.0), 12.0);
}
#[test]
fn it_works_with_subtraction() {
let operation = Operation::Subtract;
assert_eq!(operation.run(10.0, 2.0), 8.0);
}
#[test]
fn it_works_with_multiplication() {
let operation = Operation::Multiply;
assert_eq!(operation.run(10.0, 2.0), 20.0);
}
#[test]
fn it_works_with_division() {
let operation = Operation::Divide;
assert_eq!(operation.run(10.0, 2.0), 5.0);
}
}