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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
use std::{
    cmp::Ordering,
    ops::{Add, Div, Mul, Neg, Not, Rem, Sub},
};

use crate::{Numeric, PaxValue, Percent, Size, ToPaxValue};

use super::{PaxAny, ToFromPaxAny};

const ANY_ARITH_UNSUPPORTED: &'static str =
    "types that are not representable as PaxValues are not supported in arithmetic expressions";
//----------------------------PaxValue Arithmetic-----------------------------
impl Add for PaxValue {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        match (self, rhs) {
            // Basic types
            (PaxValue::Numeric(a), PaxValue::Numeric(b)) => (a + b).to_pax_value(),
            (PaxValue::String(a), PaxValue::String(b)) => (a + &b).to_pax_value(),
            (PaxValue::String(a), PaxValue::Numeric(b)) => (a + &b.to_string()).to_pax_value(),
            (PaxValue::Numeric(a), PaxValue::String(b)) => (a.to_string() + &b).to_pax_value(),

            // Size and Percent
            (PaxValue::Size(a), PaxValue::Size(b)) => (a + b).to_pax_value(),
            (PaxValue::Percent(a), PaxValue::Percent(b)) => (Percent(a.0 + b.0)).to_pax_value(),
            (PaxValue::Percent(a), PaxValue::Size(b))
            | (PaxValue::Size(b), PaxValue::Percent(a)) => (Size::Percent(a.0) + b).to_pax_value(),
            (PaxValue::Bool(a), PaxValue::Numeric(b))
            | (PaxValue::Numeric(b), PaxValue::Bool(a)) => {
                (Numeric::I64(a as i64) + b).to_pax_value()
            }
            (PaxValue::Size(a), PaxValue::Numeric(b))
            | (PaxValue::Numeric(b), PaxValue::Size(a)) => match a {
                Size::Pixels(px) => Size::Pixels(px + b).to_pax_value(),
                Size::Percent(per) => Size::Percent(per + b).to_pax_value(),
                Size::Combined(px, per) => Size::Combined(px + b, per + b).to_pax_value(),
            },
            (a, b) => panic!("can't add {:?} and {:?}", a, b),
        }
    }
}

impl Mul for PaxValue {
    type Output = Self;

    fn mul(self, rhs: Self) -> Self::Output {
        match (self, rhs) {
            (PaxValue::Numeric(a), PaxValue::Numeric(b)) => (a * b).to_pax_value(),
            (PaxValue::Bool(a), PaxValue::Numeric(b))
            | (PaxValue::Numeric(b), PaxValue::Bool(a)) => {
                (Numeric::I64(a as i64) * b).to_pax_value()
            }
            (PaxValue::Size(a), PaxValue::Numeric(b))
            | (PaxValue::Numeric(b), PaxValue::Size(a)) => match a {
                Size::Pixels(px) => Size::Pixels(px * b).to_pax_value(),
                Size::Percent(per) => Size::Percent(per * b).to_pax_value(),
                Size::Combined(px, per) => Size::Combined(px * b, per * b).to_pax_value(),
            },
            (a, b) => panic!("can't multiply {:?} and {:?}", a, b),
        }
    }
}

impl Sub for PaxValue {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self::Output {
        match (self, rhs) {
            (PaxValue::Numeric(a), PaxValue::Numeric(b)) => (a - b).to_pax_value(),
            // Size and Percent
            (PaxValue::Size(a), PaxValue::Size(b)) => (a - b).to_pax_value(),
            (PaxValue::Percent(a), PaxValue::Percent(b)) => (Percent(a.0 - b.0)).to_pax_value(),
            (PaxValue::Percent(a), PaxValue::Size(b)) => (Size::Percent(a.0) - b).to_pax_value(),
            (PaxValue::Size(a), PaxValue::Percent(b)) => (a - Size::Percent(b.0)).to_pax_value(),
            (PaxValue::Size(a), PaxValue::Numeric(b))
            | (PaxValue::Numeric(b), PaxValue::Size(a)) => match a {
                Size::Pixels(px) => Size::Pixels(px - b).to_pax_value(),
                Size::Percent(per) => Size::Percent(per - b).to_pax_value(),
                Size::Combined(px, per) => Size::Combined(px - b, per - b).to_pax_value(),
            },
            (a, b) => panic!("can't subtract {:?} and {:?}", a, b),
        }
    }
}

impl Div for PaxValue {
    type Output = Self;

    fn div(self, rhs: Self) -> Self::Output {
        match (self, rhs) {
            (PaxValue::Numeric(a), PaxValue::Numeric(b)) => (a / b).to_pax_value(),
            (PaxValue::Size(a), PaxValue::Numeric(b))
            | (PaxValue::Numeric(b), PaxValue::Size(a)) => match a {
                Size::Pixels(px) => Size::Pixels(px / b).to_pax_value(),
                Size::Percent(per) => Size::Percent(per / b).to_pax_value(),
                Size::Combined(px, per) => Size::Combined(px / b, per / b).to_pax_value(),
            },
            (a, b) => panic!("can't divide {:?} and {:?}", a, b),
        }
    }
}

impl Neg for PaxValue {
    type Output = Self;

    fn neg(self) -> Self::Output {
        match self {
            PaxValue::Numeric(a) => (-a).to_pax_value(),
            PaxValue::Size(a) => (-a).to_pax_value(),
            PaxValue::Percent(p) => (-p.0).to_pax_value(),
            PaxValue::Rotation(r) => (-r).to_pax_value(),
            a => panic!("can't negate {:?}", a),
        }
    }
}

impl PartialEq for PaxValue {
    fn eq(&self, rhs: &Self) -> bool {
        match (self, rhs) {
            (PaxValue::Bool(a), PaxValue::Bool(b)) => a == b,
            (PaxValue::Numeric(a), PaxValue::Numeric(b)) => a == b,
            (PaxValue::String(a), PaxValue::String(b)) => a == b,
            (PaxValue::Size(a), PaxValue::Size(b)) => a == b,
            (PaxValue::Percent(a), PaxValue::Percent(b)) => a == b,
            (PaxValue::Color(a), PaxValue::Color(b)) => a == b,
            (PaxValue::Rotation(a), PaxValue::Rotation(b)) => a == b,
            (PaxValue::Option(a), PaxValue::Option(b)) => a == b,
            (PaxValue::Vec(a), PaxValue::Vec(b)) => a == b,
            (PaxValue::Object(a), PaxValue::Object(b)) => a == b,
            (PaxValue::Enum(a, b, c), PaxValue::Enum(d, e, f)) => a == d && b == e && c == f,
            (PaxValue::Range(a, b), PaxValue::Range(c, d)) => a == c && b == d,
            (a, b) => panic!("can't compare {:?} and {:?}", a, b),
        }
    }
}

impl PartialOrd for PaxValue {
    fn partial_cmp(&self, rhs: &Self) -> Option<Ordering> {
        match (self, rhs) {
            (PaxValue::Bool(a), PaxValue::Bool(b)) => a.partial_cmp(b),
            (PaxValue::Numeric(a), PaxValue::Numeric(b)) => a.partial_cmp(b),
            (PaxValue::String(a), PaxValue::String(b)) => a.partial_cmp(b),
            (a, b) => panic!("can't compare {:?} and {:?}", a, b),
        }
    }
}

impl Rem for PaxValue {
    type Output = Self;

    fn rem(self, rhs: Self) -> Self::Output {
        match (self, rhs) {
            (PaxValue::Numeric(a), PaxValue::Numeric(b)) => (a % b).to_pax_value(),
            (a, b) => panic!("can't get remainder of {:?} and {:?}", a, b),
        }
    }
}

impl Not for PaxValue {
    type Output = PaxValue;

    fn not(self) -> Self::Output {
        match self {
            PaxValue::Bool(v) => PaxValue::Bool(!v),
            v => panic!("! operator not valid for {:?}", v),
        }
    }
}

impl PaxValue {
    // the operator && currently can't be overloaded, we use this function instead
    // NOTE: this does not short circuit in the normal way that && does
    pub fn op_and(self, rhs: Self) -> Self {
        match (self, rhs) {
            (PaxValue::Bool(a), PaxValue::Bool(b)) => PaxValue::Bool(a && b),
            (a, b) => panic!("&& operator not valid for {:?} and {:?}", a, b),
        }
    }
    // the operator || currently can't be overloaded, we use this function instead
    // NOTE: this does not short circuit in the normal way that && does
    pub fn op_or(self, rhs: Self) -> Self {
        match (self, rhs) {
            (PaxValue::Bool(a), PaxValue::Bool(b)) => PaxValue::Bool(a || b),
            (a, b) => panic!("&& operator not valid for {:?} and {:?}", a, b),
        }
    }

    pub fn op_not(self) -> Self {
        match self {
            PaxValue::Bool(v) => PaxValue::Bool(!v),
            v => panic!("! operator not valid for {:?}", v),
        }
    }

    // exponentiation, self ^ exp
    pub fn pow(self, exp: Self) -> Self {
        match (self, exp) {
            (PaxValue::Numeric(a), PaxValue::Numeric(b)) => a.pow(b).to_pax_value(),
            (a, b) => panic!("exponentiation not valid between {:?} and {:?}", a, b),
        }
    }

    pub fn min(self, rhs: Self) -> Self {
        match (self, rhs) {
            (PaxValue::Numeric(a), PaxValue::Numeric(b)) => a.min(b).to_pax_value(),
            (a, b) => panic!("min not valid between {:?} and {:?}", a, b),
        }
    }

    pub fn max(self, rhs: Self) -> Self {
        match (self, rhs) {
            (PaxValue::Numeric(a), PaxValue::Numeric(b)) => a.max(b).to_pax_value(),
            (a, b) => panic!("max not valid between {:?} and {:?}", a, b),
        }
    }
}

//----------------------------PaxAny Arithmetic-----------------------------
impl Add for PaxAny {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        match (self, rhs) {
            (PaxAny::Builtin(a), PaxAny::Builtin(b)) => (a + b).to_pax_any(),
            _ => panic!("{}", ANY_ARITH_UNSUPPORTED),
        }
    }
}

impl Mul for PaxAny {
    type Output = Self;

    fn mul(self, rhs: Self) -> Self::Output {
        match (self, rhs) {
            (PaxAny::Builtin(a), PaxAny::Builtin(b)) => (a * b).to_pax_any(),
            _ => panic!("{}", ANY_ARITH_UNSUPPORTED),
        }
    }
}

impl Sub for PaxAny {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self::Output {
        match (self, rhs) {
            (PaxAny::Builtin(a), PaxAny::Builtin(b)) => (a - b).to_pax_any(),
            _ => panic!("{}", ANY_ARITH_UNSUPPORTED),
        }
    }
}

impl Div for PaxAny {
    type Output = Self;

    fn div(self, rhs: Self) -> Self::Output {
        match (self, rhs) {
            (PaxAny::Builtin(a), PaxAny::Builtin(b)) => (a / b).to_pax_any(),
            _ => panic!("{}", ANY_ARITH_UNSUPPORTED),
        }
    }
}

impl Rem for PaxAny {
    type Output = Self;

    fn rem(self, rhs: Self) -> Self::Output {
        match (self, rhs) {
            (PaxAny::Builtin(a), PaxAny::Builtin(b)) => (a % b).to_pax_any(),
            _ => panic!("{}", ANY_ARITH_UNSUPPORTED),
        }
    }
}

impl Neg for PaxAny {
    type Output = Self;

    fn neg(self) -> Self::Output {
        match self {
            PaxAny::Builtin(a) => (-a).to_pax_any(),
            _ => panic!("{}", ANY_ARITH_UNSUPPORTED),
        }
    }
}

impl PartialEq for PaxAny {
    fn eq(&self, rhs: &Self) -> bool {
        match (self, rhs) {
            (PaxAny::Builtin(a), PaxAny::Builtin(b)) => a == b,
            _ => panic!("{}", ANY_ARITH_UNSUPPORTED),
        }
    }
}

impl PartialOrd for PaxAny {
    fn partial_cmp(&self, rhs: &Self) -> Option<Ordering> {
        match (self, rhs) {
            (PaxAny::Builtin(a), PaxAny::Builtin(b)) => a.partial_cmp(b),
            _ => panic!("{}", ANY_ARITH_UNSUPPORTED),
        }
    }
}

impl Not for PaxAny {
    type Output = PaxAny;

    fn not(self) -> Self::Output {
        match self {
            PaxAny::Builtin(v) => (!v).to_pax_any(),
            _ => panic!("{}", ANY_ARITH_UNSUPPORTED),
        }
    }
}

impl PaxAny {
    // the operator && currently can't be overloaded, we use this function instead
    // NOTE: this does not short circuit in the normal way that && does
    pub fn op_and(self, rhs: Self) -> Self {
        match (self, rhs) {
            (PaxAny::Builtin(a), PaxAny::Builtin(b)) => a.op_and(b).to_pax_any(),
            _ => panic!("{}", ANY_ARITH_UNSUPPORTED),
        }
    }
    // the operator || currently can't be overloaded, we use this function instead
    // NOTE: this does not short circuit in the normal way that && does
    pub fn op_or(self, rhs: Self) -> Self {
        match (self, rhs) {
            (PaxAny::Builtin(a), PaxAny::Builtin(b)) => a.op_or(b).to_pax_any(),
            _ => panic!("{}", ANY_ARITH_UNSUPPORTED),
        }
    }

    // exponentiation, self ^ exp
    pub fn pow(self, exp: Self) -> Self {
        match (self, exp) {
            (PaxAny::Builtin(a), PaxAny::Builtin(b)) => a.pow(b).to_pax_any(),
            _ => panic!("{}", ANY_ARITH_UNSUPPORTED),
        }
    }
}