varunsrin / rusty_money

Money library for Rust
MIT License
93 stars 34 forks source link

feat: impl neg for Money #84

Closed oti-dev closed 1 year ago

oti-dev commented 1 year ago

Implement Neg trait for Money to enable unary - operator -- it's fairly simple, and might come handy in some scenarios, e.g. tests.

For instance, to make my code more clear I used a helper method in my tests:

fn usd(major: i64) -> Money<'static, Currency> {
    use rusty_money::iso::USD;

    Money::from_major(major, USD)
}

Right now these assertions work fine:

assert_eq!(total, -1 * usd(10))
assert_eq!(total, usd(-10))

but I don't like neither. This feels more natural to me:

assert_eq!(total, -usd(10))