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
//! percent_ops contains trait for percentage operations.
//!
//! It has blanket implementation for types implementing BaseMoney.
use crate::{
BaseMoney, BaseOps, Currency, Decimal,
base::{Amount, DecimalNumber},
macros::dec,
};
/// Trait for percentage operations.
///
/// It has blanket implementation for types implementing BaseMoney.
pub trait PercentOps<C: Currency> {
type Output;
/// Calculates what a certain percentage of a money amount equals.
///
/// `pcn` is the percentage, 20% -> pcn = 20.
///
/// # Examples
///
/// ```
/// use moneylib::{BaseMoney, PercentOps, macros::{dec, money}};
///
/// let price = money!(USD, 200);
/// let tax = price.percent(15).unwrap(); // 15% of $200
/// assert_eq!(tax.amount(), dec!(30));
///
/// // Returns None on overflow
/// let none_on_overflow = price.percent(moneylib::Decimal::MAX);
/// assert!(none_on_overflow.is_none());
/// ```
fn percent<D>(&self, pcn: D) -> Option<Self::Output>
where
D: DecimalNumber;
/// Adds amount by percentage
///
/// `pcn` is the percentage, 20% -> pcn = 20.
///
/// # Examples
///
/// ```
/// use moneylib::{BaseMoney, PercentOps, macros::{dec, money}};
///
/// let price = money!(USD, 100);
/// let after_tax = price.percent_add(20).unwrap(); // $100 + 20% = $120
/// assert_eq!(after_tax.amount(), dec!(120));
///
/// // Returns None on overflow
/// let none_on_overflow = price.percent_add(moneylib::Decimal::MAX);
/// assert!(none_on_overflow.is_none());
/// ```
fn percent_add<D>(&self, pcn: D) -> Option<Self::Output>
where
D: DecimalNumber;
/// Adds self by multiple percentages from original amount.
///
/// Each items in `pcns` are percentage, 20% -> 20.
///
/// Order of `pcns` does **NOT** matter.
///
/// # Examples
///
/// ```
/// use moneylib::{BaseMoney, PercentOps, macros::{dec, money}};
///
/// let base = money!(USD, 1_000);
/// // All percentages are applied to the original base amount:
/// // $1000 + 10% of $1000 + 5% of $1000 = $1000 + $100 + $50 = $1150
/// let total = base.percent_adds_fixed([10, 5]).unwrap();
/// assert_eq!(total.amount(), dec!(1150));
///
/// // Returns None on overflow
/// let none_on_overflow = base.percent_adds_fixed([moneylib::Decimal::MAX]);
/// assert!(none_on_overflow.is_none());
/// ```
fn percent_adds_fixed<D, I>(&self, pcns: I) -> Option<Self::Output>
where
for<'a> &'a I: IntoIterator<Item = &'a D>,
D: DecimalNumber;
/// Adds self by multiple percentages compounding.
///
/// Each items in `pcns` are percentage, 20% -> 20.
///
/// Order of `pcns` **DOES** matter.
///
/// # Examples
///
/// ```
/// use moneylib::{BaseMoney, PercentOps, macros::{dec, money}};
///
/// let base = money!(USD, 1_000);
/// // Percentages compound on the running total:
/// // Step 1: $1000 + 10% of $1000 = $1100
/// // Step 2: $1100 + 5% of $1100 = $1155
/// let total = base.percent_adds_compound([10, 5]).unwrap();
/// assert_eq!(total.amount(), dec!(1155));
///
/// // Returns None on overflow
/// let none_on_overflow = base.percent_adds_compound([moneylib::Decimal::MAX]);
/// assert!(none_on_overflow.is_none());
/// ```
fn percent_adds_compound<D, I>(&self, pcns: I) -> Option<Self::Output>
where
for<'a> &'a I: IntoIterator<Item = &'a D>,
D: DecimalNumber;
/// Substracts amount by percentage(discount)
///
/// `pcn` is the percentage, 20% -> pcn = 20.
///
/// # Examples
///
/// ```
/// use moneylib::{BaseMoney, PercentOps, macros::{dec, money}};
///
/// let price = money!(USD, 200);
/// let after_discount = price.percent_sub(25).unwrap(); // $200 - 25% = $150
/// assert_eq!(after_discount.amount(), dec!(150));
///
/// // Returns None on overflow
/// let none_on_overflow = price.percent_sub(moneylib::Decimal::MAX);
/// assert!(none_on_overflow.is_none());
/// ```
fn percent_sub<D>(&self, pcn: D) -> Option<Self::Output>
where
D: DecimalNumber;
/// Substracts self by multiple percentages in sequence.
///
/// Each items in `pcns` are percentage, 20% -> 20.
///
/// Order of `pcns` **DOES** matter.
///
/// # Examples
///
/// ```
/// use moneylib::{BaseMoney, PercentOps, macros::{dec, money}};
///
/// let gross = money!(USD, 1_000);
/// // Deductions compound on the running total:
/// // Step 1: $1000 - 10% of $1000 = $900
/// // Step 2: $900 - 5% of $900 = $855
/// let net = gross.percent_subs_sequence([10, 5]).unwrap();
/// assert_eq!(net.amount(), dec!(855));
///
/// // Returns None on overflow
/// let none_on_overflow = gross.percent_subs_sequence([moneylib::Decimal::MAX]);
/// assert!(none_on_overflow.is_none());
/// ```
fn percent_subs_sequence<D, I>(&self, pcns: I) -> Option<Self::Output>
where
for<'a> &'a I: IntoIterator<Item = &'a D>,
D: DecimalNumber;
/// Determines what percentage one money is of another.
///
/// # Examples
///
/// ```
/// use moneylib::{BaseMoney, PercentOps, macros::{dec, money}};
///
/// let profit = money!(USD, 50);
/// let revenue = money!(USD, 200);
/// let margin_percentage = profit.percent_of(revenue).unwrap(); // $50 is 25% of $200
/// assert_eq!(margin_percentage, dec!(25));
///
/// // Returns None when dividing by zero
/// let zero = money!(USD, 0);
/// assert!(profit.percent_of(zero).is_none());
/// ```
fn percent_of<M>(&self, rhs: M) -> Option<Decimal>
where
M: Amount<C>;
}
impl<M, C> PercentOps<C> for M
where
M: BaseMoney<C> + BaseOps<C> + Amount<C>,
C: Currency,
{
type Output = M;
fn percent<D>(&self, pcn: D) -> Option<Self::Output>
where
D: DecimalNumber,
{
self.checked_mul(pcn.get_decimal()?)?.checked_div(dec!(100))
}
fn percent_add<D>(&self, pcn: D) -> Option<Self::Output>
where
D: DecimalNumber,
{
self.percent(pcn)?.checked_add(self.to_owned())
}
fn percent_adds_fixed<D, I>(&self, pcns: I) -> Option<Self::Output>
where
for<'a> &'a I: IntoIterator<Item = &'a D>,
D: DecimalNumber,
{
let mut result = self.amount();
for pcn in pcns.into_iter() {
result = result.checked_add(self.percent(pcn.get_decimal()?)?.amount())?;
}
Self::Output::new(result).ok()
}
fn percent_adds_compound<D, I>(&self, pcns: I) -> Option<Self::Output>
where
for<'a> &'a I: IntoIterator<Item = &'a D>,
D: DecimalNumber,
{
let mut result = self.clone();
let mut current = self.clone();
for pcn in pcns.into_iter() {
result = result.checked_add(current.percent(pcn.get_decimal()?)?)?;
current = result.clone();
}
Some(result)
}
fn percent_sub<D>(&self, pcn: D) -> Option<Self::Output>
where
D: DecimalNumber,
{
self.checked_sub(self.percent(pcn)?)
}
fn percent_subs_sequence<D, I>(&self, pcns: I) -> Option<Self::Output>
where
for<'a> &'a I: IntoIterator<Item = &'a D>,
D: DecimalNumber,
{
let mut result = self.clone();
let mut current = self.clone();
for pcn in pcns.into_iter() {
result = result.checked_sub(current.percent(pcn.get_decimal()?)?)?;
current = result.clone();
}
Some(result)
}
fn percent_of<D>(&self, rhs: D) -> Option<Decimal>
where
D: Amount<C>,
{
(self.checked_div(rhs.get_decimal()?)?)
.amount()
.checked_mul(dec!(100))
}
}