aboutsummaryrefslogtreecommitdiff
path: root/budget/src/lib.rs
diff options
context:
space:
mode:
authorYaroslav <contact@yaroslavps.com>2020-10-06 14:13:14 +0300
committerYaroslav <contact@yaroslavps.com>2020-10-06 14:13:14 +0300
commit742b9fa46d74762f58ae939afd980a532cc4636f (patch)
tree0cd372442e5518dece1a9ec371ae71dccc5faae4 /budget/src/lib.rs
parentcc688c4dc73d3b13be5aba1bd292cc31265c5d04 (diff)
downloadfinbudg-0.1.2.tar.gz
finbudg-0.1.2.zip
Account for days elapsed based on latest datev0.1.2
Instead of counting the number of days for the averge through the number of iterations, let it be the difference between the latest date on entry and the start of the period. Id est: * 'Missing' dates from the input are implicit. * The order of the days doesn't affect the output (although it doesn't make sense to put the days out of order).
Diffstat (limited to 'budget/src/lib.rs')
-rw-r--r--budget/src/lib.rs31
1 files changed, 17 insertions, 14 deletions
diff --git a/budget/src/lib.rs b/budget/src/lib.rs
index e8bb8f3..7d7c390 100644
--- a/budget/src/lib.rs
+++ b/budget/src/lib.rs
@@ -51,6 +51,7 @@ pub struct Calculated {
pub balance: f64,
pub days_left: f64,
pub days_left_essential: f64,
+ pub last_day: NaiveDate,
}
#[derive(PartialEq, Eq, Debug)]
@@ -96,7 +97,11 @@ pub fn parse_account(path: &str) -> Result<Account, ParseError> {
}
}
-pub fn calculate(account: &Account) -> Calculated {
+pub fn calculate(account: &Account) -> Option<Calculated> {
+ if account.days.len() < 1 {
+ return None;
+ }
+
let mut calculated = Calculated {
all_day_average: 0.0,
essential_day_average: 0.0,
@@ -107,12 +112,13 @@ pub fn calculate(account: &Account) -> Calculated {
balance: 0.0,
days_left: 0.0,
days_left_essential: 0.0,
+ last_day: account.days.last().unwrap().date,
};
- let mut days_calculated = 0;
-
for day in account.days.iter() {
- days_calculated += 1;
+ if day.date > calculated.last_day {
+ calculated.last_day = day.date;
+ }
for expense in day.expenses.iter() {
calculated.total += expense.price;
@@ -135,15 +141,18 @@ pub fn calculate(account: &Account) -> Calculated {
}
}
- calculated.all_day_average = calculated.total / days_calculated as f64;
+ let days_elapsed =
+ (calculated.last_day - account.start_date).num_days() + 1;
+
+ calculated.all_day_average = calculated.total / days_elapsed as f64;
calculated.essential_day_average =
- calculated.essential_subtotal / days_calculated as f64;
+ calculated.essential_subtotal / days_elapsed as f64;
for (category, subtotal) in calculated.categories_subtotal.iter() {
calculated.categories_day_average
.insert(
category.clone(),
- subtotal / days_calculated as f64,
+ subtotal / days_elapsed as f64,
);
}
@@ -153,11 +162,5 @@ pub fn calculate(account: &Account) -> Calculated {
calculated.days_left_essential =
calculated.balance / calculated.essential_day_average;
- calculated
-}
-
-pub fn get_calculated(path: &str) -> Result<Calculated, ParseError> {
- let account = parse_account(path)?;
-
- Ok(calculate(&account))
+ Some(calculated)
}