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
use regex::Regex;

use chrono;
use todo_txt;

use crate::todo;

/// Span of todo record IDs to process. ID is an order number of the todo
/// record in the file starting from 0
#[derive(Debug, Clone)]
pub enum ItemRange {
    None,
    /// one record
    One(usize),
    /// inclusive range of records `(from, to).
    /// `Range(1, 4)` means all records from 1 through 4: 1, 2, 3, and 4.
    /// Values can exceed the number of todos. All IDs greater than the number
    /// of todos are skipped
    Range(usize, usize),
    /// List of IDs
    List(Vec<usize>),
}

/// Todo state range
#[derive(Debug, Clone, PartialEq)]
pub enum TodoStatus {
    /// Only todos that are incompleted yet
    Active,
    /// All todos
    All,
    /// Only todos marked `done`
    Done,
}

/// An arbitrary range of values for todo properties check. The range is inclusive
#[derive(Debug, Clone, PartialEq)]
pub struct ValueRange {
    pub low: i64,
    pub high: i64,
}

impl Default for ValueRange {
    fn default() -> ValueRange {
        ValueRange { low: 0, high: 0 }
    }
}

/// A type of comparison for the property.
///
/// Every property supports only a limited subset:
/// * `project` and `context`: do not use `ValueSpan` because they always search for a given text;
/// * `priority`: `None`, `Any`, `Equal`, `Lower`, and `Higher`;
/// * `recurrence`: `None` and `Any`;
/// * `due`: `None`, `Any`, `Lower`, and `Range`;
#[derive(Debug, Clone, PartialEq)]
pub enum ValueSpan {
    /// Do not check the property value
    None, // without
    /// Property value must equal a given one (projects and contexts provide
    /// more ways to compare, including simple pattern matching)
    Equal, // one value
    /// Property value must be equal to or less than the given one
    Lower, // -
    /// Property value must be equal to or greater than the given one
    Higher, // +
    /// Property must be set to any value except None or empty string. Useful,
    /// e.g, to select all todos with any due date or priority
    Any, // any|(+ without priority)
    /// Property value must be within range (range is inclusive)
    Range, // from - to
}

/// For filtering by due date. `days` is inclusive range and is not used when
/// `span` is `Any` or `None`
#[derive(Debug, Clone, PartialEq)]
pub struct Due {
    pub days: ValueRange,
    pub span: ValueSpan,
}
impl Default for Due {
    fn default() -> Due {
        Due {
            span: ValueSpan::None,
            days: Default::default(),
        }
    }
}

/// For filtering by recurrence. Only `Any` and `None` are supported
#[derive(Debug, Clone, PartialEq)]
pub struct Recurrence {
    pub span: ValueSpan,
}
impl Default for Recurrence {
    fn default() -> Recurrence {
        Recurrence { span: ValueSpan::None }
    }
}

/// For filtering by priority
#[derive(Debug, Clone, PartialEq)]
pub struct Priority {
    pub value: u8,
    pub span: ValueSpan,
}
impl Default for Priority {
    fn default() -> Priority {
        Priority {
            value: todo::NO_PRIORITY,
            span: ValueSpan::None,
        }
    }
}

/// A rules for todo list filtering. Setting a field to None or empty vector
/// means that the corresponding property is not checked.
/// All text comparisons are case-insensitive.
#[derive(Debug, Clone)]
pub struct Conf {
    /// Range of todo IDs
    pub range: ItemRange,
    /// List of all project tags that a todo must include. The search
    /// supports very limited pattern matching:
    /// * `foo*` - finds all todos with projects that starts with `foo`
    /// * `*foo` - finds all todos with projects that ends with `foo`
    /// * `*foo*` - finds all todos with projects that contains `foo`
    pub projects: Vec<String>,
    /// List of all context tags that a todo must include. The search
    /// supports very limited pattern matching:
    /// * `foo*` - finds all todos with contexts that starts with `foo`
    /// * `*foo` - finds all todos with contexts that ends with `foo`
    /// * `*foo*` - finds all todos with contexts that contains `foo`
    pub contexts: Vec<String>,
    /// A text that any of text, project, or context must contain
    pub regex: Option<String>,
    /// If it is `true`, `regex` is treated as regular expression. If `use_regex`
    /// is `false`, the value of `regex` is just a substring to search for
    pub use_regex: bool,

    /// All incomplete, completed, or both types of todos
    pub all: TodoStatus,

    /// Search for a due date: any, no due date, or withing range
    pub due: Option<Due>,
    /// Search for a threshold date: any, no threshold date, or withing range
    pub thr: Option<Due>,
    /// Search for a recurrent todos
    pub rec: Option<Recurrence>,
    /// Search for a todos with priority or priority range
    pub pri: Option<Priority>,
}

impl Default for Conf {
    fn default() -> Conf {
        Conf {
            range: ItemRange::None,
            projects: Vec::new(),
            contexts: Vec::new(),
            regex: None,
            use_regex: false,

            all: TodoStatus::Active,
            due: None,
            thr: None,
            rec: None,
            pri: None,
        }
    }
}

fn filter_regex(tasks: &todo::TaskSlice, v: todo::IDVec, c: &Conf) -> todo::IDVec {
    let rx = match &c.regex {
        None => return v,
        Some(s) => s,
    };

    let mut new_v: todo::IDVec = Vec::new();
    if c.use_regex {
        let rx = match Regex::new(&format!("(?i){}", rx)) {
            Err(_) => {
                println!("Invalid regex");
                return v;
            }
            Ok(v) => v,
        };

        for i in v.iter() {
            let idx = *i;
            if idx >= tasks.len() {
                continue;
            }
            if rx.is_match(&tasks[idx].subject) {
                new_v.push(idx);
            }
        }
        return new_v;
    }

    let rstr = rx.to_lowercase();
    for i in v.iter() {
        let idx = *i;
        let low = tasks[idx].subject.to_lowercase();
        if low.find(&rstr).is_some() {
            new_v.push(idx);
            continue;
        }
    }
    new_v
}

fn filter_context(tasks: &todo::TaskSlice, v: todo::IDVec, c: &Conf) -> todo::IDVec {
    if c.contexts.is_empty() {
        return v;
    }

    let mut new_v: todo::IDVec = Vec::new();
    for i in v.iter() {
        let idx = *i;
        'outer: for ctx in tasks[idx].contexts.iter() {
            let low = ctx.to_lowercase();
            for tag in c.contexts.iter() {
                let tag = tag.to_lowercase();
                if str_matches(&low, &tag) {
                    new_v.push(idx);
                    break 'outer;
                }
            }
        }
    }
    new_v
}

fn filter_project(tasks: &todo::TaskSlice, v: todo::IDVec, c: &Conf) -> todo::IDVec {
    if c.projects.is_empty() {
        return v;
    }

    let mut new_v: todo::IDVec = Vec::new();
    for i in v.iter() {
        let idx = *i;
        'outer: for prj in tasks[idx].projects.iter() {
            let low = prj.to_lowercase();
            for tag in c.projects.iter() {
                let tag = tag.to_lowercase();
                if str_matches(&low, &tag) {
                    new_v.push(idx);
                    break 'outer;
                }
            }
        }
    }
    new_v
}

fn filter_priority(tasks: &todo::TaskSlice, v: todo::IDVec, c: &Conf) -> todo::IDVec {
    match &c.pri {
        None => v,
        Some(p) => {
            let mut new_v: todo::IDVec = Vec::new();
            for i in v.iter() {
                let idx = *i;
                match p.span {
                    ValueSpan::None => {
                        if tasks[idx].priority == todo::NO_PRIORITY {
                            new_v.push(idx);
                        }
                    }
                    ValueSpan::Equal => {
                        if p.value == tasks[idx].priority {
                            new_v.push(idx);
                        }
                    }
                    ValueSpan::Lower => {
                        if p.value <= tasks[idx].priority {
                            new_v.push(idx);
                        }
                    }
                    ValueSpan::Higher => {
                        if p.value >= tasks[idx].priority {
                            new_v.push(idx);
                        }
                    }
                    ValueSpan::Any => {
                        if tasks[idx].priority < todo::NO_PRIORITY {
                            new_v.push(idx);
                        }
                    }
                    _ => {}
                }
            }
            new_v
        }
    }
}

fn filter_recurrence(tasks: &todo::TaskSlice, v: todo::IDVec, c: &Conf) -> todo::IDVec {
    match &c.rec {
        None => v,
        Some(r) => {
            let mut new_v: todo::IDVec = Vec::new();
            for i in v.iter() {
                let idx = *i;
                match r.span {
                    ValueSpan::None => {
                        if tasks[idx].recurrence.is_none() {
                            new_v.push(idx);
                        }
                    }
                    ValueSpan::Any => {
                        if tasks[idx].recurrence.is_some() {
                            new_v.push(idx);
                        }
                    }
                    _ => {}
                }
            }
            new_v
        }
    }
}

fn filter_due(tasks: &todo::TaskSlice, v: todo::IDVec, c: &Conf) -> todo::IDVec {
    match &c.due {
        None => v,
        Some(due) => {
            let today = chrono::Local::now().date().naive_local();
            let mut new_v: todo::IDVec = Vec::new();
            for i in v.iter() {
                let idx = *i;
                match due.span {
                    ValueSpan::None => {
                        if tasks[idx].due_date.is_none() {
                            new_v.push(idx);
                        }
                    }
                    ValueSpan::Any => {
                        if tasks[idx].due_date.is_some() {
                            new_v.push(idx);
                        }
                    }
                    ValueSpan::Lower => {
                        if let Some(d) = tasks[idx].due_date {
                            let diff = d - today;
                            if diff.num_days() < due.days.low {
                                new_v.push(idx);
                            }
                        }
                    }
                    ValueSpan::Range => {
                        if let Some(d) = tasks[idx].due_date {
                            let diff = d - today;
                            if diff.num_days() >= due.days.low && diff.num_days() <= due.days.high {
                                new_v.push(idx);
                            }
                        }
                    }
                    _ => {}
                }
            }
            new_v
        }
    }
}

fn filter_threshold(tasks: &todo::TaskSlice, v: todo::IDVec, c: &Conf) -> todo::IDVec {
    match &c.thr {
        None => v,
        Some(thr) => {
            let today = chrono::Local::now().date().naive_local();
            let mut new_v: todo::IDVec = Vec::new();
            for i in v.iter() {
                let idx = *i;
                match thr.span {
                    ValueSpan::None => {
                        if tasks[idx].threshold_date.is_none() {
                            new_v.push(idx);
                        }
                    }
                    ValueSpan::Any => {
                        if tasks[idx].threshold_date.is_some() {
                            new_v.push(idx);
                        }
                    }
                    ValueSpan::Lower => {
                        if let Some(d) = tasks[idx].threshold_date {
                            let diff = d - today;
                            if diff.num_days() < thr.days.low {
                                new_v.push(idx);
                            }
                        }
                    }
                    ValueSpan::Range => {
                        if let Some(d) = tasks[idx].threshold_date {
                            let diff = d - today;
                            if diff.num_days() >= thr.days.low && diff.num_days() <= thr.days.high {
                                new_v.push(idx);
                            }
                        }
                    }
                    _ => {}
                }
            }
            new_v
        }
    }
}

fn is_status_ok(task: &todo_txt::task::Extended, status: &TodoStatus) -> bool {
    !((*status == TodoStatus::Active && task.finished) || (*status == TodoStatus::Done && !task.finished))
}

/// Entry function to filter the list of todo records
///
/// The function does not modify the todo list. It looks through the todo
/// records and fill the vector with ID of todos which meet all the criteria.
///
/// * `tasks` - list of todos to filter
/// * `c` - filtering rules
///
/// Returns:
/// the list of todo IDs which meet filtering criteria
pub fn filter(tasks: &todo::TaskSlice, c: &Conf) -> todo::IDVec {
    let mut v: todo::IDVec = Vec::new();

    match c.range {
        ItemRange::One(i) => {
            if i < tasks.len() && is_status_ok(&tasks[i], &c.all) {
                v.push(i);
            }
        }
        ItemRange::Range(min, max) => {
            let mut start = min;

            while start <= max {
                if start >= tasks.len() {
                    break;
                }
                if is_status_ok(&tasks[start], &c.all) {
                    v.push(start);
                }
                start += 1;
            }
        }
        ItemRange::List(ref lst) => {
            for idx in lst.iter() {
                if *idx == 0 || *idx >= tasks.len() {
                    continue;
                }
                if is_status_ok(&tasks[*idx], &c.all) {
                    v.push(*idx);
                }
            }
        }
        _ => {
            for (i, ref item) in tasks.iter().enumerate() {
                if is_status_ok(item, &c.all) {
                    v.push(i);
                }
            }
        }
    }
    v = filter_regex(tasks, v, c);
    v = filter_project(tasks, v, c);
    v = filter_context(tasks, v, c);
    v = filter_priority(tasks, v, c);
    v = filter_recurrence(tasks, v, c);
    v = filter_due(tasks, v, c);
    v = filter_threshold(tasks, v, c);

    v
}

fn str_matches(orig: &str, patt: &str) -> bool {
    if patt.starts_with('*') && patt.ends_with('*') {
        let p = patt.trim_matches('*');
        orig.find(p).is_some()
    } else if patt.starts_with('*') {
        let p = patt.trim_start_matches('*');
        orig.ends_with(p)
    } else if patt.ends_with('*') {
        let p = patt.trim_end_matches('*');
        orig.starts_with(p)
    } else {
        orig == patt
    }
}

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

    #[test]
    fn matches() {
        assert!(!str_matches("abcd", "abc"));
        assert!(str_matches("abcd", "abcd"));
        assert!(!str_matches("abcd", "abcde"));
        assert!(str_matches("abcd", "abc*"));
        assert!(str_matches("abcd", "*bcd"));
        assert!(str_matches("abcd", "*b*"));
        assert!(!str_matches("abcd", "bc*"));
        assert!(!str_matches("abcd", "*bc"));
        assert!(!str_matches("abcd", ""));
        assert!(!str_matches("", "abcd"));
        assert!(str_matches("abcd", "*d*"));
        assert!(str_matches("abcd", "*a*"));
    }
}