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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
use std::fmt;
use bill::{Bill, Currency, Tax};
use chrono::{Date, Utc, NaiveTime};
use icalendar::Calendar;
use semver::Version;
use yaml_rust::Yaml;
use storage::Storable;
use super::error::{SpecResult, ErrorList};
use super::product::Product;
use super::product::error::Result as ProductResult;
pub fn print_specresult(label: &str, result: &SpecResult) {
match *result {
Ok(_) => println!("{}: ✓", label),
Err(ref errs) => println!("{}: ✗\n{}", label, errs)
}
}
pub trait Validatable {
fn validate(&self) -> SpecResult;
fn is_valid(&self) -> bool {
self.validate().is_ok()
}
fn errors(&self) -> Option<ErrorList>{
self.validate().err()
}
}
pub trait IsProject {
fn name(&self) -> Option<&str>;
fn event_date(&self) -> Option<Date<Utc>>;
fn format(&self) -> Option<Version>;
fn canceled(&self) -> bool;
fn responsible(&self) -> Option<&str>;
fn long_desc(&self) -> String;
}
pub trait IsProjectExt {
fn age(&self) -> Option<i64>;
}
impl<T> IsProjectExt for T where T: Storable {
fn age(&self) -> Option<i64> {
self.modified_date()
.map(|date|
(Utc::today().signed_duration_since(date))
.num_days()
)
}
}
pub trait Offerable {
fn appendix(&self) -> Option<i64>;
fn date(&self) -> Option<Date<Utc>>;
fn number(&self) -> Option<String>;
}
pub trait IsClient {
fn email(&self) -> Option<&str>;
fn address(&self) -> Option<&str>;
fn title(&self) -> Option<&str>;
fn salute(&self) -> Option<&str>;
fn first_name(&self) -> Option<&str>;
fn last_name(&self) -> Option<&str>;
fn full_name(&self) -> Option<String>;
fn addressing(&self) -> Option<String>;
}
pub trait Invoicable {
fn number(&self) -> Option<i64>;
fn date(&self) -> Option<Date<Utc>>;
fn number_str(&self) -> Option<String>;
fn number_long_str(&self) -> Option<String>;
fn official(&self) -> Option<String>;
}
pub struct Employee {
pub name: String,
pub salary: Currency,
pub time: f64,
pub wage: Currency,
}
pub trait HasEmployees {
fn wages_date(&self) -> Option<Date<Utc>>;
fn salary(&self) -> Option<Currency>;
fn tax(&self) -> Option<Tax>;
fn net_wages(&self) -> Option<Currency> ;
fn gross_wages(&self) -> Option<Currency> ;
fn total_time(&self) -> Option<f64>;
fn to_product(&self) -> Option<Product> {
if let Some(salary) = self.salary() {
Some(Product {
name: "Service",
unit: Some("h"),
tax: self.tax().unwrap_or_else(|| Tax::new(0.0)),
price: salary
})
} else {
None
}
}
fn employees_string(&self) -> Option<String>;
fn employees(&self) -> Option<Vec<Employee>>;
fn employees_payed(&self) -> bool;
fn wages(&self) -> Option<Currency>;
}
pub trait Redeemable: IsProject {
fn payed_date(&self) -> Option<Date<Utc>>;
fn is_payed(&self) -> bool;
fn bills(&self) -> ProductResult<(Bill<Product>, Bill<Product>)>;
fn tax(&self) -> Option<Tax>;
fn sum_sold(&self) -> ProductResult<Currency> {
let (_,invoice) = self.bills()?;
Ok(invoice.net_total())
}
}
#[derive(Debug)]
pub struct EventTime {
pub start: NaiveTime,
pub end: NaiveTime
}
#[derive(Debug)]
pub struct Event {
pub begin: Date<Utc>,
pub end: Option<Date<Utc>>,
pub times: Vec<EventTime>
}
impl fmt::Display for Event {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(end) = self.end { writeln!(f, "start: {}\nend: {}", self.begin, end) }
else { writeln!(f, "start: {}", self.begin) }?;
for time in &self.times {
if time.start == time.end { writeln!(f, " * {}", time.start) }
else { writeln!(f, " * {} - {}", time.start, time.end) } ?
}
Ok(())
}
}
pub trait HasEvents {
fn to_ical(&self) -> Calendar;
fn events(&self) -> Option<Vec<Event>>;
fn times(&self,yaml: &Yaml) -> Option<Vec<EventTime>>;
fn location(&self) -> Option<&str>;
}