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
extern crate chrono;

use std::collections::HashMap;
use std::str::FromStr;
use std::fmt::{self, Write};
pub use chrono::NaiveDate as Date;

#[derive(Debug, Eq, PartialEq)]
pub enum Recurrence {
    Daily(bool, u16),
    BDaily(bool, u16),
    Monthly(bool, u16),
    Weekly(bool, u16),
    Yearly(bool, u16),
}

impl FromStr for Recurrence {
    type Err = ();
    fn from_str(s: &str) -> Result<Recurrence, ()> {
        use self::Recurrence::*;

        let hard = s.starts_with("+");

        s[hard as usize..s.len() - 1].parse::<u16>().map_err(|_| ()).and_then(|num| {
            Ok(match &s[s.len() - 1..] {
                "d" => Daily(hard, num),
                "b" => BDaily(hard, num),
                "m" => Monthly(hard, num),
                "w" => Weekly(hard, num),
                "y" => Yearly(hard, num),
                _ => return Err(()),
            })
        })
    }
}

impl fmt::Display for Recurrence {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use self::Recurrence::*;
        try!(f.write_str("rec:"));
        match *self {
            Daily(hard, num) => {
                if hard { try!(f.write_char('+')); }
                try!(write!(f, "{}d", num));
            },
            BDaily(hard, num) => {
                if hard { try!(f.write_char('+')); }
                try!(write!(f, "{}b", num));
            },
            Monthly(hard, num) => {
                if hard { try!(f.write_char('+')); }
                try!(write!(f, "{}m", num));
            },
            Weekly(hard, num) => {
                if hard { try!(f.write_char('+')); }
                try!(write!(f, "{}w", num));
            },
            Yearly(hard, num) => {
                if hard { try!(f.write_char('+')); }
                try!(write!(f, "{}y", num));
            },
        }

        Ok(())
    }
}

#[derive(Debug, Eq, PartialEq, Default)]
pub struct Task {
    pub subject: String,
    pub priority: u8,
    pub create_date: Option<Date>,
    pub finish_date: Option<Date>,
    pub finished: bool,
    pub threshold_date: Option<Date>,
    pub due_date: Option<Date>,
    pub recurrence: Option<Recurrence>,
    pub contexts: Vec<String>,
    pub projects: Vec<String>,
    pub hashtags: Vec<String>,
    pub tags: HashMap<String, String>,
}

impl fmt::Display for Task {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if self.finished {
            try!(f.write_str("x "));
            if let Some(ref finish_date) = self.finish_date {
                try!(write!(f, "{} ", finish_date));
            }
        }

        if self.priority < 26 {
            try!(write!(f, "({}) ", (self.priority + b'A') as char));
        }

        if let Some(ref create_date) = self.create_date {
            try!(write!(f, "{} ", create_date));
        }

        try!(f.write_str(&self.subject));

        if let Some(ref date) = self.due_date {
            try!(write!(f, " due:{}", date));
        }
        if let Some(ref date) = self.threshold_date {
            try!(write!(f, " t:{}", date));
        }
        if let Some(ref rec) = self.recurrence {
            try!(write!(f, " rec:{}", rec));
        }
        for (tag, value) in &self.tags {
            try!(write!(f, " {}:{}", tag, value));
        }

        Ok(())
    }
}

impl FromStr for Task {
    type Err = ();
    fn from_str(mut s: &str) -> Result<Task, ()> {
        // parse finish state
        let (finished, mut finish_date) = if s.starts_with("x ") {
            s = &s[2..];
            (true, s[..10].parse::<Date>().ok())
        } else {
            (false, None)
        };

        if finish_date.is_some() {
            s = &s[11..];
        }

        // parse priority
        let priority = if s.starts_with("(") && &s[2..4] == ") " {
            match s.as_bytes()[1] {
                p @ b'A'...b'Z' => {
                    s = &s[4..];
                    p - b'A'
                }
                _ => 26,
            }
        } else {
            26
        };

        // parse creation date
        let mut create_date = match s[..10].parse::<Date>() {
            Ok(date) => {
                s = &s[11..];
                Some(date)
            }
            Err(_) => None,
        };

        // If creation date follows finished mark, it can be parsed as a finish date,
        // which is wrong. Note finish state part and creation date can be separated
        // by priority, so this confusion is possible only if no priority given.
        if priority == 26 && finish_date.is_some() && create_date.is_none() {
            create_date = finish_date;
            finish_date = None;
        }

        let buf = s;

        // Subject is the line with technical info removed (priority, creation date and finish state, tags).
        let mut subject = Vec::new();

        // FSM to parse line for tags, contexts and projects.

        #[derive(Copy, Clone, Eq, PartialEq)]
        enum St {
            Init,
            Ctx(usize),
            Prj(usize),
            Hash(usize),
            Tag0(usize),
            Tag1(usize, usize),
        }
        let mut state = St::Init;
        let mut contexts = Vec::new();
        let mut projects = Vec::new();
        let mut hashtags = Vec::new();
        let mut tags = HashMap::new();

        // Some known tags: threshold date (`t:`), due date (`due:`) and recurrence (`rec:`).
        let mut threshold_date = None;
        let mut due_date = None;
        let mut recurrence = None;

        for (i, c) in buf.bytes().enumerate() {
            let new_state = match (c, state) {
                (b'@', St::Init) => St::Ctx(i),
                (b'+', St::Init) => St::Prj(i),
                (b'#', St::Init) => St::Hash(i),
                (b'a'...b'z', St::Init) => St::Tag0(i),
                (b':', St::Tag0(j)) => St::Tag1(j, i),
                (b' ', St::Tag0(_)) => St::Init,
                (b' ', St::Ctx(j)) => {
                    if i - j > 1 {
                        contexts.push(buf[j + 1..i].to_owned());
                    }
                    St::Init
                }
                (b' ', St::Prj(j)) => {
                    if i - j > 1 {
                        projects.push(buf[j + 1..i].to_owned());
                    }
                    St::Init
                }
                (b' ', St::Hash(j)) => {
                    if i - j > 1 {
                        hashtags.push(buf[j + 1..i].to_owned());
                    }
                    St::Init
                }
                (b' ', St::Tag1(j, k)) => {
                    if i - k > 1 {
                        match &buf[j..k] {
                            "rec" => {
                                recurrence = buf[k + 1..i].parse::<Recurrence>().ok();
                            }
                            "due" => {
                                due_date = buf[k + 1..i].parse::<Date>().ok();
                            }
                            "t" => {
                                threshold_date = buf[k + 1..i].parse::<Date>().ok();
                            }
                            tag => {
                                tags.insert(tag.to_owned(), buf[k + 1..i].to_owned());
                            }
                        }
                    }
                    St::Init
                }
                _ => state,
            };

            if new_state == St::Init {
                match state {
                    St::Tag0(j) | St::Hash(j) | St::Prj(j) | St::Ctx(j) => {
                        subject.extend(&buf.as_bytes()[j..i]);
                    }
                    St::Init => subject.push(buf.as_bytes()[i]),
                    _ => {}
                }
            }

            state = new_state;
        }

        // Check final state, so tags at the end of line are also parsed.
        match state {
            St::Tag1(j, k) => {
                tags.insert(buf[j..k].to_owned(), buf[k + 1..].to_owned());
            }
            St::Prj(j) => {
                projects.push(buf[j + 1..].to_owned());
                subject.extend(&buf.as_bytes()[j..]);
            }
            St::Ctx(j) => {
                contexts.push(buf[j + 1..].to_owned());
                subject.extend(&buf.as_bytes()[j..]);
            }
            St::Hash(j) => {
                hashtags.push(buf[j + 1..].to_owned());
                subject.extend(&buf.as_bytes()[j..]);
            }
            St::Tag0(j) => {
                subject.extend(&buf.as_bytes()[j..]);
            }
            _ => {}
        }

        let subject = String::from_utf8(subject).unwrap_or_else(|_| s.to_owned());

        Ok(Task {
            subject: subject,
            priority: priority,
            create_date: create_date,
            finish_date: finish_date,
            finished: finished,
            threshold_date: threshold_date,
            due_date: due_date,
            recurrence: recurrence,
            contexts: contexts,
            projects: projects,
            hashtags: hashtags,
            tags: tags,
        })
    }
}

#[cfg(test)]
mod test {
    use super::{Date, Recurrence, Task};

    #[test]
    fn it_works() {
        let todo_item = "(A) 2016-03-24 22:00 сходить на занятие в @microfon rec:+1w \
                         due:2016-04-05 t:2016-04-05 at:20:00";
        let task = todo_item.parse::<Task>().unwrap();
        assert_eq!(task.subject, "22:00 сходить на занятие в @microfon");
        assert_eq!(task,
                   Task {
                       subject: "22:00 сходить на занятие в @microfon".to_owned(),
                       create_date: Some(Date::from_ymd(2016, 3, 24)),
                       priority: 0,
                       recurrence: Some(Recurrence::Weekly(true, 1)),
                       due_date: Some(Date::from_ymd(2016, 4, 5)),
                       threshold_date: Some(Date::from_ymd(2016, 4, 5)),
                       contexts: vec!["microfon".to_owned()],
                       tags: vec![("at".to_owned(), "20:00".to_owned())].into_iter().collect(),
                       ..Task::default()
                   });

        let todo_item = "2016-03-27 сменить загранпаспорт due:2020-08-14 t:2020-04-14 +документы";
        let task = todo_item.parse::<Task>().unwrap();
        assert_eq!(task.subject, "сменить загранпаспорт +документы");
        assert_eq!(task,
                   Task {
                       subject: "сменить загранпаспорт +документы".to_owned(),
                       create_date: Some(Date::from_ymd(2016, 3, 27)),
                       priority: 26,
                       due_date: Some(Date::from_ymd(2020, 8, 14)),
                       threshold_date: Some(Date::from_ymd(2020, 4, 14)),
                       projects: vec!["документы".to_owned()],
                       ..Task::default()
                   });

        let todo_item = "x 2016-03-27 сменить загранпаспорт due:2020-08-14 t:2020-04-14 +документы";
        let task = todo_item.parse::<Task>().unwrap();
        assert_eq!(task.subject, "сменить загранпаспорт +документы");
        assert_eq!(task,
                   Task {
                       subject: "сменить загранпаспорт +документы".to_owned(),
                       create_date: Some(Date::from_ymd(2016, 3, 27)),
                       priority: 26,
                       due_date: Some(Date::from_ymd(2020, 8, 14)),
                       threshold_date: Some(Date::from_ymd(2020, 4, 14)),
                       projects: vec!["документы".to_owned()],
                       finished: true,
                       ..Task::default()
                   });
    }
}