Skip to content

Commit

Permalink
General patches (#13)
Browse files Browse the repository at this point in the history
* fix: Miscalculation on yearly costs

* fix: Typo on recurrences

* fix: bug #10; temporary fix for bug #11

* Fix: bug #6, solution is untested

* Run cargo fmt

* Fix: issue #7

* Bump version
  • Loading branch information
margual56 authored Sep 27, 2023
1 parent a293d5e commit 8aeaabc
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 35 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "nix-bucks",
"private": true,
"version": "0.1.2",
"version": "0.1.3",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "nix-bucks"
version = "0.1.2"
version = "0.1.3"
description = "A simple budgeting app"
authors = ["Marcos Gutiérrez Alonso <[email protected]>"]
license = "GPL-3.0"
Expand Down
22 changes: 19 additions & 3 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ const ORGANIZATION: &str = "margual56";
const APPLICATION: &str = "NixBucks";

pub fn format_money(amount: f32) -> String {
return format!("{:+.2} €", amount);
if amount == 0.0 {
// This is to avoid the whole "- 0.0" debacle, because of floating
// point precision.
"0.00 €".to_string()
} else {
format!("{:+.2} €", amount)
}
}

#[derive(Serialize, Deserialize, Clone)]
Expand Down Expand Up @@ -56,7 +62,17 @@ impl Default for App {

path.read_to_string(&mut buffer).unwrap();

serde_json::from_str::<Self>(&buffer).unwrap().update()
serde_json::from_str::<Self>(&buffer)
.unwrap_or(Self {
initial_savings: 0.0,
subscriptions: HashMap::new(),
fixed_expenses: HashMap::new(),
incomes: HashMap::new(),
p_incomes: HashMap::new(),
dismissed_ad: false,
lang: String::from("en"),
})
.update()
} else {
println!("Directory not found, returning default value");
Self {
Expand Down Expand Up @@ -238,7 +254,7 @@ impl App {

for subscription in self.subscriptions.clone().into_values() {
cost += subscription.cost()
/ subscription
* subscription
.recurrence()
.times_in_a_year()
.unwrap_or(1.0 / 12.0);
Expand Down
82 changes: 55 additions & 27 deletions src-tauri/src/utils/recurrence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use serde::{Deserialize, Serialize};
/// - `m`: The month.
/// # Returns
/// - The amount of days in the month.
#[allow(dead_code)]
#[cached]
pub fn days_in_month(m: u8) -> u8 {
match m {
Expand Down Expand Up @@ -40,19 +39,37 @@ pub enum SimpleRecurrence {
Year,
}

impl SimpleRecurrence {
// Returns the string representation according to the language given.
// # Arguments
// - `lang`: The language.
// # Returns
// - The string representation according to the language given.
// pub fn to_lang_str(&self, lang: &str) -> String {
// match self {
// Self::Day => t!("recurrence.simple.day", lang),
// Self::Month => t!("recurrence.simple.month", lang),
// Self::Year => t!("recurrence.simple.year", lang),
// }
// }
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Day {
Normal(u8),
Last,
}

impl Day {
pub fn from_day(day: u8, month: u8) -> Self {
let max_days = days_in_month(month);

if day >= max_days {
Self::Last
} else {
Self::Normal(day)
}
}

pub fn get_day(&self, month: u8) -> u8 {
let max_days = days_in_month(month);

match self {
Self::Normal(d) => {
if *d > max_days {
max_days
} else {
*d
}
}
Self::Last => max_days,
}
}
}

/// A more complex recurrence enum. It stores the recurrence in a more complex way.
Expand All @@ -61,9 +78,9 @@ pub enum Recurrence {
/// Amount of days
Day(u8),
/// Day of the month, amount of months
Month(u8, u8),
Month(Day, u8),
/// Day of the month, month of the year, amount of years
Year(u8, u8, u8),
Year(Day, u8, u8),
}

impl Recurrence {
Expand All @@ -82,8 +99,8 @@ impl Recurrence {
) -> Self {
match value {
SimpleRecurrence::Day => Self::Day(days),
SimpleRecurrence::Month => Self::Month(days, months),
SimpleRecurrence::Year => Self::Year(days, months, years),
SimpleRecurrence::Month => Self::Month(Day::from_day(days, months), months),
SimpleRecurrence::Year => Self::Year(Day::from_day(days, months), months, years),
}
}

Expand All @@ -99,11 +116,19 @@ impl Recurrence {
impl Display for Recurrence {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Day(days) => write!(f, "Each {} days", days),
Self::Month(day, months) => write!(f, "Each {} months on day {}", months, day),
Self::Year(day, month, years) => {
write!(f, "Each {} years on day {} of month {}", years, day, month)
}
Self::Day(days) => write!(f, "Every {} day(s)", days),
Self::Month(day_r, months) => match day_r {
Day::Normal(day) => write!(f, "Every {} month(s) on day {}", months, day),
Day::Last => write!(f, "The last day of the month every {} month(s)", months),
},
Self::Year(day_r, month, years) => match day_r {
Day::Normal(day) => write!(
f,
"Every {} year(s) on day {} of month {}",
years, day, month
),
Day::Last => write!(f, "The last day of month {} every {} year(s)", month, years),
},
}
}
}
Expand Down Expand Up @@ -141,7 +166,10 @@ pub fn times_until(recurrence: Recurrence, from: NaiveDate, to: NaiveDate) -> u3
}
Recurrence::Month(day, each_months) => {
// Count the amount of times the day "day" has passed since today to the target date
let mut start = from.clone().with_day(day as u32).unwrap();
let mut start = from
.clone()
.with_day(day.get_day(from.month0() as u8 + 1) as u32)
.unwrap();

let mut times: u32 = 0;

Expand All @@ -155,7 +183,7 @@ pub fn times_until(recurrence: Recurrence, from: NaiveDate, to: NaiveDate) -> u3

let target = to
.clone()
.with_day(day as u32)
.with_day(day.get_day(to.month0() as u8 + 1) as u32)
.unwrap()
.checked_add_days(Days::new(1))
.unwrap();
Expand All @@ -174,7 +202,7 @@ pub fn times_until(recurrence: Recurrence, from: NaiveDate, to: NaiveDate) -> u3
// Count the amount of times the day "day" has passed since today to the target date
let mut start = from
.clone()
.with_day(day as u32)
.with_day(day.get_day(from.month0() as u8 + 1) as u32)
.unwrap()
.with_month(month as u32)
.unwrap();
Expand All @@ -191,7 +219,7 @@ pub fn times_until(recurrence: Recurrence, from: NaiveDate, to: NaiveDate) -> u3

let target = to
.clone()
.with_day(day as u32)
.with_day(day.get_day(to.month0() as u8 + 1) as u32)
.unwrap()
.checked_add_days(Days::new(1))
.unwrap()
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"package": {
"productName": "nix-bucks",
"version": "0.1.2"
"version": "0.1.3"
},
"tauri": {
"allowlist": {
Expand Down
2 changes: 1 addition & 1 deletion src/styles/app_styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@

padding-bottom: 3em;

overflow-y: scroll;
overflow-y: auto;
}

.footer .horizontal-grid {
Expand Down

0 comments on commit 8aeaabc

Please sign in to comment.