complish 0.0.1

Core library for project-aware task management with git integration
Documentation
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
mod task_list;
mod task_note;
mod task_priority;
mod task_resolution;
mod task_status;
mod task_work_log;

use chrono::{DateTime, Duration, Utc};
use eyre::{Result, eyre};
use serde::{Deserialize, Serialize};
pub use task_list::TaskList;
pub use task_note::TaskNote;
pub use task_priority::TaskPriority;
pub use task_resolution::TaskResolution;
pub use task_status::TaskStatus;
pub use task_work_log::TaskWorkLog;

#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
pub struct Task {
  pub completed_at: Option<DateTime<Utc>>,
  pub created_at: DateTime<Utc>,
  pub description: Option<String>,
  pub due_at: Option<DateTime<Utc>>,
  pub friendly_id: String,
  pub id: u32,
  pub notes: Vec<TaskNote>,
  pub priority: TaskPriority,
  pub resolution: Option<TaskResolution>,
  pub status: TaskStatus,
  pub subject: String,
  pub tags: Vec<String>,
  pub updated_at: DateTime<Utc>,
  pub work_logs: Vec<TaskWorkLog>,
}

impl Task {
  pub fn new(id: u32, friendly_id: impl Into<String>, subject: impl Into<String>) -> Self {
    let now = Utc::now();

    Self {
      completed_at: None,
      created_at: now,
      description: None,
      due_at: None,
      friendly_id: friendly_id.into(),
      id,
      notes: Vec::new(),
      priority: TaskPriority::default(),
      resolution: None,
      status: TaskStatus::default(),
      subject: subject.into(),
      tags: Vec::new(),
      updated_at: now,
      work_logs: Vec::new(),
    }
  }

  pub fn add_description(&mut self, description: impl Into<String>) {
    self.description = Some(description.into());
    self.touch();
  }

  pub fn add_note(&mut self, note: impl Into<String>) {
    let note = TaskNote::new(note);
    self.notes.push(note);
    self.touch();
  }

  pub fn add_tag(&mut self, tag: impl Into<String>) {
    let tag = tag.into();
    if !self.has_tag(&tag) {
      self.tags.push(tag);
      self.touch();
    }
  }

  pub fn cancel(&mut self) {
    if !self.is_done() {
      self.status = TaskStatus::Done;
      self.resolution = Some(TaskResolution::Cancelled);
      self.touch();
    }
  }

  pub fn cancel_with_note(&mut self, note: impl Into<String>) {
    if !self.is_done() {
      self.cancel();
      self.add_note(note);
    }
  }

  pub fn complete(&mut self) {
    if !self.is_done() {
      self.completed_at = Some(Utc::now());
      self.status = TaskStatus::Done;
      self.resolution = Some(TaskResolution::Completed);
      self.touch();
    }
  }

  pub fn complete_with_note(&mut self, note: impl Into<String>) {
    if !self.is_done() {
      self.complete();
      self.add_note(note);
    }
  }

  pub fn delegate(&mut self) {
    if !self.is_done() {
      self.status = TaskStatus::Done;
      self.resolution = Some(TaskResolution::Delegated);
      self.touch();
    }
  }

  pub fn delegate_with_note(&mut self, note: impl Into<String>) {
    if !self.is_done() {
      self.delegate();
      self.add_note(note);
    }
  }

  pub fn has_tag(&self, tag: &str) -> bool {
    self.tags.contains(&tag.to_string())
  }

  pub fn is_being_worked(&self) -> bool {
    self.work_logs.iter().any(|wl| wl.ended_at.is_none())
  }

  pub fn is_cancelled(&self) -> bool {
    matches!(self.resolution, Some(TaskResolution::Cancelled)) && self.is_done()
  }

  pub fn is_complete(&self) -> bool {
    matches!(self.resolution, Some(TaskResolution::Completed)) && self.is_done()
  }

  pub fn is_delegated(&self) -> bool {
    matches!(self.resolution, Some(TaskResolution::Delegated)) && self.is_done()
  }

  pub fn is_done(&self) -> bool {
    self.status == TaskStatus::Done
  }

  pub fn is_in_progress(&self) -> bool {
    self.status == TaskStatus::InProgress
  }

  pub fn is_overdue(&self) -> bool {
    self.due_at.is_some_and(|due| due < Utc::now())
  }

  pub fn is_todo(&self) -> bool {
    self.status == TaskStatus::Todo
  }

  pub fn remove_tag(&mut self, tag: &str) -> bool {
    let initial_len = self.tags.len();
    self.tags.retain(|t| t != tag);
    if self.tags.len() == initial_len {
      false
    } else {
      self.touch();
      true
    }
  }

  pub fn reopen(&mut self) {
    if self.is_done() {
      self.completed_at = None;
      self.status = TaskStatus::Todo;
      self.resolution = None;
      self.touch();
    }
  }

  pub fn reopen_with_note(&mut self, note: impl Into<String>) {
    if self.is_done() {
      self.reopen();
      self.add_note(note);
    }
  }

  pub fn set_due_date(&mut self, due_date: impl Into<DateTime<Utc>>) -> Result<()> {
    let due_date = due_date.into();
    if due_date < Utc::now() {
      return Err(eyre!("Due date cannot be in the past"));
    }

    self.due_at = Some(due_date);
    self.touch();
    Ok(())
  }

  pub fn set_priority(&mut self, priority: TaskPriority) {
    if self.priority != priority {
      self.priority = priority;
      self.touch();
    }
  }

  pub fn start_work(&mut self) -> Result<()> {
    self.start_work_impl(None, None)
  }

  pub fn start_work_with_note(&mut self, note: impl Into<String>) -> Result<()> {
    self.start_work_impl(Some(&note.into()), None)
  }

  pub fn start_work_with_note_and_source(
    &mut self,
    note: impl Into<String>,
    source: impl Into<String>,
  ) -> Result<()> {
    self.start_work_impl(Some(&note.into()), Some(&source.into()))
  }

  pub fn start_work_with_source(&mut self, source: impl Into<String>) -> Result<()> {
    self.start_work_impl(None, Some(&source.into()))
  }

  pub fn stop_work(&mut self) {
    self
      .work_logs
      .iter_mut()
      .filter(|wl| wl.ended_at.is_none())
      .for_each(TaskWorkLog::stop);
    self.touch();
  }

  pub fn total_time_worked(&self) -> Duration {
    self
      .work_logs
      .iter()
      .filter_map(task_work_log::TaskWorkLog::duration)
      .sum()
  }

  pub fn touch(&mut self) {
    self.updated_at = Utc::now();
  }

  pub fn update_friendly_id(&mut self, friendly_id: impl Into<String>) {
    self.friendly_id = friendly_id.into();
    self.touch();
  }

  fn start_work_impl(&mut self, note: Option<&str>, source: Option<&str>) -> Result<()> {
    if self.status == TaskStatus::Done {
      return Err(eyre!("Task is already done"));
    }

    if self.status != TaskStatus::InProgress {
      self.status = TaskStatus::InProgress;
    }

    self.stop_work();

    let work_log = match (note, source) {
      (Some(note), Some(source)) => TaskWorkLog::new().with_note(note).with_source(source),
      (Some(note), None) => TaskWorkLog::new().with_note(note),
      (None, Some(source)) => TaskWorkLog::new().with_source(source),
      (None, None) => TaskWorkLog::new(),
    };

    self.work_logs.push(work_log);
    self.touch();

    Ok(())
  }
}

#[cfg(test)]
mod tests {
  use super::*;

  fn create_task() -> Task {
    Task::new(1, "1", "a test task")
  }

  mod new {
    use pretty_assertions::assert_eq;

    use super::*;

    #[test]
    fn it_creates_a_new_task() {
      let task = Task::new(1, "1", "a test task");

      assert_eq!(task.completed_at, None);
      assert!(task.created_at.timestamp() > 0);
      assert_eq!(task.description, None);
      assert_eq!(task.due_at, None);
      assert_eq!(task.friendly_id, "1");
      assert_eq!(task.id, 1);
      assert_eq!(task.notes, Vec::new());
      assert_eq!(task.priority, TaskPriority::default());
      assert_eq!(task.resolution, None);
      assert_eq!(task.status, TaskStatus::default());
      assert_eq!(task.subject, "a test task");
      assert_eq!(task.tags, Vec::<String>::new());
      assert!(task.updated_at.timestamp() > 0);
      assert_eq!(task.work_logs, Vec::new());
    }
  }

  mod add_description {
    use pretty_assertions::assert_eq;

    use super::*;

    #[test]
    fn it_adds_a_description() {
      let mut task = create_task();
      task.add_description("a test description");

      assert_eq!(task.description, Some("a test description".to_string()));
    }
  }

  mod add_note {
    use pretty_assertions::assert_eq;

    use super::*;

    #[test]
    fn it_adds_a_note() {
      let mut task = create_task();
      task.add_note("a test note");

      assert_eq!(task.notes.len(), 1);
      assert_eq!(task.notes[0].content, "a test note");
    }
  }

  mod add_tag {
    use pretty_assertions::assert_eq;

    use super::*;

    #[test]
    fn it_adds_a_tag() {
      let mut task = create_task();
      task.add_tag("a test tag");

      assert_eq!(task.tags.len(), 1);
      assert_eq!(task.tags[0], "a test tag");
    }

    #[test]
    fn it_does_not_add_a_duplicate_tag() {
      let mut task = create_task();
      task.add_tag("a test tag");
      task.add_tag("a test tag");

      assert_eq!(task.tags.len(), 1);
    }
  }

  mod cancel {
    use pretty_assertions::{assert_eq, assert_ne};

    use super::*;

    #[test]
    fn it_cancels_the_task() {
      let mut task = create_task();
      task.cancel();

      assert_eq!(task.status, TaskStatus::Done);
      assert_eq!(task.resolution, Some(TaskResolution::Cancelled));
    }

    #[test]
    fn it_does_not_cancel_the_task_if_already_done() {
      let mut task = create_task();
      task.complete();
      task.cancel();

      assert_ne!(task.resolution, Some(TaskResolution::Cancelled));
    }
  }

  mod cancel_with_note {
    use pretty_assertions::assert_eq;

    use super::*;

    #[test]
    fn it_cancels_the_task_with_a_note() {
      let mut task = create_task();
      task.cancel_with_note("a test note");

      assert!(task.is_cancelled());
      assert_eq!(task.notes.len(), 1);
      assert_eq!(task.notes[0].content, "a test note");
    }
  }

  mod complete {
    use pretty_assertions::assert_eq;

    use super::*;

    #[test]
    fn it_completes_the_task() {
      let mut task = create_task();
      task.complete();

      assert!(task.completed_at.is_some());
      assert_eq!(task.status, TaskStatus::Done);
      assert_eq!(task.resolution, Some(TaskResolution::Completed));
    }

    #[test]
    fn it_does_not_complete_the_task_if_already_done() {
      let mut task = create_task();
      task.complete();
      let completed_at = task.completed_at.unwrap();
      task.complete();

      assert_eq!(task.completed_at, Some(completed_at));
    }
  }

  mod complete_with_note {
    use pretty_assertions::assert_eq;

    use super::*;

    #[test]
    fn it_cancels_the_task_with_a_note() {
      let mut task = create_task();
      task.complete_with_note("a test note");

      assert!(task.is_complete());
      assert_eq!(task.notes.len(), 1);
      assert_eq!(task.notes[0].content, "a test note");
    }
  }
}