cash/traits/
create.rs

1use crate::{
2	Cash,
3	HashMap,
4	sanitize_negative,
5};
6
7#[allow(dead_code)]
8
9// TODO rename create into init
10
11// TODO add log history on init
12
13// TODO create new trait READ to allow executing read ops on history log
14
15pub async fn new (currency: &str, amount: f64, allow_negatives: bool) -> Cash {
16	Cash {
17		currency: String::from(currency),
18		amount,
19		allow_negatives,
20		log: HashMap::from([]),
21		newlog: Vec::new(),
22	}
23}
24
25impl Cash {
26	pub fn new (currency: &str, amount: f64, allow_negatives: bool) -> Self {
27		Self {
28			currency: String::from(currency),
29			amount,
30			allow_negatives,
31			log: HashMap::from([]),
32			newlog: Vec::new(),
33		}
34	}
35
36	pub fn create (currency: &str, amount: f64, allow_negatives: bool) -> Self {
37		let new_amount: f64 = if allow_negatives { amount } else { sanitize_negative(amount) };
38
39		// TODO add date
40		// TODO extract match currency into a helper function
41		// TODO add array of currencies as a vector to search within
42
43		match currency {
44			"USD" | "EUR" | "GBP" | "CAD" | "AUD" | "RON" | "JPY" => Self {
45				currency: String::from(currency),
46				amount: new_amount,
47				allow_negatives,
48				log: HashMap::from([]),
49				newlog: vec![],
50			},
51			// fallback to USD in case an unknown/unsupported currency is supplied
52			_ => Self {
53				currency: String::from("USD"),
54				amount: new_amount,
55				allow_negatives,
56				log: HashMap::from([]),
57				newlog: vec![],
58			},
59		}
60	}
61}
62
63pub trait Create {
64	fn currency (&self) -> String;
65	fn amount (&self) -> f64;
66}
67
68impl Create for Cash {
69	fn currency (&self) -> String {
70		String::from(&self.currency)
71	}
72
73	fn amount (&self) -> f64 {
74		self.amount
75	}
76}