rust_finprim 0.5.1

Various finance and accounting calculations/formulas implemented Rust
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
use crate::amort_dep_tax::DepreciationPeriod;
use crate::FloatLike;
use crate::RoundingMode;

#[cfg(feature = "std")]
/// Straight Line Depreciation (SLN)
///
/// Calculates the depreciation schedule for an asset using the straight-line method.
///
/// # Feature
/// This function requires the `std` feature to be enabled as it uses the `std::Vec`. `sln_into`
/// can be used in a `no_std` environment as any allocation is done by the caller.
///
/// # Arguments
/// * `cost` - The initial cost of the asset
/// * `salvage` - The estimated salvage value of the asset at the end of its useful life
/// * `life` - The number of periods over which the asset will be depreciated
///
///
/// # Returns
/// * A vector of `DepreciationPeriod` instances representing each period in the depreciation schedule.
///
/// # Examples
/// * $10,000 asset, $1,000 salvage value, 5 year life
/// ```
/// use rust_finprim::amort_dep_tax::sln;
///
/// let cost = 10_000.0;
/// let salvage = 1_000.0;
/// let life = 5;
/// let schedule = sln(cost, salvage, life);
/// ```
pub fn sln<T: FloatLike>(cost: T, salvage: T, life: u32) -> Vec<DepreciationPeriod<T>> {
    let mut periods = vec![DepreciationPeriod::default(); life as usize];
    sln_into(periods.as_mut_slice(), cost, salvage);
    periods
}

/// Straight Line Depreciation (SLN) Into
///
/// Calculates the depreciation schedule for an asset using the straight-line method, mutating a
/// "slice" of `DepreciationPeriod`.
///
/// # Arguments
/// * `slice` - A mutable slice of `DepreciationPeriod` instances to be filled with the depreciation schedule.
///
/// **Warning**: The length of the slice should be as long as the life as the asset or there will
/// be unexpected behavior.
/// * `cost` - The initial cost of the asset
/// * `salvage` - The estimated salvage value of the asset at the end of its useful life
///
/// # Examples
/// * $10,000 asset, $1,000 salvage value, 5 year life
/// ```
/// use rust_finprim::amort_dep_tax::{DepreciationPeriod, sln_into};
///
/// let life = 5;
/// let cost = 10_000.0;
/// let salvage = 1_000.0;
///
/// let mut schedule = vec![DepreciationPeriod::default(); life as usize];
/// sln_into(&mut schedule, cost, salvage);
/// ```
pub fn sln_into<T: FloatLike>(slice: &mut [DepreciationPeriod<T>], cost: T, salvage: T) {
    let life = slice.len();
    let depreciation_expense = (cost - salvage) / T::from_usize(life);

    let mut remaining_book_value = cost;
    for (period, item) in slice.iter_mut().enumerate() {
        remaining_book_value -= depreciation_expense;
        item.period = period as u32 + 1;
        item.depreciation_expense = depreciation_expense;
        item.remaining_book_value = remaining_book_value;
    }
}

#[cfg(feature = "std")]
/// Declining Balance Depreciation (DB)
///
/// Calculates the depreciation schedule for an asset using the declining balance method given a
/// declining balance factor (e.g. double-declining balance).
///
/// # Feature
/// This function requires the `std` feature to be enabled as it uses the `std::Vec`. `sln_into`
/// can be used in a `no_std` environment as any allocation is done by the caller.
///
/// # Arguments
/// * `cost` - The initial cost of the assert
/// * `salvage` - The estimated salvage value of the asset at the end of its useful life
/// * `life` - The number of periods over which the asset will be depreciated
/// * `factor` (optional) - The factor by which the straight-line depreciation rate is multiplied (default is 2 for double-declining balance)
/// * `round` (optional) - A tuple specifying the number of decimal places and a rounding strategy for the amounts `(dp, RoundingMode)`,
/// default is no rounding of calculations. The final depreciation expense is adjusted to ensure the remaining book value is equal to the salvage value.
///
/// If rounding is enabled, the final period will be adjusted to "zero" out the remaining book
/// value to the salvage value.
///
/// # Returns
/// * A vector of `DepreciationPeriod` instances representing each period in the depreciation schedule.
///
/// # Examples
/// * $10,000 asset, $1,000 salvage value, 5 year life
/// ```
/// use rust_finprim::amort_dep_tax::db;
///
/// let cost = 10_000.0;
/// let salvage = 1_000.0;
/// let life = 5;
/// let schedule = db(cost, salvage, life, None, None);
/// ```
pub fn db<T: FloatLike>(
    cost: T,
    salvage: T,
    life: u32,
    factor: Option<T>,
    round: Option<(u32, RoundingMode, T)>,
) -> Vec<DepreciationPeriod<T>> {
    let mut periods = vec![DepreciationPeriod::default(); life as usize];
    db_into(periods.as_mut_slice(), cost, salvage, factor, round);
    periods
}

/// Declining Balance Depreciation (DB) Into
///
/// Calculates the depreciation schedule for an asset using the declining balance method given a
/// declining balance factor (e.g. double-declining balance), mutating a "slice" of DepreciationPeriod.
///
/// # Arguments
/// * `slice` - A mutable slice of `DepreciationPeriod` instances to be filled with the depreciation schedule.
///
/// **Warning**: The length of the slice should be as long as the life as the asset or there will
/// be unexpected behavior.
/// * `cost` - The initial cost of the assert
/// * `salvage` - The estimated salvage value of the asset at the end of its useful life
/// * `factor` (optional) - The factor by which the straight-line depreciation rate is multiplied (default is 2 for double-declining balance)
/// * `round` (optional) - A tuple specifying the number of decimal places and a rounding strategy for the amounts `(dp, RoundingMode)`,
/// default is no rounding of calculations. The final depreciation expense is adjusted to ensure the remaining book value is equal to the salvage value.
///
/// If rounding is enabled, the final period will be adjusted to "zero" out the remaining book
/// value to the salvage value.
///
/// # Examples
/// * $10,000 asset, $1,000 salvage value, 5 year life
/// ```
/// use rust_finprim::amort_dep_tax::{DepreciationPeriod, db_into};
///
/// let life = 5;
/// let cost = 10_000.0;
/// let salvage = 1_000.0;
///
/// let mut schedule = vec![DepreciationPeriod::default(); life as usize];
/// db_into(&mut schedule, cost, salvage, None, None);
/// ```
pub fn db_into<T: FloatLike>(
    slice: &mut [DepreciationPeriod<T>],
    cost: T,
    salvage: T,
    factor: Option<T>,
    round: Option<(u32, RoundingMode, T)>,
) {
    let factor = factor.unwrap_or(T::two());
    let life = slice.len();

    let mut remain_bv = cost;
    let mut accum_dep = T::zero();
    for (period, item) in slice.iter_mut().enumerate() {
        let mut dep_exp = factor * (cost - accum_dep) / T::from_usize(life);
        if let Some((dp, rounding, epsilon)) = round {
            dep_exp = dep_exp.round_with_mode(dp, rounding, epsilon);
        }

        if dep_exp > remain_bv - salvage {
            dep_exp = remain_bv - salvage;
        }
        accum_dep += dep_exp;
        remain_bv -= dep_exp;

        item.period = period as u32 + 1;
        item.depreciation_expense = dep_exp;
        item.remaining_book_value = remain_bv;
    }

    if round.is_some() {
        let last = slice.last_mut().unwrap();
        last.depreciation_expense += last.remaining_book_value - salvage;
        last.remaining_book_value = salvage;
    }
}

#[cfg(feature = "std")]
/// Sum of the Years Digits (SYD)
///
/// Calculates the depreciation schedule for an asset using the sum of the years' digits method.
/// The sum of the years' digits method is an accelerated depreciation method that allocates
/// more depreciation expense to the early years of an asset's life.
///
/// # Feature
/// This function requires the `std` feature to be enabled as it uses the `std::Vec`. `syd_into`
/// can be used in a `no_std` environment as any allocation is done by the caller.
///
/// # Arguments
/// * `cost` - The initial cost of the asset
/// * `salvage` - The estimated salvage value of the asset at the end of its useful life
/// * `life` - The number of periods over which the asset will be depreciated
/// * `round` (optional) - A tuple specifying the number of decimal places and a rounding strategy for the amounts `(dp, RoundingMode)`,
/// default is no rounding of calculations. The final depreciation expense is adjusted to ensure the remaining book value is equal to the salvage value.
///
/// If rounding is enabled, the final period will be adjusted to "zero" out the remaining book value to the salvage value.
///
/// # Returns
/// * A vector of `DepreciationPeriod` instances representing each period in the depreciation schedule.
///
/// # Examples
/// * $10,000 asset, $1,000 salvage value, 5 year life
/// ```
/// use rust_finprim::amort_dep_tax::syd;
///
/// let cost = 10_000.0;
/// let salvage = 1_000.0;
/// let life = 5;
/// let schedule = syd(cost, salvage, life, None);
/// ```
pub fn syd<T: FloatLike>(
    cost: T,
    salvage: T,
    life: u32,
    round: Option<(u32, RoundingMode, T)>,
) -> Vec<DepreciationPeriod<T>> {
    let mut periods = vec![DepreciationPeriod::default(); life as usize];
    syd_into(periods.as_mut_slice(), cost, salvage, round);
    periods
}

/// Sum of the Years Digits (SYD) Into
///
/// Calculates the depreciation schedule for an asset using the sum of the years' digits method.
/// The sum of the years' digits method is an accelerated depreciation method that allocates
/// more depreciation expense to the early years of an asset's life. Mutates a slice of
/// `DepreciationPeriod`.
///
/// # Arguments
/// * `slice` - A mutable slice of `DepreciationPeriod` instances to be filled with the depreciation schedule.
///
/// **Warning**: The length of the slice should be as long as the life as the asset or there will
/// be unexpected behavior.
/// * `salvage` - The estimated salvage value of the asset at the end of its useful life
/// * `life` - The number of periods over which the asset will be depreciated
/// * `round` (optional) - A tuple specifying the number of decimal places and a rounding strategy for the amounts `(dp, RoundingMode)`,
/// default is no rounding of calculations. The final depreciation expense is adjusted to ensure the remaining book value is equal to the salvage value.
///
/// If rounding is enabled, the final period will be adjusted to "zero" out the remaining book value to the salvage value.
///
/// # Returns
/// * A vector of `DepreciationPeriod` instances representing each period in the depreciation schedule.
///
/// # Examples
/// * $10,000 asset, $1,000 salvage value, 5 year life
/// ```
/// use rust_finprim::amort_dep_tax::{DepreciationPeriod, syd_into};
///
/// let life = 5;
/// let cost = 10_000.0;
/// let salvage = 1_000.0;
///
/// let mut schedule = vec![DepreciationPeriod::default(); life as usize];
/// syd_into(&mut schedule, cost, salvage, None);
/// ```
pub fn syd_into<T: FloatLike>(
    slice: &mut [DepreciationPeriod<T>],
    cost: T,
    salvage: T,
    round: Option<(u32, RoundingMode, T)>,
) {
    let life = slice.len();
    let mut remain_bv = cost;
    let mut accum_dep = T::zero();
    let sum_of_years = T::from_usize(life * (life + 1)) / T::two();
    for (period, item) in slice.iter_mut().enumerate() {
        let mut dep_exp = (cost - salvage) * T::from_usize(life - (period)) / sum_of_years;
        if let Some((dp, rounding, epsilon)) = round {
            dep_exp = dep_exp.round_with_mode(dp, rounding, epsilon)
        };

        accum_dep += dep_exp;
        remain_bv -= dep_exp;

        item.period = period as u32 + 1;
        item.depreciation_expense = dep_exp;
        item.remaining_book_value = remain_bv;
    }

    if round.is_some() {
        let last = slice.last_mut().unwrap();
        last.depreciation_expense += last.remaining_book_value - salvage;
        last.remaining_book_value = salvage;
    }
}

#[cfg(feature = "std")]
/// MACRS Deprectiation
///
/// Calculates the depreciation schedule for an asset using the Modified Accelerated Cost Recovery
/// System (MACRS method). MACRS is a depreciation method allowed by the IRS for tax purposes.
///
/// # Feature
/// This function requires the `std` feature to be enabled as it uses the `std::Vec`. `sln_into`
/// can be used in a `no_std` environment as any allocation is done by the caller.
///
/// # Arguments
/// * `cost` - The initial cost of the asset
/// * `rates` - A slice representing the MACRS depreciation rates for all periods of the asset's
/// life, starting with the first year (period 1) and ending with the last year (period 2). Rates
/// for each period can be found in IRS Publication 946 or other tax resources. The rates should
/// be in decimal form (e.g., 0.20 for 20%).
///
/// # Returns
/// * A vector of `DepreciationPeriod` instances representing each period in the depreciation schedule.
/// The length of the vector will be equal to the number of rates provided.
///
/// # Examples
/// * $10,000 asset, MACRS rates for 5 year life
/// ```
/// use rust_finprim::amort_dep_tax::macrs;
///
/// let cost = 10_000.0;
/// let rates = vec![
///    0.20,
///    0.32,
///    0.1920,
///    0.1152,
///    0.1152,
///    0.0576
/// ];
/// let schedule = macrs(cost, &rates);
/// ```
pub fn macrs<T: FloatLike>(cost: T, rates: &[T]) -> Vec<DepreciationPeriod<T>> {
    let mut periods = vec![DepreciationPeriod::default(); rates.len()];
    macrs_into(periods.as_mut_slice(), cost, rates);
    periods
}

/// MACRS Deprectiation Into
///
/// Calculates the depreciation schedule for an asset using the Modified Accelerated Cost Recovery
/// System (MACRS method). MACRS is a depreciation method allowed by the IRS for tax purposes.
/// Mutates a slice of `DepreciationPeriod`.
///
/// # Arguments
/// * `slice` - A mutable slice of `DepreciationPeriod` instances to be filled with the depreciation schedule.
///
/// **Warning**: The length of the slice should be as long as the life as the asset, in this case,
/// that is as long as the number of rates provided. If the length of the slice is not equal to
/// the number of rates, this will panic.
/// * `cost` - The initial cost of the asset
/// * `rates` - A slice representing the MACRS depreciation rates for all periods of the asset's
/// life, starting with the first year (period 1) and ending with the last year (period 2). Rates
/// for each period can be found in IRS Publication 946 or other tax resources. The rates should
/// be in decimal form (e.g., 0.20 for 20%).
///
/// # Returns
/// * A vector of `DepreciationPeriod` instances representing each period in the depreciation schedule.
/// The length of the vector will be equal to the number of rates provided.
///
/// # Examples
/// * $10,000 asset, MACRS rates for 5 year life
/// ```
/// use rust_finprim::amort_dep_tax::{DepreciationPeriod, macrs_into};
///
/// let cost = 10_000.0;
/// let rates = vec![
///    0.20,
///    0.32,
///    0.1920,
///    0.1152,
///    0.1152,
///    0.0576
/// ];
/// let life = rates.len() as u32;
/// let mut schedule = vec![DepreciationPeriod::default(); life as usize];
/// macrs_into(&mut schedule, cost, &rates);
/// ```
pub fn macrs_into<T: FloatLike>(slice: &mut [DepreciationPeriod<T>], cost: T, rates: &[T]) {
    if slice.len() != rates.len() {
        panic!("Length of slice must be equal to the number of rates");
    }
    let mut remain_bv = cost;
    for (period, &rate) in rates.iter().enumerate() {
        let dep_exp = cost * rate;
        remain_bv -= dep_exp;
        let item = &mut slice[period];
        item.period = period as u32 + 1;
        item.depreciation_expense = dep_exp;
        item.remaining_book_value = remain_bv;
    }
}

// since the underlying logic is the same. Just the allocation is different.
#[cfg(test)]
#[cfg(feature = "std")]
mod tests {
    use super::*;

    #[cfg(not(feature = "std"))]
    extern crate std;
    #[cfg(not(feature = "std"))]
    use std::{assert_eq, println, vec};

    #[test]
    fn test_macrs() {
        let cost = 10_000.0;
        let rates = vec![0.20, 0.32, 0.1920, 0.1152, 0.1152, 0.0576];
        const LIFE: usize = 6;
        let mut schedule: [DepreciationPeriod<f64>; LIFE] = [DepreciationPeriod::default(); LIFE];
        macrs_into(&mut schedule, cost, &rates);
        schedule.iter().for_each(|period| println!("{:?}", period));
        assert_eq!(schedule.len(), rates.len());
        assert_eq!(schedule[0].depreciation_expense, 2000.0);
        assert_eq!(schedule[0].remaining_book_value, 8000.0);
        assert_eq!(schedule[5].depreciation_expense, 576.0);
        assert_eq!(schedule[5].remaining_book_value, 0.0);
    }

    #[test]
    fn test_syd() {
        struct TestCase {
            cost: f64,
            salvage: f64,
            life: u32,
            round: Option<(u32, RoundingMode, f64)>,
            expected: f64,
        }

        impl TestCase {
            fn new(cost: f64, salvage: f64, life: u32, round: Option<(u32, RoundingMode)>, expected: f64) -> Self {
                Self {
                    cost,
                    salvage,
                    life,
                    round: round.map(|(dp, mode)| (dp, mode, 1e-5)),
                    expected,
                }
            }
        }

        let cases = [
            TestCase::new(10_000.00, 1_000.00, 5, None, 600.00),
            TestCase::new(9_000.00, 1_000.00, 5, Some((2, RoundingMode::HalfToEven)), 533.33),
            TestCase::new(9_000.00, 1_500.00, 10, Some((2, RoundingMode::HalfToEven)), 136.36),
        ];
        for case in &cases {
            let schedule = syd(case.cost, case.salvage, case.life, case.round);
            schedule.iter().for_each(|period| println!("{:?}", period));
            assert_eq!(schedule.len(), case.life as usize);
            assert!((schedule.last().unwrap().depreciation_expense - case.expected).abs() < 1e-5);
        }
    }

    #[test]
    fn test_db() {
        struct TestCase {
            cost: f64,
            salvage: f64,
            life: u32,
            factor: Option<f64>,
            round: Option<(u32, RoundingMode, f64)>,
            expected: f64,
        }
        impl TestCase {
            fn new(
                cost: f64,
                salvage: f64,
                life: u32,
                factor: Option<f64>,
                round: Option<(u32, RoundingMode)>,
                expected: f64,
            ) -> Self {
                Self {
                    cost,
                    salvage,
                    life,
                    factor,
                    round: round.map(|(dp, mode)| (dp, mode, 1e-5)),
                    expected,
                }
            }
        }

        let cases = [
            TestCase::new(4_000.00, 1_000.00, 5, None, None, 0.00),
            TestCase::new(10_000.00, 1_000.00, 5, None, None, 296.00),
            TestCase::new(10_000.00, 1_000.00, 10, None, None, 268.435456),
            TestCase::new(
                10_000.00,
                1_000.00,
                10,
                None,
                Some((2, RoundingMode::HalfToEven)),
                342.18,
            ),
        ];
        for case in &cases {
            let schedule = db(case.cost, case.salvage, case.life, case.factor, case.round);
            schedule.iter().for_each(|period| println!("{:?}", period));
            assert_eq!(schedule.len(), case.life as usize);
            assert_eq!(schedule.last().unwrap().depreciation_expense, case.expected);
        }
    }

    #[test]
    fn test_sln() {
        let cost = 10_000.0;
        let salvage = 1_000.0;
        let life = 5;
        let schedule = sln(cost, salvage, life);
        schedule.iter().for_each(|period| println!("{:?}", period));
        assert_eq!(schedule.len(), 5);
        assert_eq!(schedule[0].depreciation_expense, 1800.0);
        assert_eq!(schedule[0].remaining_book_value, 8200.0);
    }
}