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
//! A task is something the user wants or needs to do.

use std::fmt;

/// Represents the user-assigned status of a task.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum TaskStatus {
    /// The user has not yet started the task.
    ToDo,

    /// The user has started, but not completed, the task.
    Started,

    /// The user cannot complete the task due to external circumstances.
    Blocked,

    /// The user has completed the task.
    Done,
}

impl TaskStatus {
    /// Return a human-readable name for the task status.
    pub fn display_name(&self) -> &str {
        match self {
            TaskStatus::ToDo => "To Do",
            TaskStatus::Started => "In Progress",
            TaskStatus::Blocked => "Blocked",
            TaskStatus::Done => "Done",
        }
    }
}

/// A task the user wants or needs to do.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Task {
    status: TaskStatus,
    content: String,
}

impl Task {
    /// Create a new task with the specified status and content.
    pub fn new(status: TaskStatus, content: &str) -> Task {
        Task {
            status,
            content: content.to_string(),
        }
    }

    /// Parse a task from its string representation.
    /// A task string always begins with one of four characters:
    /// "*" means `ToDo`, "^" means `Started`, "+" means `Completed`,
    /// and "-" means `Blocked`.  The rest of the string, except for trailing whitespace,
    /// is the content of the task.  Returns `None` if the string is not a valid task.
    pub fn from_string(s: &str) -> Option<Task> {
        let parse_content = |s: &str| s[1..].trim().to_string();
        if s.starts_with("*") {
            Some(Task {
                status: TaskStatus::ToDo,
                content: parse_content(s),
            })
        } else if s.starts_with("^") {
            Some(Task {
                status: TaskStatus::Started,
                content: parse_content(s),
            })
        } else if s.starts_with("+") {
            Some(Task {
                status: TaskStatus::Done,
                content: parse_content(s),
            })
        } else if s.starts_with("-") {
            Some(Task {
                status: TaskStatus::Blocked,
                content: parse_content(s),
            })
        } else {
            None
        }
    }

    /// Returns the status of the task.
    pub fn status(&self) -> TaskStatus {
        self.status
    }

    /// Returns the content of the task.
    pub fn content(&self) -> &str {
        &self.content
    }
}

impl fmt::Display for Task {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self.status {
            TaskStatus::ToDo => write!(f, "* ")?,
            TaskStatus::Started => write!(f, "^ ")?,
            TaskStatus::Done => write!(f, "+ ")?,
            TaskStatus::Blocked => write!(f, "- ")?,
        };
        write!(f, "{}", self.content)
    }
}

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

    #[test]
    fn test_parse_todo() {
        let t = Task::from_string("* INCOMPLETE").expect("Could not parse todo task");
        assert_eq!(t.status(), TaskStatus::ToDo);
        assert_eq!(t.content(), "INCOMPLETE");
    }

    #[test]
    fn test_parse_started() {
        let t = Task::from_string("^ STARTED").expect("Could not parse started task");
        assert_eq!(t.status(), TaskStatus::Started);
        assert_eq!(t.content(), "STARTED");
    }

    #[test]
    fn test_parse_done() {
        let t = Task::from_string("+ Done").expect("Could not parse done task");
        assert_eq!(t.status(), TaskStatus::Done);
        assert_eq!(t.content(), "Done");
    }

    #[test]
    fn test_parse_blocked() {
        let t = Task::from_string("- Blocked").expect("Could not parse blocked task");
        assert_eq!(t.status(), TaskStatus::Blocked);
        assert_eq!(t.content(), "Blocked");
    }

    #[test]
    fn test_parse_ignore() {
        let t = Task::from_string("Comment");
        assert!(t.is_none());
    }

    #[test]
    fn test_parse_ignore_leading_whitespace() {
        let t = Task::from_string("     * COMMENT");
        assert!(t.is_none());
    }

    #[test]
    fn test_trim_whitespace() {
        let t = Task::from_string("+    done      \n").expect("Could not parse task");
        assert_eq!(t.status(), TaskStatus::Done);
        assert_eq!(t.content(), "done");
    }

    #[test]
    fn test_fmt_todo() {
        let t = Task::new(TaskStatus::ToDo, "INCOMPLETE");
        let s = format!("{}", t);
        assert_eq!(s, "* INCOMPLETE");
    }

    #[test]
    fn test_fmt_started() {
        let t = Task::new(TaskStatus::Started, "STARTED");
        let s = format!("{}", t);
        assert_eq!(s, "^ STARTED");
    }

    #[test]
    fn test_fmt_done() {
        let t = Task::new(TaskStatus::Done, "DONE");
        let s = format!("{}", t);
        assert_eq!(s, "+ DONE");
    }

    #[test]
    fn test_fmt_blocked() {
        let t = Task::new(TaskStatus::Blocked, "BLOCKED");
        let s = format!("{}", t);
        assert_eq!(s, "- BLOCKED");
    }
}