Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 10 additions & 18 deletions app/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,20 @@ pub fn parse_amount_nature_cent(amount: &str) -> Result<Option<AmountNature>> {
}

pub fn compare_change(current: Dollar, previous: Dollar) -> String {
if previous == 0.0 {
"∞".to_string()
} else {
let diff = ((current - previous) / previous) * 100.0;
if diff < 0.0 {
format!("↓{:.2}", diff.abs())
} else {
format!("↑{diff:.2}")
}
match current.cent().percent_change(previous.cent()) {
None => "∞".to_string(),
Some(diff) if diff < 0.0 => format!("↓{:.2}", diff.abs()),
Some(diff) => format!("↑{diff:.2}"),
}
}

pub fn compare_change_opt(current: Dollar, previous: Option<Dollar>) -> String {
match previous {
Some(prev) if prev != 0.0 => {
let diff = ((current - prev) / prev) * 100.0;
if diff < 0.0 {
format!("↓{:.2}", diff.abs())
} else {
format!("↑{diff:.2}")
}
}
_ => "∞".to_string(),
None => "∞".to_string(),
Some(prev) => match current.cent().percent_change(prev.cent()) {
None => "∞".to_string(),
Some(diff) if diff < 0.0 => format!("↓{:.2}", diff.abs()),
Some(diff) => format!("↑{diff:.2}"),
},
}
}
14 changes: 7 additions & 7 deletions app/src/views/summary_models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl PeakMonthlyMovement {
}
}

#[derive(PartialEq, Debug)]
#[derive(Debug)]
pub(crate) struct SummaryNet {
pub(crate) total_income: Dollar,
pub(crate) total_expense: Dollar,
Expand Down Expand Up @@ -96,13 +96,13 @@ impl SummaryNet {
}
}

#[derive(PartialEq, Debug)]
#[derive(Debug)]
pub(crate) enum LargestType {
Earning,
Expense,
}

#[derive(PartialEq, Debug)]
#[derive(Debug)]
pub(crate) enum PeakType {
Earning,
Expense,
Expand All @@ -126,7 +126,7 @@ impl fmt::Display for PeakType {
}
}

#[derive(PartialEq, Debug)]
#[derive(Debug)]
pub(crate) struct SummaryLargest {
largest_type: LargestType,
method: String,
Expand Down Expand Up @@ -171,7 +171,7 @@ impl SummaryLargest {
}
}

#[derive(PartialEq, Debug)]
#[derive(Debug)]
pub(crate) struct SummaryPeak {
peak_type: PeakType,
amount: Dollar,
Expand Down Expand Up @@ -207,7 +207,7 @@ impl SummaryPeak {
}
}

#[derive(Debug, PartialEq)]
#[derive(Debug)]
pub(crate) struct SummaryMethods {
method: String,
pub(crate) total_earning: Dollar,
Expand Down Expand Up @@ -281,7 +281,7 @@ impl SummaryMethods {
}
}

#[derive(Debug, PartialEq)]
#[derive(Debug)]
pub(crate) struct SummaryLendBorrows {
pub(crate) borrows: Dollar,
pub(crate) lends: Dollar,
Expand Down
99 changes: 14 additions & 85 deletions shared/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ use std::ops::{Add, AddAssign, Div, Mul, Sub, SubAssign};
pub const LAST_POSSIBLE_TIME: NaiveTime =
NaiveTime::from_hms_nano_opt(23, 59, 59, 999_999_999).unwrap();

impl Cent {
#[must_use]
pub fn percent_change(self, previous: Cent) -> Option<f64> {
if previous.0 == 0 {
None
} else {
let cur = self.0 as f64;
let prev = previous.0 as f64;
Some(((cur - prev) / prev) * 100.0)
}
}
}

impl Add<i64> for Cent {
type Output = Cent;

Expand All @@ -14,14 +27,6 @@ impl Add<i64> for Cent {
}
}

impl Add<f64> for Dollar {
type Output = Dollar;

fn add(self, rhs: f64) -> Self::Output {
Dollar(self.0 + rhs)
}
}

impl Sub<i64> for Cent {
type Output = Cent;

Expand All @@ -30,30 +35,6 @@ impl Sub<i64> for Cent {
}
}

impl Sub<f64> for Dollar {
type Output = Dollar;

fn sub(self, rhs: f64) -> Self::Output {
Dollar(self.0 - rhs)
}
}

impl Sub for Dollar {
type Output = Dollar;

fn sub(self, rhs: Dollar) -> Self::Output {
Dollar(self.0 - rhs.0)
}
}

impl Div for Dollar {
type Output = f64;

fn div(self, rhs: Dollar) -> Self::Output {
self.0 / rhs.0
}
}

impl Mul<i64> for Cent {
type Output = Cent;

Expand All @@ -62,20 +43,6 @@ impl Mul<i64> for Cent {
}
}

impl Mul<f64> for Dollar {
type Output = Dollar;

fn mul(self, rhs: f64) -> Self::Output {
Dollar(self.0 * rhs)
}
}

impl AddAssign<f64> for Dollar {
fn add_assign(&mut self, rhs: f64) {
self.0 += rhs;
}
}

impl AddAssign<i64> for Cent {
fn add_assign(&mut self, rhs: i64) {
self.0 += rhs;
Expand All @@ -100,20 +67,6 @@ impl AddAssign for Cent {
}
}

impl AddAssign<Dollar> for f64 {
fn add_assign(&mut self, other: Dollar) {
*self += other.0;
}
}

impl Div<i64> for Cent {
type Output = Cent;

fn div(self, rhs: i64) -> Self::Output {
Cent(self.0 / rhs)
}
}

impl Div<f64> for Dollar {
type Output = Dollar;

Expand Down Expand Up @@ -146,48 +99,24 @@ impl PartialEq<i64> for Cent {
}
}

impl PartialEq<f64> for Dollar {
fn eq(&self, other: &f64) -> bool {
self.0 == *other
}
}

impl PartialEq for Cent {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}

impl PartialEq for Dollar {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}

impl PartialOrd for Cent {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.0.partial_cmp(&other.0)
}
}

impl PartialOrd for Dollar {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.0.partial_cmp(&other.0)
}
}

impl PartialOrd<i64> for Cent {
fn partial_cmp(&self, other: &i64) -> Option<Ordering> {
self.0.partial_cmp(other)
}
}

impl PartialOrd<f64> for Dollar {
fn partial_cmp(&self, other: &f64) -> Option<Ordering> {
self.0.partial_cmp(other)
}
}

impl fmt::Display for Dollar {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:.2}", self.0)
Expand Down Expand Up @@ -230,6 +159,6 @@ impl Dollar {

#[must_use]
pub fn cent(&self) -> Cent {
Cent::new((self.0 * 100.0) as i64)
Cent::new((self.0 * 100.0).round() as i64)
}
}
2 changes: 1 addition & 1 deletion tui/src/pages/chart_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ pub fn chart_ui(
let method_at_index = tx_methods[method_index];
let balance = current_balances.get(&method_at_index.id).unwrap().dollar();

cumulative_balance += balance;
cumulative_balance += balance.value();

balance.value()
};
Expand Down
Loading