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
77
78
79
use serde::{Deserialize, Serialize};
use super::{DataError, DataItem};
use crate::cash_flow::CashFlow;
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
pub enum TransactionType {
Cash,
Asset { asset_id: usize, position: f64 },
Dividend { asset_id: usize },
Interest { asset_id: usize },
Tax { transaction_ref: Option<usize> },
Fee { transaction_ref: Option<usize> },
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Transaction {
pub id: Option<usize>,
pub transaction_type: TransactionType,
pub cash_flow: CashFlow,
pub note: Option<String>,
}
impl Transaction {
pub fn set_asset_id(&mut self, asset_id: usize) {
self.transaction_type = match self.transaction_type {
TransactionType::Asset {
asset_id: _,
position,
} => TransactionType::Asset { asset_id, position },
TransactionType::Dividend { asset_id: _ } => TransactionType::Dividend { asset_id },
TransactionType::Interest { asset_id: _ } => TransactionType::Interest { asset_id },
_ => self.transaction_type,
}
}
pub fn set_transaction_ref(&mut self, trans_ref: usize) {
self.transaction_type = match self.transaction_type {
TransactionType::Tax { transaction_ref: _ } => TransactionType::Tax {
transaction_ref: Some(trans_ref),
},
TransactionType::Fee { transaction_ref: _ } => TransactionType::Fee {
transaction_ref: Some(trans_ref),
},
_ => self.transaction_type,
}
}
}
impl DataItem for Transaction {
fn get_id(&self) -> Result<usize, DataError> {
match self.id {
Some(id) => Ok(id),
None => Err(DataError::DataAccessFailure(
"tried to get id of temporary transaction".to_string(),
)),
}
}
fn set_id(&mut self, id: usize) -> Result<(), DataError> {
match self.id {
Some(_) => Err(DataError::DataAccessFailure(
"tried to change valid transaction id".to_string(),
)),
None => {
self.id = Some(id);
Ok(())
}
}
}
}