//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
//! Module containing `Task` type as well as trait implementations
use std::result::Result as RResult;
use std::fmt;
use serde::{Serialize, Serializer};
use serde::ser::SerializeMap;
use serde::{Deserialize, Deserializer};
use serde::de::{Visitor, Error, MapAccess};
use uuid::Uuid;
use chrono::Utc;
use priority::TaskPriority;
use status::TaskStatus;
use project::Project;
use tag::Tag;
use date::Date;
use annotation::Annotation;
use uda::{UDA, UDAName, UDAValue};
use urgency::Urgency;
/// Task type
///
/// A task must have four things:
///
/// - A Status
/// - An UUID
/// - An Entry-Date
/// - A Description
///
/// all other Data is optional by taskwarrior. This type is a simple rust representation of the
/// JSON exported by taskwarrior.
///
/// For further explanations of the fields please consult the documentation on https://taskwarrior.org/
///
/// It is deserializeable and serializeable via serde_json, so importing and exporting taskwarrior
/// tasks is simply serializing and deserializing objects of this type.
#[derive(Debug, Clone, PartialEq, Builder)]
#[builder(setter(into))]
pub struct Task {
/// The temporary assigned task id
#[builder(default)]
id: Option<u64>,
/// The status of the task
#[builder(default = "TaskStatus::Pending")]
status: TaskStatus,
/// The uuid which identifies the task and is important for syncing
#[builder(default = "Uuid::new_v4()")]
uuid: Uuid,
/// The entry date, when this task was created
#[builder(default = "Date::from(Utc::now().naive_utc())")]
entry: Date,
/// The description of the task (i.e. its main content)
/// This field is the only mandatory field, when using the TaskBuilder.
description: String,
/// A list of annotations with timestamps
#[builder(default)]
annotations: Option<Vec<Annotation>>,
/// The uuids of other tasks which have to be completed before this one becomes unblocked.
#[builder(default)]
depends: Option<Vec<Uuid>>,
/// The due date of the task
#[builder(default)]
due: Option<Date>,
/// When the task was last deleted or completed
#[builder(default)]
end: Option<Date>,
/// The imask is used internally for recurrence
#[builder(default)]
imask: Option<f64>,
/// The mask is used internally for recurrence
#[builder(default)]
mask: Option<String>,
/// When the task was last modified
#[builder(default)]
modified: Option<Date>,
/// A task can have a parent task
#[builder(default)]
parent: Option<Uuid>,
/// The priority of the task
#[builder(default)]
priority: Option<TaskPriority>,
/// A task can be part of a project. Typically of the form "project.subproject.subsubproject"
#[builder(default)]
project: Option<Project>,
/// The timespan after which this task should recur
#[builder(default)]
recur: Option<String>,
/// When the task becomes ready
#[builder(default)]
scheduled: Option<Date>,
/// When the task becomes active
#[builder(default)]
start: Option<Date>,
/// The tags associated with the task
#[builder(default)]
tags: Option<Vec<Tag>>,
/// When the recurrence stops
#[builder(default)]
until: Option<Date>,
/// This hides the task until the wait date
#[builder(default)]
wait: Option<Date>,
/// This contains the urgency of the task
#[builder(default)]
urgency: Option<Urgency>,
/// A map of user defined attributes
#[builder(default)]
uda: UDA,
}
/*
* TODO: We do not fail if the JSON parsing fails. This panics. We rely on taskwarrior to be nice
* to us. I guess this should be fixed.
*/
impl Task {
/// Create a new Task instance
pub fn new(
id: Option<u64>,
status: TaskStatus,
uuid: Uuid,
entry: Date,
description: String,
annotations: Option<Vec<Annotation>>,
depends: Option<Vec<Uuid>>,
due: Option<Date>,
end: Option<Date>,
imask: Option<f64>,
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>,
urgency: