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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
use regex::Regex;

use crate::timer;
use crate::todo;

/// Setting unused end of Lower/Higher ValueRange makes the filter to include
/// todos that have a given date field undefined
pub const INCLUDE_NONE: i64 = -9_999_998;
const NONE_TITLE: &str = "none";
const ANY_TITLE: &str = "any";

/// 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
    /// Timer is running
    Active,
}

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

/// For filtering by timer
#[derive(Debug, Clone, PartialEq)]
pub struct Timer {
    pub span: ValueSpan,
    pub value: usize,
}
impl Default for Timer {
    fn default() -> Timer {
        Timer {
            value: 0,
            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`
    /// Special values:
    /// * none - select todos with no contexts
    /// * any - select todos that have at least one context
    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`
    /// Special values:
    /// * none - select todos with no contexts
    /// * any - select todos that have at least one context
    pub contexts: Vec<String>,
    /// List of all tags that a todo must include. The search
    /// supports very limited pattern matching:
    /// * `foo*` - finds all todos with tags that starts with `foo`
    /// * `*foo` - finds all todos with tags that ends with `foo`
    /// * `*foo*` - finds all todos with tags that contains `foo`
    /// Special values:
    /// * none - select todos with no tags
    /// * any - select todos that have at least one tag
    pub tags: 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<DateRange>,
    /// Search for a threshold date: any, no threshold date, or withing range
    pub thr: Option<DateRange>,
    /// Search for recurrent todos
    pub rec: Option<Recurrence>,
    /// Search for todos with priority or priority range
    pub pri: Option<Priority>,
    /// Search for todos with timer related stuff: active, inactive, any time spent
    pub tmr: Option<Timer>,
    /// Search for a creation date: any, no create date, or withing range
    pub created: Option<DateRange>,
    /// Search for a finished date: any, no finish date, or withing range
    pub finished: Option<DateRange>,
}

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

            all: TodoStatus::Active,
            due: None,
            thr: None,
            rec: None,
            pri: None,
            tmr: None,
            created: None,
            finished: 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 vec_match(task_list: &[String], filter: &[String]) -> bool {
    if filter.is_empty() {
        return true;
    }
    for f in filter.iter() {
        if (f == NONE_TITLE && task_list.is_empty()) || (f == ANY_TITLE && !task_list.is_empty()) {
            return true;
        }
    }
    for ctx in task_list.iter() {
        let low = ctx.to_lowercase();
        for tag in filter.iter() {
            let ltag = tag.to_lowercase();
            if str_matches(&low, &ltag) {
                return true;
            }
        }
    }
    false
}

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;
        if vec_match(&tasks[idx].contexts, &c.contexts) {
            new_v.push(idx);
        }
    }
    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;
        if vec_match(&tasks[idx].projects, &c.projects) {
            new_v.push(idx);
        }
    }
    new_v
}

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

    let mut new_v: todo::IDVec = Vec::new();
    for i in v.iter() {
        let idx = *i;
        let mut tag_list: Vec<String> = Vec::new();
        for (k, _v) in tasks[idx].tags.iter() {
            tag_list.push(k.to_string());
        }
        if vec_match(&tag_list, &c.tags) {
            new_v.push(idx);
        }
    }
    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::Higher => {
                        if let Some(d) = tasks[idx].due_date {
                            let diff = d - today;
                            if diff.num_days() > due.days.high {
                                new_v.push(idx);
                            }
                        } else if due.days.low == INCLUDE_NONE {
                            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);
                            }
                        } else if due.days.high == INCLUDE_NONE {
                            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_created(tasks: &todo::TaskSlice, v: todo::IDVec, c: &Conf) -> todo::IDVec {
    match &c.created {
        None => v,
        Some(created) => {
            let mut new_v: todo::IDVec = Vec::new();
            for i in v.iter() {
                let idx = *i;
                if date_in_range(&tasks[idx].create_date, &created) {
                    new_v.push(idx);
                }
            }
            new_v
        }
    }
}

fn filter_finished(tasks: &todo::TaskSlice, v: todo::IDVec, c: &Conf) -> todo::IDVec {
    match &c.finished {
        None => v,
        Some(finished) => {
            let mut new_v: todo::IDVec = Vec::new();
            for i in v.iter() {
                let idx = *i;
                if date_in_range(&tasks[idx].finish_date, &finished) {
                    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 mut new_v: todo::IDVec = Vec::new();
            for i in v.iter() {
                let idx = *i;
                if date_in_range(&tasks[idx].threshold_date, &thr) {
                    new_v.push(idx);
                }
            }
            new_v
        }
    }
}

fn date_in_range(date: &Option<chrono::NaiveDate>, range: &DateRange) -> bool {
    let today = chrono::Local::now().date().naive_local();
    match range.span {
        ValueSpan::None => date.is_none(),
        ValueSpan::Any => date.is_some(),
        ValueSpan::Higher => {
            if let Some(d) = date {
                let diff = *d - today;
                diff.num_days() > range.days.high
            } else {
                range.days.low == INCLUDE_NONE
            }
        }
        ValueSpan::Lower => {
            if let Some(d) = date {
                let diff = *d - today;
                diff.num_days() < range.days.low
            } else {
                range.days.high == INCLUDE_NONE
            }
        }
        ValueSpan::Range => {
            if let Some(d) = date {
                let diff = *d - today;
                diff.num_days() >= range.days.low && diff.num_days() <= range.days.high
            } else {
                false
            }
        }
        _ => false,
    }
}

fn filter_timer(tasks: &todo::TaskSlice, v: todo::IDVec, c: &Conf) -> todo::IDVec {
    match &c.tmr {
        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 !timer::is_timer_on(&tasks[idx]) {
                            new_v.push(idx);
                        }
                    }
                    ValueSpan::Active => {
                        if timer::is_timer_on(&tasks[idx]) {
                            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_tag(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_created(tasks, v, c);
    v = filter_finished(tasks, v, c);
    v = filter_threshold(tasks, v, c);
    v = filter_timer(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*"));
    }
}