1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use crate::{
	Cash,
	HashMap,
	sanitize_negative,
};

#[allow(dead_code)]

// TODO rename create into init

// TODO add log history on init

// TODO create new trait READ to allow executing read ops on history log

pub async fn new (currency: &str, amount: f64, allow_negatives: bool) -> Cash {
	Cash {
		currency: String::from(currency),
		amount,
		allow_negatives,
		log: HashMap::from([]),
		newlog: Vec::new(),
	}
}

impl Cash {
	pub fn new (currency: &str, amount: f64, allow_negatives: bool) -> Self {
		Self {
			currency: String::from(currency),
			amount,
			allow_negatives,
			log: HashMap::from([]),
			newlog: Vec::new(),
		}
	}

	pub fn create (currency: &str, amount: f64, allow_negatives: bool) -> Self {
		let new_amount: f64 = if allow_negatives { amount } else { sanitize_negative(amount) };

		// TODO add date
		// TODO extract match currency into a helper function
		// TODO add array of currencies as a vector to search within

		match currency {
			"USD" | "EUR" | "GBP" | "CAD" | "AUD" | "RON" | "JPY" => Self {
				currency: String::from(currency),
				amount: new_amount,
				allow_negatives,
				log: HashMap::from([]),
				newlog: vec![],
			},
			// fallback to USD in case an unknown/unsupported currency is supplied
			_ => Self {
				currency: String::from("USD"),
				amount: new_amount,
				allow_negatives,
				log: HashMap::from([]),
				newlog: vec![],
			},
		}
	}
}

pub trait Create {
	fn currency (&self) -> String;
	fn amount (&self) -> f64;
}

impl Create for Cash {
	fn currency (&self) -> String {
		String::from(&self.currency)
	}

	fn amount (&self) -> f64 {
		self.amount
	}
}