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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use serde::{ Serialize, Deserialize, Serializer, Deserializer };
use crate::transaction_type::TransactionType;
use crate::LocalDateTimeStringExt;
use chrono::prelude::*;
use ordered_float::OrderedFloat;
use regex::Regex;
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialOrd, Ord)]
pub struct Transaction {
#[serde(with = "transaction_date_format")]
#[serde(default = "Local::now")]
pub date: DateTime<Local>,
#[serde(skip_serializing_if = "Option::is_none")]
pub check_number: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub category: Option<String>,
pub vendor: String,
#[serde(default = "String::new", skip_serializing_if = "String::is_empty")]
pub memo: String,
#[serde(default = "default_float")]
pub amount: OrderedFloat<f64>,
#[serde(rename = "type")]
pub transaction_type: TransactionType,
#[serde(default = "default_reconciled", skip_serializing_if = "is_default_reconciled")]
pub is_reconciled: bool
}
impl Transaction {
pub fn new() -> Transaction {
Transaction {
date: Local::now(),
check_number: None,
category: None,
vendor: String::new(),
memo: String::new(),
amount: default_float(),
transaction_type: TransactionType::Withdrawal,
is_reconciled: default_reconciled()
}
}
pub fn from(date: Option<&str>, check_number: Option<u32>, category: Option<&str>, vendor: &str, memo: &str, amount: f64, transaction_type: TransactionType, is_reconciled: bool) -> Result<Transaction, String> {
if let Some(date_string) = date {
match date_string.local_datetime() {
Ok(date_time) => Ok(Transaction {
date: date_time,
check_number,
category: if let Some(category) = category {
Some(String::from(category))
} else {
None
},
vendor: String::from(vendor),
memo: String::from(memo),
amount: OrderedFloat(amount),
transaction_type,
is_reconciled
}),
Err(error) => Err(error)
}
} else {
Ok(Transaction {
date: Local::now(),
check_number,
category: if let Some(category) = category {
Some(String::from(category))
} else {
None
},
vendor: String::from(vendor),
memo: String::from(memo),
amount: OrderedFloat(amount),
transaction_type,
is_reconciled
})
}
}
}
impl PartialEq for Transaction {
fn eq(&self, other: &Self) -> bool {
self.date == other.date &&
self.check_number == other.check_number &&
self.category == other.category &&
self.vendor == other.vendor &&
self.memo == other.memo &&
self.amount == other.amount &&
self.transaction_type == other.transaction_type &&
self.is_reconciled == other.is_reconciled
}
}
pub mod transaction_date_format {
use super::*;
pub const FORMAT: &'static str = "%Y-%m-%d";
pub fn serialize<S>(date: &DateTime<Local>, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer, {
let date_string = format!("{}", date.format(&FORMAT));
serializer.serialize_str(&date_string)
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<DateTime<Local>, D::Error> where D: Deserializer<'de>, {
let date_string = String::deserialize(deserializer)?;
let naive_date = NaiveDate::parse_from_str(&date_string, FORMAT).map_err(serde::de::Error::custom)?;
let naive_datetime = naive_date.and_hms(0, 0, 0);
let local_datetime = Local.from_local_datetime(&naive_datetime).unwrap();
Ok(local_datetime)
}
pub fn is_proper_format(s: &str) -> bool {
let re = Regex::new(r"^\d{4}-\d{1,2}-\d{1,2}$").unwrap();
re.is_match(s)
}
}
fn default_float() -> OrderedFloat<f64> {
OrderedFloat::<f64>(0.0)
}
fn default_reconciled() -> bool {
false
}
fn is_default_reconciled(v: &bool) -> bool {
*v == default_reconciled()
}