use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use crate::{BaseMoney, Currency};
use super::RawMoney;
impl<C> Add for RawMoney<C>
where
C: Currency,
{
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
let ret = self
.amount()
.checked_add(rhs.amount())
.expect("addition operation overflow");
Self::from_decimal(ret)
}
}
impl<C> Sub for RawMoney<C>
where
C: Currency,
{
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
let ret = self
.amount()
.checked_sub(rhs.amount())
.expect("subtraction operation overflow");
Self::from_decimal(ret)
}
}
impl<C> Mul for RawMoney<C>
where
C: Currency,
{
type Output = Self;
fn mul(self, rhs: Self) -> Self::Output {
let ret = self
.amount()
.checked_mul(rhs.amount())
.expect("multiplication operation overflow");
Self::from_decimal(ret)
}
}
impl<C> Div for RawMoney<C>
where
C: Currency,
{
type Output = Self;
fn div(self, rhs: Self) -> Self::Output {
let ret = self
.amount()
.checked_div(rhs.amount())
.expect("division operation overflow");
Self::from_decimal(ret)
}
}
impl<C> AddAssign for RawMoney<C>
where
C: Currency,
{
fn add_assign(&mut self, other: Self) {
let ret = self
.amount()
.checked_add(other.amount())
.expect("addition operation overflow");
*self = Self::from_decimal(ret);
}
}
impl<C> SubAssign for RawMoney<C>
where
C: Currency,
{
fn sub_assign(&mut self, other: Self) {
let ret = self
.amount()
.checked_sub(other.amount())
.expect("subtraction operation overflow");
*self = Self::from_decimal(ret);
}
}
impl<C> MulAssign for RawMoney<C>
where
C: Currency,
{
fn mul_assign(&mut self, other: Self) {
let ret = self
.amount()
.checked_mul(other.amount())
.expect("multiplication operation overflow");
*self = Self::from_decimal(ret);
}
}
impl<C> DivAssign for RawMoney<C>
where
C: Currency,
{
fn div_assign(&mut self, other: Self) {
let ret = self
.amount()
.checked_div(other.amount())
.expect("division operation overflow");
*self = Self::from_decimal(ret);
}
}
impl<C> Neg for RawMoney<C>
where
C: Currency,
{
type Output = Self;
fn neg(self) -> Self::Output {
Self::from_decimal(-self.amount())
}
}