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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
use uuid::Uuid;
use priority::TaskPriority;
use status::TaskStatus;
use project::Project;
use tag::Tag;
use date::Date;
use annotation::Annotation;
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Task {
status : TaskStatus,
uuid : Uuid,
entry : Date,
description : String,
#[serde(skip_serializing_if = "Option::is_none")]
annotations : Option<Vec<Annotation>>,
#[serde(skip_serializing_if = "Option::is_none")]
depends : Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
due : Option<Date>,
#[serde(skip_serializing_if = "Option::is_none")]
end : Option<Date>,
#[serde(skip_serializing_if = "Option::is_none")]
imask : Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
mask : Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
modified : Option<Date>,
#[serde(skip_serializing_if = "Option::is_none")]
parent : Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none")]
priority : Option<TaskPriority>,
#[serde(skip_serializing_if = "Option::is_none")]
project : Option<Project>,
#[serde(skip_serializing_if = "Option::is_none")]
recur : Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
scheduled : Option<Date>,
#[serde(skip_serializing_if = "Option::is_none")]
start : Option<Date>,
#[serde(skip_serializing_if = "Option::is_none")]
tags : Option<Vec<Tag>>,
#[serde(skip_serializing_if = "Option::is_none")]
until : Option<Date>,
#[serde(skip_serializing_if = "Option::is_none")]
wait : Option<Date>,
}
impl Task {
pub fn new(
status : TaskStatus,
uuid : Uuid,
entry : Date,
description : String,
annotations : Option<Vec<Annotation>>,
depends : Option<String>,
due : Option<Date>,
end : Option<Date>,
imask : Option<i64>,
mask : Option<String>,
modified : Option<Date>,
parent : Option<Uuid>,
priority : Option<TaskPriority>,
project : Option<Project>,
recur : Option<String>,
scheduled : Option<Date>,
start : Option<Date>,
tags : Option<Vec<Tag>>,
until : Option<Date>,
wait : Option<Date>,
) -> Task
{
Task {
status : status,
uuid : uuid,
entry : entry,
description : description,
annotations : annotations,
depends : depends,
due : due,
end : end,
imask : imask,
mask : mask,
modified : modified,
parent : parent,
priority : priority,
project : project,
recur : recur,
scheduled : scheduled,
start : start,
tags : tags,
until : until,
wait : wait,
}
}
pub fn status(&self) -> &TaskStatus {
&self.status
}
pub fn status_mut(&mut self) -> &mut TaskStatus {
&mut self.status
}
pub fn uuid(&self) -> &Uuid {
&self.uuid
}
pub fn uuid_mut(&mut self) -> &mut Uuid {
&mut self.uuid
}
pub fn entry(&self) -> &Date {
&self.entry
}
pub fn entry_mut(&mut self) -> &mut Date {
&mut self.entry
}
pub fn description(&self) -> &String {
&self.description
}
pub fn description_mut(&mut self) -> &mut String {
&mut self.description
}
pub fn annotations(&self) -> Option<&Vec<Annotation>> {
self.annotations.as_ref()
}
pub fn annotations_mut(&mut self) -> Option<&mut Vec<Annotation>> {
self.annotations.as_mut()
}
pub fn depends(&self) -> Option<&String> {
self.depends.as_ref()
}
pub fn depends_mut(&mut self) -> Option<&mut String> {
self.depends.as_mut()
}
pub fn due(&self) -> Option<&Date> {
self.due.as_ref()
}
pub fn due_mut(&mut self) -> Option<&mut Date> {
self.due.as_mut()
}
pub fn end(&self) -> Option<&Date> {
self.end.as_ref()
}
pub fn end_mut(&mut self) -> Option<&mut Date> {
self.end.as_mut()
}
pub fn imask(&self) -> Option<&i64> {
self.imask.as_ref()
}
pub fn imask_mut(&mut self) -> Option<&mut i64> {
self.imask.as_mut()
}
pub fn mask(&self) -> Option<&String> {
self.mask.as_ref()
}
pub fn mask_mut(&mut self) -> Option<&mut String> {
self.mask.as_mut()
}
pub fn modified(&self) -> Option<&Date> {
self.modified.as_ref()
}
pub fn modified_mut(&mut self) -> Option<&mut Date> {
self.modified.as_mut()
}
pub fn parent(&self) -> Option<&Uuid> {
self.parent.as_ref()
}
pub fn parent_mut(&mut self) -> Option<&mut Uuid> {
self.parent.as_mut()
}
pub fn priority(&self) -> Option<&TaskPriority> {
self.priority.as_ref()
}
pub fn priority_mut(&mut self) -> Option<&mut TaskPriority> {
self.priority.as_mut()
}
pub fn project(&self) -> Option<&Project> {
self.project.as_ref()
}
pub fn project_mut(&mut self) -> Option<&mut Project> {
self.project.as_mut()
}
pub fn recur(&self) -> Option<&String> {
self.recur.as_ref()
}
pub fn recur_mut(&mut self) -> Option<&mut String> {
self.recur.as_mut()
}
pub fn scheduled(&self) -> Option<&Date> {
self.scheduled.as_ref()
}
pub fn scheduled_mut(&mut self) -> Option<&mut Date> {
self.scheduled.as_mut()
}
pub fn start(&self) -> Option<&Date> {
self.start.as_ref()
}
pub fn start_mut(&mut self) -> Option<&mut Date> {
self.start.as_mut()
}
pub fn tags(&self) -> Option<&Vec<Tag>> {
self.tags.as_ref()
}
pub fn tags_mut(&mut self) -> Option<&mut Vec<Tag>> {
self.tags.as_mut()
}
pub fn until(&self) -> Option<&Date> {
self.until.as_ref()
}
pub fn until_mut(&mut self) -> Option<&mut Date> {
self.until.as_mut()
}
pub fn wait(&self) -> Option<&Date> {
self.wait.as_ref()
}
pub fn wait_mut(&mut self) -> Option<&mut Date> {
self.wait.as_mut()
}
}
#[cfg(test)]
mod test {
use date::Date;
use date::TASKWARRIOR_DATETIME_TEMPLATE;
use status::TaskStatus;
use task::Task;
use annotation::Annotation;
use uuid::Uuid;
use chrono::NaiveDateTime;
use serde_json;
fn mkdate(s: &str) -> Date {
let n = NaiveDateTime::parse_from_str(s, TASKWARRIOR_DATETIME_TEMPLATE);
Date::from(n.unwrap())
}
#[test]
fn test_deser() {
let s =
r#"{
"id": 1,
"description": "test",
"entry": "20150619T165438Z",
"status": "waiting",
"uuid": "8ca953d5-18b4-4eb9-bd56-18f2e5b752f0"
}"#;
println!("{}", s);
let task = serde_json::from_str(s);
println!("{:?}", task);
assert!(task.is_ok());
let task : Task = task.unwrap();
assert!(task.status().clone() == TaskStatus::Waiting);
assert!(task.description() == "test");
assert!(task.entry().clone() == mkdate("20150619T165438Z"));
assert!(task.uuid().clone() == Uuid::parse_str("8ca953d5-18b4-4eb9-bd56-18f2e5b752f0").unwrap());
let back = serde_json::to_string(&task).unwrap();
assert!(back.contains("description"));
assert!(back.contains("test"));
assert!(back.contains("entry"));
assert!(back.contains("20150619T165438Z"));
assert!(back.contains("status"));
assert!(back.contains("waiting"));
assert!(back.contains("uuid"));
assert!(back.contains("8ca953d5-18b4-4eb9-bd56-18f2e5b752f0"));
}
#[test]
fn test_deser_more() {
let s =
r#"{
"id": 1,
"description": "some description",
"entry": "20150619T165438Z",
"modified": "20160327T164007Z",
"project": "someproject",
"status": "waiting",
"tags": ["some", "tags", "are", "here"],
"uuid": "8ca953d5-18b4-4eb9-bd56-18f2e5b752f0",
"wait": "20160508T164007Z",
"urgency": 0.583562
}"#;
println!("{}", s);
let task = serde_json::from_str(s);
println!("{:?}", task);
assert!(task.is_ok());
let task : Task = task.unwrap();
assert!(task.status().clone() == TaskStatus::Waiting);
assert!(task.description() == "some description");
assert!(task.entry().clone() == mkdate("20150619T165438Z"));
assert!(task.uuid().clone() == Uuid::parse_str("8ca953d5-18b4-4eb9-bd56-18f2e5b752f0").unwrap());
assert!(task.modified() == Some(&mkdate("20160327T164007Z")));
assert!(task.project() == Some(&String::from("someproject")));
if let Some(tags) = task.tags() {
for tag in tags {
let any_tag = [ "some", "tags", "are", "here", ]
.into_iter().any(|t| tag == *t);
assert!(any_tag, "Tag {} missing", tag);
}
} else {
assert!(false, "Tags completely missing");
}
assert!(task.wait() == Some(&mkdate("20160508T164007Z")));
let back = serde_json::to_string(&task).unwrap();
assert!(back.contains("description"));
assert!(back.contains("some description"));
assert!(back.contains("entry"));
assert!(back.contains("20150619T165438Z"));
assert!(back.contains("project"));
assert!(back.contains("someproject"));
assert!(back.contains("status"));
assert!(back.contains("waiting"));
assert!(back.contains("tags"));
assert!(back.contains("some"));
assert!(back.contains("tags"));
assert!(back.contains("are"));
assert!(back.contains("here"));
assert!(back.contains("uuid"));
assert!(back.contains("8ca953d5-18b4-4eb9-bd56-18f2e5b752f0"));
}
#[test]
fn test_deser_annotation() {
let s =
r#"{
"id":192,
"description":"Some long description for a task",
"entry":"20160423T125820Z",
"modified":"20160423T125942Z",
"project":"project",
"status":"pending",
"tags":["search","things"],
"uuid":"5a04bb1e-3f4b-49fb-b9ba-44407ca223b5",
"annotations":[{"entry":"20160423T125911Z","description":"An Annotation"},
{"entry":"20160423T125926Z","description":"Another Annotation"},
{"entry":"20160422T125942Z","description":"A Third Anno"}
],
"urgency":10.911
}"#;
println!("{}", s);
let task = serde_json::from_str(s);
println!("{:?}", task);
assert!(task.is_ok());
let task : Task = task.unwrap();
let all_annotations = vec![
Annotation::new(mkdate("20160423T125911Z"), String::from("An Annotation")),
Annotation::new(mkdate("20160423T125926Z"), String::from("Another Annotation")),
Annotation::new(mkdate("20160422T125942Z"), String::from("A Third Anno"))
];
if let Some(annotations) = task.annotations() {
for annotation in annotations {
let r = all_annotations.iter().any(|ann| {
let descr = ann.description() == annotation.description();
let entry = ann.entry() == annotation.entry();
descr && entry
});
assert!(r, "Annotation {:?} missing or buggy", annotation);
}
} else {
assert!(false, "Annotations missing");
}
}
}