Skip to content

Commit

Permalink
feat: Amount now implements Deref and DerefMul to Decimal
Browse files Browse the repository at this point in the history
  • Loading branch information
meuter committed Mar 10, 2024
1 parent af4b269 commit bad6ad1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 21 deletions.
4 changes: 2 additions & 2 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "oxydized-money"
authors = ["Cédric Meuter <cedric.meuter@gmail.com>"]
version = "0.2.0"
version = "0.3.0"
edition = "2021"
description = "Library providing data types to manipulate amounts of money in specific currencies and convert amounts between then"
readme = "README.md"
Expand Down
2 changes: 1 addition & 1 deletion macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "oxydized-money-macros"
authors = ["Cédric Meuter <cedric.meuter@gmail.com>"]
version = "0.2.0"
version = "0.3.0"
edition = "2021"
description = "Companion library to oxydized-gains providing convenience macros"
readme = "../README.md"
Expand Down
42 changes: 25 additions & 17 deletions src/amount.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use crate::{AmountResult, Currency, CurrencyError, Decimal, Result};
use std::{cmp::Ordering, fmt::Display};
use std::{
cmp::Ordering,
fmt::Display,
ops::{Deref, DerefMut},
};

/// `Amount` represents an amount of money in a specific currency.
/// The quantity part is stored as a 128-bit fixed precision [`Decimal`].
Expand Down Expand Up @@ -52,22 +56,6 @@ impl Amount {
Amount(self.value().abs(), self.currency())
}

/// Returns `true` if and only if the quantity is zero.
///
/// # Examples
///
/// ```
/// use oxydized_money_macros::{eur};
/// use oxydized_money::Decimal;
///
/// assert!( ! eur!(10).is_zero());
/// assert!( ! eur!(-10).is_zero());
/// assert!( eur!(0).is_zero());
/// ```
pub fn is_zero(&self) -> bool {
self.value().is_zero()
}

/// Returns `self` converted in another currency using the provided
/// exchange rate.
///
Expand Down Expand Up @@ -125,6 +113,20 @@ impl PartialOrd<Amount> for Amount {
}
}

impl Deref for Amount {
type Target = Decimal;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl DerefMut for Amount {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

#[cfg(test)]
mod test {
use crate as oxydized_money;
Expand Down Expand Up @@ -167,4 +169,10 @@ mod test {
assert_matches!(eur!(1).partial_cmp(&usd!(2)), None);
assert_matches!(eur!(3).partial_cmp(&usd!(2)), None);
}

#[test]
fn test_as_ref_decimal() {
assert!(eur!(-1).is_sign_negative());
assert!(eur!(0).is_zero());
}
}

0 comments on commit bad6ad1

Please sign in to comment.