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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
//! Tasks related code.

use crate::{
  config::Config, error::Error, filter::TaskDescriptionFilter, metadata::Metadata,
  metadata::Priority,
};
use chrono::{DateTime, Duration, Utc};
use serde::{Deserialize, Serialize};
use serde_json as json;
use std::{cmp::Reverse, collections::HashMap, fmt, fs, str::FromStr};
use unicase::UniCase;

/// Create, edit, remove and list tasks.
#[derive(Debug, Deserialize, Serialize)]
pub struct TaskManager {
  /// Next UID to use for the next task to create.
  next_uid: UID,
  /// List of known tasks.
  tasks: HashMap<UID, Task>,
}

impl TaskManager {
  /// Create a manager from a configuration.
  pub fn new_from_config(config: &Config) -> Result<Self, Error> {
    let path = config.tasks_path();

    if path.is_file() {
      Ok(json::from_reader(
        fs::File::open(path).map_err(Error::CannotOpenFile)?,
      )?)
    } else {
      let task_mgr = TaskManager {
        next_uid: UID::default(),
        tasks: HashMap::new(),
      };
      Ok(task_mgr)
    }
  }

  /// Increment the next UID to use.
  fn increment_uid(&mut self) {
    let uid = self.next_uid.0 + 1;
    self.next_uid = UID(uid);
  }

  /// Register a task and give it an [`UID`].
  pub fn register_task(&mut self, task: Task) -> UID {
    let uid = self.next_uid;

    self.increment_uid();
    self.tasks.insert(uid, task);

    uid
  }

  pub fn save(&mut self, config: &Config) -> Result<(), Error> {
    Ok(json::to_writer_pretty(
      fs::File::create(config.tasks_path()).map_err(Error::CannotSave)?,
      self,
    )?)
  }

  pub fn tasks(&self) -> impl Iterator<Item = (&UID, &Task)> {
    self.tasks.iter()
  }

  pub fn get(&self, uid: UID) -> Option<&Task> {
    self.tasks.get(&uid)
  }

  pub fn get_mut(&mut self, uid: UID) -> Option<&mut Task> {
    self.tasks.get_mut(&uid)
  }

  pub fn rename_project(
    &mut self,
    current_project: impl AsRef<str>,
    new_project: impl AsRef<str>,
    mut on_renamed: impl FnMut(UID),
  ) {
    let current_project = current_project.as_ref();
    let new_project = new_project.as_ref();

    for (uid, task) in &mut self.tasks {
      match task.project() {
        Some(project) if project == current_project => {
          task.set_project(new_project);
          on_renamed(*uid);
        }

        _ => (),
      }
    }
  }

  /// Get a listing of tasks that can be filtered with metadata and name filters.
  pub fn filtered_task_listing(
    &self,
    metadata: Vec<Metadata>,
    name_filter: TaskDescriptionFilter,
    todo: bool,
    start: bool,
    done: bool,
    cancelled: bool,
    case_insensitive: bool,
  ) -> Vec<(&UID, &Task)> {
    let mut tasks: Vec<_> = self
      .tasks()
      .filter(|(_, task)| {
        // filter the task depending on what is passed as argument
        let status_filter = match task.status() {
          Status::Ongoing => start,
          Status::Todo => todo,
          Status::Done => done,
          Status::Cancelled => cancelled,
        };

        if metadata.is_empty() {
          status_filter
        } else {
          status_filter && task.check_metadata(metadata.iter(), case_insensitive)
        }
      })
      .filter(|(_, task)| {
        if !name_filter.is_empty() {
          let mut name_filter = name_filter.clone();

          for word in task.name().split_ascii_whitespace() {
            let word_found = name_filter.remove(word);

            if word_found && name_filter.is_empty() {
              return true;
            }
          }

          false
        } else {
          true
        }
      })
      .collect();

    tasks.sort_by_key(|&(uid, task)| Reverse((task.priority(), task.age(), task.status(), uid)));

    tasks
  }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Task {
  /// Name of the task.
  name: String,
  /// Event history.
  history: Vec<Event>,
}

impl Task {
  /// Create a new [`Task`] and populate automatically its history with creation date and status.
  pub fn new(name: impl Into<String>) -> Self {
    let date = Utc::now();

    Task {
      name: name.into(),
      history: vec![
        Event::Created(date),
        Event::StatusChanged {
          event_date: date,
          status: Status::Todo,
        },
      ],
    }
  }

  /// Get the name of the [`Task`].
  pub fn name(&self) -> &str {
    &self.name
  }

  /// Get the current status of the [`Task`].
  pub fn status(&self) -> Status {
    self
      .history
      .iter()
      .filter_map(|event| match event {
        Event::StatusChanged { status, .. } => Some(status),
        _ => None,
      })
      .copied()
      .last()
      .unwrap_or(Status::Todo)
  }

  /// Get the creation date of the [`Task`].
  pub fn creation_date(&self) -> Option<&DateTime<Utc>> {
    self.history.iter().find_map(|event| match event {
      Event::Created(ref date) => Some(date),
      _ => None,
    })
  }

  /// Get the age of the [`Task`]; i.e. the duration since its creation date.
  pub fn age(&self) -> Duration {
    Utc::now().signed_duration_since(self.creation_date().copied().unwrap_or_else(Utc::now))
  }

  /// Change the name of the [`Task`].
  pub fn change_name(&mut self, name: impl Into<String>) {
    self.name = name.into()
  }

  /// Change the status of the [`Task`].
  pub fn change_status(&mut self, status: Status) {
    self.history.push(Event::StatusChanged {
      event_date: Utc::now(),
      status,
    });
  }

  /// Add a new note to the [`Task`].
  pub fn add_note(&mut self, content: impl Into<String>) {
    self.history.push(Event::NoteAdded {
      event_date: Utc::now(),
      content: content.into(),
    });
  }

  /// Replace the content of a note for a given [`Task`].
  pub fn replace_note(&mut self, note_uid: UID, content: impl Into<String>) -> Result<(), Error> {
    // ensure the note exists first
    let mut count = 0;
    let id: u32 = note_uid.into();
    let previous_note = self.history.iter().find(|event| match event {
      Event::NoteAdded { .. } => {
        if id == count {
          true
        } else {
          count += 1;
          false
        }
      }

      _ => false,
    });

    if previous_note.is_none() {
      return Err(Error::UnknownNote(note_uid));
    }

    self.history.push(Event::NoteReplaced {
      event_date: Utc::now(),
      note_uid,
      content: content.into(),
    });

    Ok(())
  }

  /// Iterate over the notes, if any.
  pub fn notes(&self) -> Vec<Note> {
    let mut notes = Vec::new();

    for event in &self.history {
      match event {
        Event::NoteAdded {
          event_date,
          content,
        } => {
          let note = Note {
            creation_date: *event_date,
            last_modification_date: *event_date,
            content: content.clone(),
          };
          notes.push(note);
        }

        Event::NoteReplaced {
          event_date,
          note_uid,
          content,
        } => {
          if let Some(note) = notes.get_mut(usize::from(*note_uid)) {
            note.last_modification_date = *event_date;
            note.content = content.clone();
          }
        }

        _ => (),
      }
    }

    notes
  }

  /// Iterate over the whole history, if any.
  pub fn history(&self) -> impl Iterator<Item = &Event> {
    self.history.iter()
  }

  /// Compute the time spent on this task.
  pub fn spent_time(&self) -> Duration {
    let (spent, last_wip) =
      self
        .history
        .iter()
        .fold((Duration::zero(), None), |(spent, last_wip), event| {
          match event {
            Event::StatusChanged { event_date, status } => match (status, last_wip) {
              // We go from any status to WIP status; return the spent time untouched and set the new “last_wip” with the
              // time at which the status change occurred
              (Status::Ongoing, _) => (spent, Some(*event_date)),
              // We go to anything but WIP while the previous status was WIP; accumulate.
              (_, Some(last_wip)) => (spent + (event_date.signed_duration_since(last_wip)), None),
              // We go between inactive status, ignore
              _ => (spent, last_wip),
            },
            _ => (spent, last_wip),
          }
        });

    if let Some(last_wip) = last_wip {
      // last status was WIP; accumulate moaaar
      spent + Utc::now().signed_duration_since(last_wip)
    } else {
      spent
    }
  }

  /// Mark this task as part of the input project.
  ///
  /// If a project was already present, this method overrides it. Passing an empty string puts that task into the
  /// _orphaned_ project.
  pub fn set_project(&mut self, project: impl Into<String>) {
    self.history.push(Event::SetProject {
      event_date: Utc::now(),
      project: project.into(),
    });
  }

  /// Set the priority of this task.
  ///
  /// If a priority was already set, this method overrides it. Passing [`None`] removes the priority.
  pub fn set_priority(&mut self, priority: Priority) {
    self.history.push(Event::SetPriority {
      event_date: Utc::now(),
      priority,
    });
  }

  /// Add a tag to task.
  pub fn add_tag(&mut self, tag: impl Into<String>) {
    self.history.push(Event::AddTag {
      event_date: Utc::now(),
      tag: tag.into(),
    });
  }

  /// Apply a list of metadata.
  pub fn apply_metadata(&mut self, metadata: impl IntoIterator<Item = Metadata>) {
    for md in metadata {
      match md {
        Metadata::Project(project) => self.set_project(project),
        Metadata::Priority(priority) => self.set_priority(priority),
        Metadata::Tag(tag) => self.add_tag(tag),
      }
    }
  }

  /// Check all metadata against this I have no idea how to express the end of this sentence so good luck.
  pub fn check_metadata<'a>(
    &self,
    metadata: impl IntoIterator<Item = &'a Metadata>,
    case_insensitive: bool,
  ) -> bool {
    if case_insensitive {
      let own_project = self.project().map(UniCase::new);
      let own_tags = self.tags().map(UniCase::new).collect::<Vec<_>>();
      metadata.into_iter().all(|md| match md {
        Metadata::Project(ref project) => own_project == Some(UniCase::new(project)),
        Metadata::Priority(priority) => self.priority() == Some(*priority),
        Metadata::Tag(ref tag) => own_tags.contains(&UniCase::new(tag)),
      })
    } else {
      metadata.into_iter().all(|md| match md {
        Metadata::Project(ref project) => self.project() == Some(project),
        Metadata::Priority(priority) => self.priority() == Some(*priority),
        Metadata::Tag(ref tag) => self.tags().any(|t| t == tag),
      })
    }
  }

  /// Get the current project.
  pub fn project(&self) -> Option<&str> {
    self
      .history
      .iter()
      .filter_map(|event| match event {
        Event::SetProject { ref project, .. } => Some(project.as_str()),
        _ => None,
      })
      .last()
  }

  /// Get the current project.
  pub fn priority(&self) -> Option<Priority> {
    self
      .history
      .iter()
      .filter_map(|event| match event {
        Event::SetPriority { priority, .. } => Some(*priority),
        _ => None,
      })
      .last()
  }

  /// Get the current tags of a task.
  pub fn tags(&self) -> impl Iterator<Item = &str> {
    self.history.iter().filter_map(|event| match event {
      Event::AddTag { ref tag, .. } => Some(tag.as_str()),
      _ => None,
    })
  }
}

/// Unique identifier.
#[derive(Clone, Copy, Debug, Deserialize, Hash, Eq, Ord, PartialEq, PartialOrd, Serialize)]
pub struct UID(u32);

impl UID {
  pub fn val(self) -> u32 {
    self.0
  }

  pub fn dec(self) -> Self {
    Self(self.0.checked_sub(1).unwrap_or(0))
  }
}

impl From<UID> for u32 {
  fn from(uid: UID) -> Self {
    uid.0
  }
}

impl From<UID> for usize {
  fn from(uid: UID) -> Self {
    uid.0 as _
  }
}

impl Default for UID {
  fn default() -> Self {
    UID(0)
  }
}

impl FromStr for UID {
  type Err = <u32 as FromStr>::Err;

  fn from_str(s: &str) -> Result<Self, Self::Err> {
    u32::from_str(s).map(UID)
  }
}

impl fmt::Display for UID {
  fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
    self.0.fmt(f)
  }
}

/// State of a task.
#[derive(Clone, Copy, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
pub enum Status {
  /// An “ongoing” state.
  ///
  /// Users will typically have “ONGOING”, “WIP”, etc.
  Ongoing,
  /// A “todo” state.
  ///
  /// Users will typically have “TODO“, “PLANNED”, etc.
  Todo,
  /// A “done” state.
  ///
  /// Users will typically have "DONE".
  Done,
  /// A “cancelled” state.
  ///
  /// Users will typically have "CANCELLED", "WONTFIX", etc.
  Cancelled,
}

/// Task event.
///
/// Such events occurred when a change is made to a task (created, edited, scheduled, state
/// changed, etc.).
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub enum Event {
  /// Event generated when a task is created.
  Created(DateTime<Utc>),

  /// Event generated when the status of a task changes.
  StatusChanged {
    event_date: DateTime<Utc>,
    status: Status,
  },

  /// Event generated when a note is added to a task.
  NoteAdded {
    event_date: DateTime<Utc>,
    content: String,
  },

  /// Event generated when a note is replaced in a task.
  NoteReplaced {
    event_date: DateTime<Utc>,
    note_uid: UID,
    content: String,
  },

  /// Event generated when a project is set on a task.
  SetProject {
    event_date: DateTime<Utc>,
    project: String,
  },

  /// Event generated when a priority is set on a task.
  SetPriority {
    event_date: DateTime<Utc>,
    priority: Priority,
  },

  /// Event generated when a tag is added to a task.
  AddTag {
    event_date: DateTime<Utc>,
    tag: String,
  },
}

/// A note.
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Note {
  pub creation_date: DateTime<Utc>,
  pub last_modification_date: DateTime<Utc>,
  pub content: String,
}