Skip to main content

moneylib/
dec_ops.rs

1use crate::Currency;
2
3use crate::{BaseMoney, Decimal, Money};
4use std::ops::{Add, Div, Mul, Rem, Sub};
5
6// Money + Decimal = Money
7impl<C> Add<Decimal> for Money<C>
8where
9    C: Currency,
10{
11    type Output = Self;
12
13    fn add(self, rhs: Decimal) -> Self::Output {
14        // WARN: PANIC!
15        let ret = self
16            .amount()
17            .checked_add(rhs)
18            .expect("addition operation overflow");
19
20        Self::from_decimal(ret)
21    }
22}
23
24// Money - Decimal = Money
25impl<C> Sub<Decimal> for Money<C>
26where
27    C: Currency,
28{
29    type Output = Self;
30
31    fn sub(self, rhs: Decimal) -> Self::Output {
32        // WARN: PANIC!
33        let ret = self
34            .amount()
35            .checked_sub(rhs)
36            .expect("subtraction operation overflow");
37
38        Self::from_decimal(ret)
39    }
40}
41
42// Money * Decimal = Money
43impl<C> Mul<Decimal> for Money<C>
44where
45    C: Currency,
46{
47    type Output = Self;
48
49    fn mul(self, rhs: Decimal) -> Self::Output {
50        // WARN: PANIC!
51        let ret = self
52            .amount()
53            .checked_mul(rhs)
54            .expect("multiplication operation overflow");
55
56        Self::from_decimal(ret)
57    }
58}
59
60// Money / Decimal = Money
61impl<C> Div<Decimal> for Money<C>
62where
63    C: Currency,
64{
65    type Output = Self;
66
67    fn div(self, rhs: Decimal) -> Self::Output {
68        // WARN: PANIC!
69        let ret = self
70            .amount()
71            .checked_div(rhs)
72            .expect("division operation overflow");
73
74        Self::from_decimal(ret)
75    }
76}
77
78// Decimal + Money = Money
79impl<C> Add<Money<C>> for Decimal
80where
81    C: Currency,
82{
83    type Output = Money<C>;
84
85    fn add(self, rhs: Money<C>) -> Self::Output {
86        // WARN: PANIC!
87        let ret = self
88            .checked_add(rhs.amount())
89            .expect("addition operation overflow");
90
91        Money::from_decimal(ret)
92    }
93}
94
95// Decimal - Money = Money
96impl<C> Sub<Money<C>> for Decimal
97where
98    C: Currency,
99{
100    type Output = Money<C>;
101
102    fn sub(self, rhs: Money<C>) -> Self::Output {
103        // WARN: PANIC!
104        let ret = self
105            .checked_sub(rhs.amount())
106            .expect("subtraction operation overflow");
107
108        Money::from_decimal(ret)
109    }
110}
111
112// Decimal * Money = Money
113impl<C> Mul<Money<C>> for Decimal
114where
115    C: Currency,
116{
117    type Output = Money<C>;
118
119    fn mul(self, rhs: Money<C>) -> Self::Output {
120        // WARN: PANIC!
121        let ret = self
122            .checked_mul(rhs.amount())
123            .expect("multiplication operation overflow");
124
125        Money::from_decimal(ret)
126    }
127}
128
129// Decimal / Money = Money
130impl<C> Div<Money<C>> for Decimal
131where
132    C: Currency,
133{
134    type Output = Money<C>;
135
136    fn div(self, rhs: Money<C>) -> Self::Output {
137        // WARN: PANIC!
138        let ret = self
139            .checked_div(rhs.amount())
140            .expect("division operation overflow");
141
142        Money::from_decimal(ret)
143    }
144}
145
146impl<C> Rem<Decimal> for Money<C>
147where
148    C: Currency,
149{
150    type Output = Money<C>;
151
152    fn rem(self, rhs: Decimal) -> Self::Output {
153        let ret = self
154            .amount()
155            .checked_rem(rhs)
156            .expect("remainder operation failed");
157
158        Money::from_decimal(ret)
159    }
160}