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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
    Appellation: arithmetic <mod>
    Contrib: FL03 <jo3mccain@icloud.com>
*/
use super::{BinOp, BoxedBinOp};
use crate::ops::{Operator, OperatorKind};
use num::traits::NumOps;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use strum::{Display, EnumCount, EnumIs, EnumIter, VariantNames};

macro_rules! operator {
    ($kind:ident: $($op:ident),*) => {
        $(
            operator!($op, $kind);
        )*
    };
    ($op:ident, $kind:ident) => {

        #[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
        #[cfg_attr(feature = "serde", derive(Deserialize, Serialize,))]
        pub struct $op;

        impl $op {
            pub fn new() -> Self {
                Self
            }

            pub fn name(&self) -> &str {
                stringify!($op)
            }
        }

        impl core::fmt::Display for $op {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                write!(f, "{}", self.name())
            }
        }

        impl Operator for $op {

            fn kind(&self) -> OperatorKind {
                OperatorKind::$kind
            }

            fn name(&self) -> &str {
                self.name()
            }
        }
    };

}

macro_rules! operators {
    ($group:ident: [$(($variant:ident, $op:ident, $method:ident)),*]) => {
        #[derive(
            Clone,
            Copy,
            Debug,
            Display,
            EnumCount,
            EnumIs,
            EnumIter,
            Eq,
            Hash,
            Ord,
            PartialEq,
            PartialOrd,
            VariantNames,
        )]
        #[cfg_attr(
            feature = "serde",
            derive(Deserialize, Serialize,),
            serde(rename_all = "lowercase", untagged)
        )]
        #[repr(u8)]
        #[strum(serialize_all = "lowercase")]
        pub enum $group {
            $(
                $variant($op),
            )*
        }

        impl $group {
            $(
                pub fn $method() -> Self {
                    Self::$variant($op::new())
                }
            )*


            pub fn eval<A, B, C>(&self, lhs: A, rhs: B) -> C
            where
                A: NumOps<B, C>,
            {
                self.op().eval(lhs, rhs)
            }

            pub fn op<A, B, C>(self) -> BoxedBinOp<A, B, C>
            where
                A: NumOps<B, C>,
            {
                match self {
                    $(
                        $group::$variant(op) => Box::new(op),
                    )*
                }
            }

            pub fn name(&self) -> &str {
                match self {
                    $(
                        $group::$variant(op) => op.name(),
                    )*
                }
            }
        }

        impl Operator for $group {
            fn kind(&self) -> OperatorKind {
                OperatorKind::Binary
            }

            fn name(&self) -> &str {
                self.name()
            }
        }
    };
}

macro_rules! impl_binary_op {
    ($(($op:ident, $bound:ident, $operator:tt)),*) => {
        $(
            impl_binary_op!($op, $bound, $operator);
        )*

    };
    ($op:ident, $bound:ident, $operator:tt) => {
        operator!($op, Binary);

        impl<A, B, C> BinOp<A, B> for $op
        where
            A: core::ops::$bound<B, Output = C>,
        {
            type Output = C;

            fn eval(&self, lhs: A, rhs: B) -> Self::Output {
                lhs $operator rhs
            }
        }
    };
    (other: $op:ident, $bound:ident, $call:ident) => {
        operator!($op, Binary);

        impl<A, B, C> BinOp<A, B> for $op
        where
            A: $bound<B, Output = C>,
        {
            type Output = C;

            fn eval(&self, lhs: A, rhs: B) -> Self::Output {
                $bound::$call(lhs, rhs)
            }
        }
    };
}

impl_binary_op!((Addition, Add, +), (Division, Div, /), (Multiplication, Mul, *), (Remainder, Rem, %), (Subtraction, Sub, -));

use num::traits::Pow;

impl_binary_op!(other: Power, Pow, pow);

operators!(
    Arithmetic: [
        (Add, Addition, add),
        (Div, Division, div),
        (Mul, Multiplication, mul),
        (Rem, Remainder, rem),
        (Sub, Subtraction, sub)
    ]
);

impl Arithmetic {
    pub fn new(op: Arithmetic) -> Self {
        op
    }
}