aboutsummaryrefslogtreecommitdiff
path: root/src/operation.rs
blob: a61ae1f19665bff39bcc201f7219ead481a89d92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
use crate::complex::Complex;

pub type Operator = dyn Fn(Complex, Complex) -> Complex;

pub struct Operation {
    sign: char,
    func: Box<Operator>
}

impl Operation {
    pub fn new(sign: char, func: Box<Operator>) -> Self {
        Self { sign, func }
    }

    pub fn sign(&self) -> char {
        self.sign
    }

    pub fn evaluate(&self, a: Complex, b: Complex) -> Complex {
        (self.func)(a, b)
    }
}

#[macro_export]
macro_rules! operations {
    {$($x:expr => $y:expr), *} => {
        vec![$(
            Operation::new($x, Box::new($y)),
        )*]
    };
}