From 742b9fa46d74762f58ae939afd980a532cc4636f Mon Sep 17 00:00:00 2001 From: Yaroslav Date: Tue, 6 Oct 2020 14:13:14 +0300 Subject: Account for days elapsed based on latest date 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). --- src/main.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 04d5277..5c64959 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,7 +33,7 @@ fn main() { ::std::process::exit(1); } }; - let calculated = budget::calculate(&account); + let maybe_calculated = budget::calculate(&account); if no_color && !force_color { colored::control::set_override(false); @@ -41,7 +41,7 @@ fn main() { colored::control::set_override(true); } - output(account, calculated); + output(account, maybe_calculated); } fn get_cli_matches() -> ArgMatches<'static> { @@ -68,7 +68,7 @@ fn get_cli_matches() -> ArgMatches<'static> { .get_matches() } -fn output(account: Account, calculated: Calculated) { +fn output(account: Account, maybe_calculated: Option) { println!( "{}", format!( @@ -78,22 +78,23 @@ fn output(account: Account, calculated: Calculated) { ).cyan(), ); - let last_day = match account.days.last() { - Some(day) => day, + let calculated = match maybe_calculated { + Some(data) => data, None => { - println!("{}", "Your expenses are empty...".italic()); + println!(); + println!("{}", "You have no expenses...".italic()); ::std::process::exit(0); } }; - let days_until_end = account.end_date - last_day.date; + let days_until_end = account.end_date - calculated.last_day; println!( "{}", format!( "Last day on entry: {}", - last_day.date.format("%Y-%m-%d"), + calculated.last_day.format("%Y-%m-%d"), ).cyan(), ); -- cgit v1.2.3