cash 0.3.4

An immutable library to create, calculate, format and exchange currency.
Documentation
use crate::{
	Cash,
	// LogMap,
	sanitize_negative,
};

// TODO add log at each operation (log is a hashmap made of key/value pairs)
// each added log is made of the name of the operation + the amount in the argument

pub trait Calculate {
	fn add (&self, amount: f64) -> Self;
	fn subtract (&self, amount: f64) -> Self;
	fn multiply (&self, amount: f64) -> Self;
	fn divide (&self, amount: f64) -> Self;
}

impl Calculate for Cash {
	fn add (&self, amount: f64) -> Self {
		let new_amount: f64 = &self.amount + amount;

		// let piece_of_history: LogMap = LogMap {
		// 	operation: String::from("add"),
		// 	amount,
		// 	date: String::from("new date"),
		// };

		// let mut newlog: Vec<LogMap> = self.newlog.clone();
		// newlog.push(piece_of_history);

		Self {
			currency: String::from(&self.currency),
			amount: new_amount,
			allow_negatives: self.allow_negatives,
			log: self.log.clone(),
			newlog: vec![],
		}
	}

	fn subtract (&self, amount: f64) -> Self {
		let new_amount: f64 = &self.amount - amount;

		let sanitized_amount: f64 = if self.allow_negatives { new_amount } else { sanitize_negative(new_amount) };

		Self {
			currency: String::from(&self.currency),
			amount: sanitized_amount,
			allow_negatives: self.allow_negatives,
			log: self.log.clone(),
			newlog: vec![],
		}
	}

	fn multiply (&self, amount: f64) -> Self {
		let new_amount: f64 = &self.amount * amount;

		Self {
			currency: String::from(&self.currency),
			amount: new_amount,
			allow_negatives: self.allow_negatives,
			log: self.log.clone(),
			newlog: vec![],
		}
	}

	fn divide (&self, amount: f64) -> Self {
		let new_amount: f64 = &self.amount / amount;

		let sanitized_amount: f64 = if self.allow_negatives { new_amount } else { sanitize_negative(new_amount) };

		Self {
			currency: String::from(&self.currency),
			amount: sanitized_amount,
			allow_negatives: self.allow_negatives,
			log: self.log.clone(),
			newlog: vec![],
		}
	}
}