use crate::{Currency, CurrencyLocale};
use std::ops::{Add, AddAssign, Sub};
impl<L, T> Add<T> for Currency<L>
where
T: Into<Currency<L>>,
L: CurrencyLocale + Default,
{
type Output = Self;
fn add(self, rhs: T) -> Self::Output {
let rhs = rhs.into();
if rhs.negative {
self.sub(Self::new(false, rhs.full, rhs.part, rhs.locale))
} else if self.negative {
rhs.sub(Self::new(false, self.full, self.part, self.locale))
} else {
let new_part = self.part + rhs.part;
let new_full = self.full + rhs.full + usize::from(new_part >= 100);
Self::new(false, new_full, new_part % 100, self.locale)
}
}
}
impl<L, T> AddAssign<T> for Currency<L>
where
T: Into<Currency<L>>,
L: CurrencyLocale + Default + Clone,
{
fn add_assign(&mut self, rhs: T) {
let rhs = rhs.into();
if rhs.negative {
let new = self
.clone()
.sub(Self::new(false, rhs.full, rhs.part, rhs.locale));
self.negative = new.negative;
self.full = new.full;
self.part = new.part;
} else if self.negative {
let new = rhs.sub(Self::new(false, self.full, self.part, L::default()));
self.negative = new.negative;
self.full = new.full;
self.part = new.part;
} else {
let new_part = self.part + rhs.part;
self.full += rhs.full + usize::from(new_part >= 100);
self.part %= 100;
}
}
}