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
use std::{
    convert::TryFrom,
    fmt::{self, Display, Formatter},
};

use chrono_elapsed::*;
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};

use super::DueDateTime;

#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct Task {
    pub id: usize,
    pub project_id: usize,
    pub content: String,
    pub description: Option<String>,
    pub completed: State,
    pub label_ids: Option<Vec<usize>>,
    pub parent_id: Option<usize>,
    pub order: usize,
    pub priority: Priority,
    #[serde(rename = "due")] /* Renamed to `due` on (de&)serialisation. */
    pub due_datetime_obj: Option<DueDateTime>,
    pub url: String,
    // IGNORED: section_id; comment_count; assignee; assigner
}

impl Display for Task {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{completed} {content} {due_in} [{id}]",
            completed = self.completed,
            content = self.content,
            due_in = "", // TODO
            id = self.id
        )
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize_repr, Deserialize_repr)]
#[repr(u8)]
pub enum Priority {
    Low = 1,
    Medium = 2,
    High = 3,
    Urgent = 4,
}

impl Default for Priority {
    fn default() -> Self {
        Self::Low
    }
}

impl From<Priority> for u8 {
    fn from(prio: Priority) -> Self {
        match prio {
            Priority::Low => 1,
            Priority::Medium => 2,
            Priority::High => 3,
            Priority::Urgent => 4,
        }
    }
}

impl TryFrom<u8> for Priority {
    type Error = &'static str;
    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            1 => Ok(Self::Low),
            2 => Ok(Self::Medium),
            3 => Ok(Self::High),
            4 => Ok(Self::Urgent),
            _ => Err("`task::Priority` can only be represented by numbers 1-4 (low to urgent)"),
        }
    }
}

impl From<Priority> for String {
    fn from(prio: Priority) -> Self {
        match prio {
            Priority::Low => Self::from("low"),
            Priority::Medium => Self::from("medium"),
            Priority::High => Self::from("high"),
            Priority::Urgent => Self::from("urgent"),
        }
    }
}

impl TryFrom<&str> for Priority {
    type Error = &'static str;
    fn try_from(value: &str) -> Result<Self, Self::Error> {
        match value.trim() {
            "low" => Ok(Self::Low),
            "medium" => Ok(Self::Medium),
            "high" => Ok(Self::High),
            "urgent" => Ok(Self::Urgent),
            _ => Err("`task::Priority` valid strings are: 'low', 'medium', 'high' and 'urgent'"),
        }
    }
}

impl Display for Priority {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            Priority::Low => write!(f, "{}", String::from(Priority::Low)),
            Priority::Medium => write!(f, "{}", String::from(Priority::Medium)),
            Priority::High => write!(f, "{}", String::from(Priority::High)),
            Priority::Urgent => write!(f, "{}", String::from(Priority::Urgent)),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize_repr, Deserialize_repr)]
#[repr(u8)]
pub enum State {
    Incomplete = 0, // Closed
    Complete = 1,   // Open
}

impl State {
    pub fn as_char(&self) -> char {
        char::from(*self)
    }
    pub fn as_bool(&self) -> bool {
        bool::from(*self)
    }
}

impl Default for State {
    fn default() -> Self {
        Self::Incomplete
    }
}

impl From<State> for bool {
    fn from(state: State) -> Self {
        match state {
            State::Complete => true,
            State::Incomplete => false,
        }
    }
}

impl From<bool> for State {
    fn from(b: bool) -> Self {
        match b {
            true => Self::Complete,
            false => Self::Incomplete,
        }
    }
}

impl From<State> for char {
    fn from(state: State) -> Self {
        match state {
            State::Complete => '✔',
            State::Incomplete => '☐',
        }
    }
}

impl TryFrom<char> for State {
    type Error = &'static str;
    fn try_from(value: char) -> Result<Self, Self::Error> {
        match value {
            '✔' => Ok(Self::Complete),
            '☐' => Ok(Self::Incomplete),
            _ => Err("invalid char for `task::State`"),
        }
    }
}

impl Display for State {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            Self::Complete => write!(f, "{}", char::from(Self::Complete)),
            Self::Incomplete => write!(f, "{}", char::from(Self::Incomplete)),
        }
    }
}