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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
use std::{cmp::Ordering, fmt::Display};

use serde::{Deserialize, Serialize};

use crate::{Interpolatable, Size};
#[derive(Clone, Serialize, Deserialize)]
#[serde(crate = "crate::serde")]
#[derive(Debug, Copy)]
pub enum Numeric {
    I8(i8),
    I16(i16),
    I32(i32),
    I64(i64),
    U8(u8),
    U16(u16),
    U32(u32),
    U64(u64),
    F64(f64),
    F32(f32),
    ISize(isize),
    USize(usize),
}

impl Display for Numeric {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        fn fmt_num<V: Display>(
            f: &mut std::fmt::Formatter<'_>,
            v: V,
        ) -> Result<(), std::fmt::Error> {
            write!(f, "{:.2}", v)
        }
        match self {
            Numeric::I8(v) => fmt_num(f, v),
            Numeric::I16(v) => fmt_num(f, v),
            Numeric::I32(v) => fmt_num(f, v),
            Numeric::I64(v) => fmt_num(f, v),
            Numeric::U8(v) => fmt_num(f, v),
            Numeric::U16(v) => fmt_num(f, v),
            Numeric::U32(v) => fmt_num(f, v),
            Numeric::U64(v) => fmt_num(f, v),
            Numeric::F64(v) => fmt_num(f, v),
            Numeric::F32(v) => fmt_num(f, v),
            Numeric::ISize(v) => fmt_num(f, v),
            Numeric::USize(v) => fmt_num(f, v),
        }
    }
}

impl PartialEq for Numeric {
    fn eq(&self, rhs: &Self) -> bool {
        match (self.is_float(), rhs.is_float()) {
            (false, false) => self.to_int() == rhs.to_int(),
            _ => (self.to_float() - rhs.to_float()) < 1e-6,
        }
    }
}

impl Default for Numeric {
    fn default() -> Self {
        Self::F64(0.0)
    }
}

macro_rules! impl_numeric_arith {
    ($trait:ident, $method:ident, $op:tt) => {
        impl std::ops::$trait for &Numeric {
            type Output = Numeric;

            fn $method(self, rhs: Self) -> Self::Output {

                // TBD: might want to be more granular here at some point
                match (self.is_float(), rhs.is_float()) {
                    (false, false) => Numeric::I64(self.to_int() $op rhs.to_int()),
                    _ => Numeric::F64(self.to_float() $op rhs.to_float()),
                }
            }
        }
        impl std::ops::$trait for Numeric {
            type Output = Numeric;

            fn $method(self, rhs: Self) -> Self::Output {
                &self $op &rhs
            }
        }
    };
}

impl_numeric_arith!(Add, add, +);
impl_numeric_arith!(Sub, sub, -);
impl_numeric_arith!(Mul, mul, *);
impl_numeric_arith!(Div, div, /);
impl_numeric_arith!(Rem, rem, %);

impl std::ops::Neg for Numeric {
    type Output = Self;

    fn neg(self) -> Self::Output {
        use Numeric::*;
        match self {
            I8(a) => I8(-a),
            I16(a) => I16(-a),
            I32(a) => I32(-a),
            I64(a) => I64(-a),
            F32(a) => F32(-a),
            F64(a) => F64(-a),
            ISize(a) => ISize(-a),
            _ => panic!("tried to negate numeric that is unsigned"),
        }
    }
}

impl PartialOrd for Numeric {
    fn partial_cmp(&self, rhs: &Self) -> Option<Ordering> {
        match (self.is_float(), rhs.is_float()) {
            (false, false) => self.to_int().partial_cmp(&rhs.to_int()),
            _ => self.to_float().partial_cmp(&rhs.to_float()),
        }
    }
}

macro_rules! impl_to_from {
    ($return_type:ty, $Variant:path) => {
        impl From<&Numeric> for $return_type {
            fn from(value: &Numeric) -> Self {
                use Numeric::*;
                match *value {
                    I8(a) => a as $return_type,
                    I16(a) => a as $return_type,
                    I32(a) => a as $return_type,
                    I64(a) => a as $return_type,
                    U8(a) => a as $return_type,
                    U16(a) => a as $return_type,
                    U32(a) => a as $return_type,
                    U64(a) => a as $return_type,
                    F32(a) => a as $return_type,
                    F64(a) => a as $return_type,
                    ISize(a) => a as $return_type,
                    USize(a) => a as $return_type,
                }
            }
        }

        impl From<Numeric> for $return_type {
            fn from(value: Numeric) -> Self {
                (&value).into()
            }
        }

        impl From<$return_type> for Numeric {
            fn from(value: $return_type) -> Self {
                $Variant(value)
            }
        }
        impl From<&$return_type> for Numeric {
            fn from(value: &$return_type) -> Self {
                $Variant(*value)
            }
        }
    };
}

impl_to_from!(f32, Numeric::F32);
impl_to_from!(f64, Numeric::F64);
impl_to_from!(i8, Numeric::I8);
impl_to_from!(i16, Numeric::I16);
impl_to_from!(i32, Numeric::I32);
impl_to_from!(i64, Numeric::I64);
impl_to_from!(u8, Numeric::U8);
impl_to_from!(u16, Numeric::U16);
impl_to_from!(u32, Numeric::U32);
impl_to_from!(u64, Numeric::U64);
impl_to_from!(isize, Numeric::ISize);
impl_to_from!(usize, Numeric::USize);

impl Numeric {
    pub fn to_float(&self) -> f64 {
        self.into()
    }

    pub fn to_int(&self) -> i64 {
        self.into()
    }

    pub fn is_float(&self) -> bool {
        match self {
            Numeric::F64(_) | Numeric::F32(_) => true,
            _ => false,
        }
    }

    pub fn pow(self, exp: Self) -> Self {
        match (self.is_float(), exp.is_float()) {
            (false, false) => Numeric::I64(self.to_int().pow(exp.into())),
            _ => Numeric::F64(self.to_float().powf(exp.to_float())),
        }
    }

    pub fn min(self, other: Self) -> Self {
        match (self.is_float(), other.is_float()) {
            (false, false) => Numeric::I64(self.to_int().min(other.to_int())),
            _ => Numeric::F64(self.to_float().min(other.to_float())),
        }
    }

    pub fn max(self, other: Self) -> Self {
        match (self.is_float(), other.is_float()) {
            (false, false) => Numeric::I64(self.to_int().max(other.to_int())),
            _ => Numeric::F64(self.to_float().max(other.to_float())),
        }
    }
}

impl Interpolatable for Numeric {
    fn interpolate(&self, other: &Self, t: f64) -> Self {
        Numeric::F64(Into::<f64>::into(self).interpolate(&other.into(), t))
    }
}

impl From<Size> for Numeric {
    fn from(value: Size) -> Self {
        match value {
            Size::Pixels(x) | Size::Percent(x) => x,
            Size::Combined(_, _) => unreachable!("Cannot coerce a combined size to a numeric"),
        }
    }
}