1use crate::Currency;
2
3use crate::{BaseMoney, Decimal, Money};
4use std::ops::{Add, Div, Mul, Rem, Sub};
5
6impl<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 let ret = self
16 .amount()
17 .checked_add(rhs)
18 .expect("addition operation overflow");
19
20 Self::from_decimal(ret)
21 }
22}
23
24impl<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 let ret = self
34 .amount()
35 .checked_sub(rhs)
36 .expect("subtraction operation overflow");
37
38 Self::from_decimal(ret)
39 }
40}
41
42impl<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 let ret = self
52 .amount()
53 .checked_mul(rhs)
54 .expect("multiplication operation overflow");
55
56 Self::from_decimal(ret)
57 }
58}
59
60impl<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 let ret = self
70 .amount()
71 .checked_div(rhs)
72 .expect("division operation overflow");
73
74 Self::from_decimal(ret)
75 }
76}
77
78impl<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 let ret = self
88 .checked_add(rhs.amount())
89 .expect("addition operation overflow");
90
91 Money::from_decimal(ret)
92 }
93}
94
95impl<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 let ret = self
105 .checked_sub(rhs.amount())
106 .expect("subtraction operation overflow");
107
108 Money::from_decimal(ret)
109 }
110}
111
112impl<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 let ret = self
122 .checked_mul(rhs.amount())
123 .expect("multiplication operation overflow");
124
125 Money::from_decimal(ret)
126 }
127}
128
129impl<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 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}