doing-ops 0.2.4

Domain operations for the doing CLI
Documentation
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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
use std::{
  collections::HashMap,
  sync::{LazyLock, Mutex},
};

use chrono::{DateTime, Local};
use doing_taskpaper::Entry;
use doing_time::{chronify, parse_duration};
use regex::Regex;

static VALUE_QUERY_RE: LazyLock<Regex> =
  LazyLock::new(|| Regex::new(r"^(!)?@?(\S+)\s+(!?[<>=][=*]?|[$*^]=)\s+(.+)$").unwrap());

static WILDCARD_CACHE: Mutex<Option<HashMap<String, Regex>>> = Mutex::new(None);

/// A comparison operator for tag value queries.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ComparisonOp {
  /// String contains (`*=`).
  Contains,
  /// String ends with (`$=`).
  EndsWith,
  /// Equal (`==` or `=`).
  Equal,
  /// Greater than (`>`).
  GreaterThan,
  /// Greater than or equal (`>=`).
  GreaterThanOrEqual,
  /// Less than (`<`).
  LessThan,
  /// Less than or equal (`<=`).
  LessThanOrEqual,
  /// String starts with (`^=`).
  StartsWith,
}

/// A virtual property or tag name that a query targets.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Property {
  /// Start date of the entry.
  Date,
  /// Elapsed time since start (for unfinished entries).
  Duration,
  /// Time between start and `@done` date.
  Interval,
  /// Entry note text.
  Note,
  /// A named tag's value.
  Tag(String),
  /// Combined title and note text.
  Text,
  /// Time-of-day portion of the start date.
  Time,
  /// Entry title.
  Title,
}

/// A parsed tag value query from a `--val` flag.
///
/// Format: `[!][@]property operator value`
#[derive(Clone, Debug)]
pub struct TagQuery {
  negated: bool,
  op: ComparisonOp,
  property: Property,
  value: String,
}

impl TagQuery {
  /// Parse a query string into a structured `TagQuery`.
  ///
  /// Format: `[!][@]property operator value`
  ///
  /// The `@` prefix on the property name is optional and stripped during
  /// parsing, so `"project == clientA"` and `"@project == clientA"` are
  /// equivalent.
  ///
  /// Operators: `<`, `<=`, `>`, `>=`, `==`, `=`, `!=`, `*=`, `^=`, `$=`
  pub fn parse(input: &str) -> Option<Self> {
    let caps = VALUE_QUERY_RE.captures(input.trim())?;

    let negated = caps.get(1).is_some();
    let property = parse_property(&caps[2]);
    let raw_op = &caps[3];
    let value = caps[4].trim().to_lowercase();

    let (op, op_negated) = parse_operator(raw_op)?;
    let negated = negated ^ op_negated;

    Some(Self {
      negated,
      op,
      property,
      value,
    })
  }

  /// Test whether an entry matches this query.
  pub fn matches_entry(&self, entry: &Entry) -> bool {
    let result = self.evaluate(entry);
    if self.negated { !result } else { result }
  }

  fn compare_date(&self, entry_date: DateTime<Local>, value: &str) -> bool {
    if is_string_op(self.op) {
      return false;
    }
    let Ok(target) = chronify(value) else {
      return false;
    };
    compare_ord(entry_date, target, self.op)
  }

  fn compare_duration(&self, entry: &Entry, is_interval: bool) -> bool {
    if is_string_op(self.op) {
      return false;
    }
    let entry_duration = if is_interval {
      entry.interval()
    } else {
      entry.duration()
    };
    let Some(entry_duration) = entry_duration else {
      return false;
    };

    let Ok(target) = parse_duration(&self.value) else {
      return false;
    };

    compare_ord(entry_duration.num_seconds(), target.num_seconds(), self.op)
  }

  fn compare_numeric(&self, entry_val: f64, target_val: f64) -> bool {
    let ordering = entry_val.total_cmp(&target_val);
    match self.op {
      ComparisonOp::Equal => ordering == std::cmp::Ordering::Equal,
      ComparisonOp::GreaterThan => ordering == std::cmp::Ordering::Greater,
      ComparisonOp::GreaterThanOrEqual => ordering != std::cmp::Ordering::Less,
      ComparisonOp::LessThan => ordering == std::cmp::Ordering::Less,
      ComparisonOp::LessThanOrEqual => ordering != std::cmp::Ordering::Greater,
      ComparisonOp::Contains | ComparisonOp::StartsWith | ComparisonOp::EndsWith => {
        debug_assert!(
          false,
          "string operators must be handled by compare_string, not compare_numeric"
        );
        false
      }
    }
  }

  fn compare_string(&self, haystack: &str, needle: &str) -> bool {
    let n = strip_quotes(needle);
    let h = haystack.to_lowercase();

    match self.op {
      ComparisonOp::Contains => h.contains(n),
      ComparisonOp::EndsWith => h.ends_with(n),
      ComparisonOp::Equal => wildcard_match(&h, n),
      ComparisonOp::StartsWith => h.starts_with(n),
      ComparisonOp::GreaterThan => h.as_str() > n,
      ComparisonOp::GreaterThanOrEqual => h.as_str() >= n,
      ComparisonOp::LessThan => (h.as_str()) < n,
      ComparisonOp::LessThanOrEqual => h.as_str() <= n,
    }
  }

  fn compare_time(&self, entry: &Entry) -> bool {
    if is_string_op(self.op) {
      return false;
    }
    // Parse the target as a date/time expression, then compare time-of-day
    let Ok(target) = chronify(&self.value) else {
      return false;
    };
    let entry_time = entry.date().time();
    let target_time = target.time();
    compare_ord(entry_time, target_time, self.op)
  }

  fn evaluate(&self, entry: &Entry) -> bool {
    match &self.property {
      Property::Date => self.compare_date(entry.date(), &self.value),
      Property::Duration => self.compare_duration(entry, false),
      Property::Interval => self.compare_duration(entry, true),
      Property::Note => self.compare_string(&entry.note().to_line(" "), &self.value),
      Property::Tag(name) => self.evaluate_tag(entry, name),
      Property::Text => {
        let text = format!("{} {}", entry.title(), entry.note().to_line(" "));
        self.compare_string(&text, &self.value)
      }
      Property::Time => self.compare_time(entry),
      Property::Title => self.compare_string(entry.title(), &self.value),
    }
  }

  fn evaluate_tag(&self, entry: &Entry, tag_name: &str) -> bool {
    let tag_value = match entry
      .tags()
      .iter()
      .find(|t| t.name().eq_ignore_ascii_case(tag_name))
      .and_then(|t| t.value().map(String::from))
    {
      Some(v) => v,
      None => return false,
    };

    if is_string_op(self.op) {
      return self.compare_string(&tag_value, &self.value);
    }

    // Try numeric comparison
    let entry_num = parse_numeric(&tag_value);
    let target_num = parse_numeric(&self.value);
    if let (Some(e), Some(t)) = (entry_num, target_num) {
      return self.compare_numeric(e, t);
    }

    // Try date comparison
    if let (Ok(entry_dt), Ok(target_dt)) = (chronify(&tag_value), chronify(&self.value)) {
      return compare_ord(entry_dt, target_dt, self.op);
    }

    // Try duration comparison
    if let (Ok(entry_dur), Ok(target_dur)) = (parse_duration(&tag_value), parse_duration(&self.value)) {
      return compare_ord(entry_dur.num_seconds(), target_dur.num_seconds(), self.op);
    }

    // Fall back to string comparison
    self.compare_string(&tag_value, &self.value)
  }
}

/// Look up or compile and cache a wildcard regex.
fn cached_wildcard_regex(pattern: &str) -> Option<Regex> {
  let mut guard = WILDCARD_CACHE.lock().unwrap_or_else(|e| e.into_inner());
  let cache = guard.get_or_insert_with(HashMap::new);
  if let Some(rx) = cache.get(pattern) {
    return Some(rx.clone());
  }
  let rx_str = wildcard_to_regex(pattern);
  let rx = Regex::new(&rx_str).ok()?;
  cache.insert(pattern.to_string(), rx.clone());
  Some(rx)
}

/// Apply a comparison operator to two ordered values.
fn compare_ord<T: PartialOrd>(a: T, b: T, op: ComparisonOp) -> bool {
  match op {
    ComparisonOp::Equal => a == b,
    ComparisonOp::GreaterThan => a > b,
    ComparisonOp::GreaterThanOrEqual => a >= b,
    ComparisonOp::LessThan => a < b,
    ComparisonOp::LessThanOrEqual => a <= b,
    ComparisonOp::Contains | ComparisonOp::StartsWith | ComparisonOp::EndsWith => {
      debug_assert!(
        false,
        "string operators must be handled by compare_string, not compare_ord"
      );
      false
    }
  }
}

/// Check whether an operator is a string-specific operator.
fn is_string_op(op: ComparisonOp) -> bool {
  matches!(
    op,
    ComparisonOp::Contains | ComparisonOp::StartsWith | ComparisonOp::EndsWith
  )
}

/// Parse a numeric value, stripping trailing `%`.
fn parse_numeric(s: &str) -> Option<f64> {
  let s = s.trim().trim_end_matches('%').trim();
  s.parse::<f64>().ok()
}

/// Parse an operator string into a `ComparisonOp` and negation flag.
fn parse_operator(raw: &str) -> Option<(ComparisonOp, bool)> {
  match raw {
    "<" => Some((ComparisonOp::LessThan, false)),
    "<=" => Some((ComparisonOp::LessThanOrEqual, false)),
    ">" => Some((ComparisonOp::GreaterThan, false)),
    ">=" => Some((ComparisonOp::GreaterThanOrEqual, false)),
    "=" | "==" => Some((ComparisonOp::Equal, false)),
    "!=" => Some((ComparisonOp::Equal, true)),
    "*=" => Some((ComparisonOp::Contains, false)),
    "^=" => Some((ComparisonOp::StartsWith, false)),
    "$=" => Some((ComparisonOp::EndsWith, false)),
    "!<" => Some((ComparisonOp::LessThan, true)),
    "!<=" => Some((ComparisonOp::LessThanOrEqual, true)),
    "!>" => Some((ComparisonOp::GreaterThan, true)),
    "!>=" => Some((ComparisonOp::GreaterThanOrEqual, true)),
    _ => None,
  }
}

/// Parse a property name into a `Property` enum.
fn parse_property(name: &str) -> Property {
  match name.to_lowercase().as_str() {
    "date" => Property::Date,
    "duration" => Property::Duration,
    "elapsed" => Property::Duration,
    "interval" => Property::Interval,
    "note" => Property::Note,
    "text" => Property::Text,
    "time" => Property::Time,
    "title" => Property::Title,
    _ => Property::Tag(name.to_string()),
  }
}

/// Strip surrounding double quotes from a string.
fn strip_quotes(s: &str) -> &str {
  s.strip_prefix('"').and_then(|s| s.strip_suffix('"')).unwrap_or(s)
}

/// Case-insensitive wildcard match. `*` matches zero or more chars, `?` matches one.
fn wildcard_match(text: &str, pattern: &str) -> bool {
  if !pattern.contains('*') && !pattern.contains('?') {
    return text == pattern;
  }

  let rx = cached_wildcard_regex(pattern);
  rx.is_some_and(|r| r.is_match(text))
}

fn wildcard_to_regex(pattern: &str) -> String {
  let mut rx = String::from("(?i)^");
  let mut buf = [0u8; 4];
  for ch in pattern.chars() {
    match ch {
      '*' => rx.push_str(".*"),
      '?' => rx.push('.'),
      _ => rx.push_str(&regex::escape(ch.encode_utf8(&mut buf))),
    }
  }
  rx.push('$');
  rx
}

#[cfg(test)]
mod test {
  use chrono::{Duration, TimeZone};
  use doing_taskpaper::{Note, Tag, Tags};

  use super::*;

  fn entry_with_tag(name: &str, value: Option<&str>) -> Entry {
    Entry::new(
      sample_date(),
      "Working on project",
      Tags::from_iter(vec![Tag::new(name, value)]),
      Note::from_text("Some notes here"),
      "Currently",
      None::<String>,
    )
  }

  fn finished_entry() -> Entry {
    Entry::new(
      sample_date(),
      "Finished task",
      Tags::from_iter(vec![Tag::new("done", Some("2024-03-17 16:00"))]),
      Note::from_text("Task notes"),
      "Currently",
      None::<String>,
    )
  }

  fn sample_date() -> DateTime<Local> {
    Local.with_ymd_and_hms(2024, 3, 17, 14, 30, 0).unwrap()
  }

  mod compare_ord {
    use super::super::*;

    #[test]
    fn it_compares_equal() {
      assert!(compare_ord(5, 5, ComparisonOp::Equal));
      assert!(!compare_ord(5, 6, ComparisonOp::Equal));
    }

    #[test]
    fn it_compares_greater_than() {
      assert!(compare_ord(6, 5, ComparisonOp::GreaterThan));
      assert!(!compare_ord(5, 5, ComparisonOp::GreaterThan));
    }

    #[test]
    fn it_compares_greater_than_or_equal() {
      assert!(compare_ord(5, 5, ComparisonOp::GreaterThanOrEqual));
      assert!(compare_ord(6, 5, ComparisonOp::GreaterThanOrEqual));
      assert!(!compare_ord(4, 5, ComparisonOp::GreaterThanOrEqual));
    }

    #[test]
    fn it_compares_less_than() {
      assert!(compare_ord(4, 5, ComparisonOp::LessThan));
      assert!(!compare_ord(5, 5, ComparisonOp::LessThan));
    }

    #[test]
    fn it_compares_less_than_or_equal() {
      assert!(compare_ord(5, 5, ComparisonOp::LessThanOrEqual));
      assert!(compare_ord(4, 5, ComparisonOp::LessThanOrEqual));
      assert!(!compare_ord(6, 5, ComparisonOp::LessThanOrEqual));
    }
  }

  mod is_string_op {
    use super::super::*;

    #[test]
    fn it_returns_true_for_string_operators() {
      assert!(is_string_op(ComparisonOp::Contains));
      assert!(is_string_op(ComparisonOp::StartsWith));
      assert!(is_string_op(ComparisonOp::EndsWith));
    }

    #[test]
    fn it_returns_false_for_non_string_operators() {
      assert!(!is_string_op(ComparisonOp::Equal));
      assert!(!is_string_op(ComparisonOp::GreaterThan));
      assert!(!is_string_op(ComparisonOp::LessThan));
    }
  }

  mod matches_entry {
    use super::*;

    mod date_property {
      use super::*;

      #[test]
      fn it_matches_date_greater_than() {
        let entry = finished_entry();
        let query = TagQuery::parse("date > 2024-03-16").unwrap();

        assert!(query.matches_entry(&entry));
      }

      #[test]
      fn it_rejects_date_less_than() {
        let entry = finished_entry();
        let query = TagQuery::parse("date < 2024-03-16").unwrap();

        assert!(!query.matches_entry(&entry));
      }

      #[test]
      fn it_returns_false_for_contains_operator() {
        let entry = finished_entry();
        let query = TagQuery::parse("date *= 2024").unwrap();

        assert!(!query.matches_entry(&entry));
      }

      #[test]
      fn it_returns_false_for_ends_with_operator() {
        let entry = finished_entry();
        let query = TagQuery::parse("date $= 17").unwrap();

        assert!(!query.matches_entry(&entry));
      }

      #[test]
      fn it_returns_false_for_starts_with_operator() {
        let entry = finished_entry();
        let query = TagQuery::parse("date ^= 2024").unwrap();

        assert!(!query.matches_entry(&entry));
      }
    }

    mod duration_property {
      use super::*;

      #[test]
      fn it_matches_unfinished_entry_duration() {
        let entry = Entry::new(
          Local::now() - Duration::hours(3),
          "Active task",
          Tags::new(),
          Note::new(),
          "Currently",
          None::<String>,
        );
        let query = TagQuery::parse("duration > 2h").unwrap();

        assert!(query.matches_entry(&entry));
      }

      #[test]
      fn it_returns_false_for_finished_entry() {
        let entry = finished_entry();
        let query = TagQuery::parse("duration > 1h").unwrap();

        assert!(!query.matches_entry(&entry));
      }

      #[test]
      fn it_returns_false_for_contains_operator() {
        let entry = Entry::new(
          Local::now() - Duration::hours(3),
          "Active task",
          Tags::new(),
          Note::new(),
          "Currently",
          None::<String>,
        );
        let query = TagQuery::parse("duration *= 3").unwrap();

        assert!(!query.matches_entry(&entry));
      }
    }

    mod interval_property {
      use super::*;

      #[test]
      fn it_matches_interval_greater_than() {
        let entry = finished_entry();
        let query = TagQuery::parse("interval > 30m").unwrap();

        assert!(query.matches_entry(&entry));
      }

      #[test]
      fn it_rejects_interval_too_large() {
        let entry = finished_entry();
        let query = TagQuery::parse("interval > 3h").unwrap();

        assert!(!query.matches_entry(&entry));
      }

      #[test]
      fn it_returns_false_for_unfinished_entry() {
        let entry = Entry::new(
          sample_date(),
          "Active",
          Tags::new(),
          Note::new(),
          "Currently",
          None::<String>,
        );
        let query = TagQuery::parse("interval > 1h").unwrap();

        assert!(!query.matches_entry(&entry));
      }
    }

    mod negation {
      use super::*;

      #[test]
      fn it_negates_with_exclamation_prefix() {
        let entry = entry_with_tag("progress", Some("80"));
        let query = TagQuery::parse("!progress > 50").unwrap();

        assert!(!query.matches_entry(&entry));
      }

      #[test]
      fn it_negates_with_not_equal_operator() {
        let entry = entry_with_tag("status", Some("active"));
        let query = TagQuery::parse("status != active").unwrap();

        assert!(!query.matches_entry(&entry));
      }
    }

    mod note_property {
      use super::*;

      #[test]
      fn it_matches_note_contains() {
        let entry = finished_entry();
        let query = TagQuery::parse("note *= notes").unwrap();

        assert!(query.matches_entry(&entry));
      }

      #[test]
      fn it_rejects_note_not_found() {
        let entry = finished_entry();
        let query = TagQuery::parse("note *= missing").unwrap();

        assert!(!query.matches_entry(&entry));
      }
    }

    mod numeric_tag {
      use super::*;

      #[test]
      fn it_compares_numeric_tag_value() {
        let entry = entry_with_tag("progress", Some("75"));
        let query = TagQuery::parse("progress >= 50").unwrap();

        assert!(query.matches_entry(&entry));
      }

      #[test]
      fn it_handles_percentage_values() {
        let entry = entry_with_tag("progress", Some("75%"));
        let query = TagQuery::parse("progress >= 50").unwrap();

        assert!(query.matches_entry(&entry));
      }

      #[test]
      fn it_rejects_below_threshold() {
        let entry = entry_with_tag("progress", Some("25"));
        let query = TagQuery::parse("progress >= 50").unwrap();

        assert!(!query.matches_entry(&entry));
      }
    }

    mod string_tag {
      use super::*;

      #[test]
      fn it_matches_contains() {
        let entry = entry_with_tag("project", Some("my-project"));
        let query = TagQuery::parse("project *= project").unwrap();

        assert!(query.matches_entry(&entry));
      }

      #[test]
      fn it_matches_ends_with() {
        let entry = entry_with_tag("project", Some("my-project"));
        let query = TagQuery::parse("project $= project").unwrap();

        assert!(query.matches_entry(&entry));
      }

      #[test]
      fn it_matches_exact_with_wildcard() {
        let entry = entry_with_tag("project", Some("my-project"));
        let query = TagQuery::parse("project == my-*").unwrap();

        assert!(query.matches_entry(&entry));
      }

      #[test]
      fn it_matches_starts_with() {
        let entry = entry_with_tag("project", Some("my-project"));
        let query = TagQuery::parse("project ^= my").unwrap();

        assert!(query.matches_entry(&entry));
      }

      #[test]
      fn it_strips_quotes_from_value() {
        let entry = entry_with_tag("project", Some("my-project"));
        let query = TagQuery::parse(r#"project == "my-project""#).unwrap();

        assert!(query.matches_entry(&entry));
      }
    }

    mod tag_missing {
      use super::*;

      #[test]
      fn it_returns_false_for_missing_tag() {
        let entry = Entry::new(
          sample_date(),
          "No tags",
          Tags::new(),
          Note::new(),
          "Currently",
          None::<String>,
        );
        let query = TagQuery::parse("progress > 50").unwrap();

        assert!(!query.matches_entry(&entry));
      }
    }

    mod text_property {
      use super::*;

      #[test]
      fn it_searches_title_and_note() {
        let entry = finished_entry();
        let query = TagQuery::parse("text *= Finished").unwrap();

        assert!(query.matches_entry(&entry));
      }

      #[test]
      fn it_searches_note_content() {
        let entry = finished_entry();
        let query = TagQuery::parse("text *= notes").unwrap();

        assert!(query.matches_entry(&entry));
      }
    }

    mod time_property {
      use chrono::TimeZone;

      use super::*;

      #[test]
      fn it_compares_time_of_day() {
        let entry = Entry::new(
          Local.with_ymd_and_hms(2024, 3, 17, 10, 0, 0).unwrap(),
          "Morning task",
          Tags::new(),
          Note::new(),
          "Currently",
          None::<String>,
        );
        let query = TagQuery::parse("time < 2024-03-17 12:00").unwrap();

        assert!(query.matches_entry(&entry));
      }

      #[test]
      fn it_returns_false_for_contains_operator() {
        let entry = Entry::new(
          Local.with_ymd_and_hms(2024, 3, 17, 10, 0, 0).unwrap(),
          "Morning task",
          Tags::new(),
          Note::new(),
          "Currently",
          None::<String>,
        );
        let query = TagQuery::parse("time *= 10").unwrap();

        assert!(!query.matches_entry(&entry));
      }
    }

    mod title_property {
      use super::*;

      #[test]
      fn it_matches_title_contains() {
        let entry = finished_entry();
        let query = TagQuery::parse("title *= Finished").unwrap();

        assert!(query.matches_entry(&entry));
      }

      #[test]
      fn it_matches_title_starts_with() {
        let entry = finished_entry();
        let query = TagQuery::parse("title ^= Fin").unwrap();

        assert!(query.matches_entry(&entry));
      }
    }
  }

  mod parse {
    use pretty_assertions::assert_eq;

    use super::*;

    #[test]
    fn it_parses_basic_query() {
      let query = TagQuery::parse("progress > 50").unwrap();

      assert_eq!(query.property, Property::Tag("progress".into()));
      assert_eq!(query.op, ComparisonOp::GreaterThan);
      assert_eq!(query.value, "50");
      assert!(!query.negated);
    }

    #[test]
    fn it_parses_contains_operator() {
      let query = TagQuery::parse("title *= text").unwrap();

      assert_eq!(query.property, Property::Title);
      assert_eq!(query.op, ComparisonOp::Contains);
    }

    #[test]
    fn it_parses_ends_with_operator() {
      let query = TagQuery::parse("title $= suffix").unwrap();

      assert_eq!(query.property, Property::Title);
      assert_eq!(query.op, ComparisonOp::EndsWith);
    }

    #[test]
    fn it_parses_equal_operator() {
      let query = TagQuery::parse("status == active").unwrap();

      assert_eq!(query.op, ComparisonOp::Equal);
    }

    #[test]
    fn it_parses_greater_than_or_equal() {
      let query = TagQuery::parse("progress >= 75").unwrap();

      assert_eq!(query.op, ComparisonOp::GreaterThanOrEqual);
    }

    #[test]
    fn it_parses_less_than_or_equal() {
      let query = TagQuery::parse("progress <= 100").unwrap();

      assert_eq!(query.op, ComparisonOp::LessThanOrEqual);
    }

    #[test]
    fn it_parses_negated_query() {
      let query = TagQuery::parse("!status == active").unwrap();

      assert!(query.negated);
    }

    #[test]
    fn it_parses_not_equal_operator() {
      let query = TagQuery::parse("status != blocked").unwrap();

      assert_eq!(query.op, ComparisonOp::Equal);
      assert!(query.negated);
    }

    #[test]
    fn it_parses_single_equal_sign() {
      let query = TagQuery::parse("status = active").unwrap();

      assert_eq!(query.op, ComparisonOp::Equal);
    }

    #[test]
    fn it_parses_starts_with_operator() {
      let query = TagQuery::parse("title ^= prefix").unwrap();

      assert_eq!(query.property, Property::Title);
      assert_eq!(query.op, ComparisonOp::StartsWith);
    }

    #[test]
    fn it_parses_with_at_prefix() {
      let query = TagQuery::parse("@done > yesterday").unwrap();

      assert_eq!(query.property, Property::Tag("done".into()));
    }

    #[test]
    fn it_returns_none_for_invalid_input() {
      assert!(TagQuery::parse("not a query").is_none());
      assert!(TagQuery::parse("").is_none());
    }
  }

  mod parse_numeric {
    use pretty_assertions::assert_eq;

    use super::super::parse_numeric;

    #[test]
    fn it_parses_float() {
      assert_eq!(parse_numeric("3.14"), Some(3.14));
    }

    #[test]
    fn it_parses_integer() {
      assert_eq!(parse_numeric("42"), Some(42.0));
    }

    #[test]
    fn it_returns_none_for_non_numeric() {
      assert!(parse_numeric("abc").is_none());
    }

    #[test]
    fn it_strips_percentage() {
      assert_eq!(parse_numeric("75%"), Some(75.0));
    }
  }

  mod parse_operator {
    use pretty_assertions::assert_eq;

    use super::{super::parse_operator, *};

    #[test]
    fn it_parses_all_operators() {
      assert_eq!(parse_operator("<"), Some((ComparisonOp::LessThan, false)));
      assert_eq!(parse_operator("<="), Some((ComparisonOp::LessThanOrEqual, false)));
      assert_eq!(parse_operator(">"), Some((ComparisonOp::GreaterThan, false)));
      assert_eq!(parse_operator(">="), Some((ComparisonOp::GreaterThanOrEqual, false)));
      assert_eq!(parse_operator("="), Some((ComparisonOp::Equal, false)));
      assert_eq!(parse_operator("=="), Some((ComparisonOp::Equal, false)));
      assert_eq!(parse_operator("!="), Some((ComparisonOp::Equal, true)));
      assert_eq!(parse_operator("*="), Some((ComparisonOp::Contains, false)));
      assert_eq!(parse_operator("^="), Some((ComparisonOp::StartsWith, false)));
      assert_eq!(parse_operator("$="), Some((ComparisonOp::EndsWith, false)));
    }

    #[test]
    fn it_returns_none_for_invalid() {
      assert!(parse_operator("??").is_none());
    }
  }

  mod parse_property {
    use pretty_assertions::assert_eq;

    use super::{super::parse_property, *};

    #[test]
    fn it_parses_virtual_properties() {
      assert_eq!(parse_property("date"), Property::Date);
      assert_eq!(parse_property("duration"), Property::Duration);
      assert_eq!(parse_property("elapsed"), Property::Duration);
      assert_eq!(parse_property("interval"), Property::Interval);
      assert_eq!(parse_property("note"), Property::Note);
      assert_eq!(parse_property("text"), Property::Text);
      assert_eq!(parse_property("time"), Property::Time);
      assert_eq!(parse_property("title"), Property::Title);
    }

    #[test]
    fn it_parses_virtual_properties_case_insensitively() {
      assert_eq!(parse_property("Date"), Property::Date);
      assert_eq!(parse_property("TITLE"), Property::Title);
    }

    #[test]
    fn it_treats_unknown_as_tag() {
      assert_eq!(parse_property("project"), Property::Tag("project".into()));
      assert_eq!(parse_property("custom"), Property::Tag("custom".into()));
    }
  }

  mod strip_quotes {
    use pretty_assertions::assert_eq;

    use super::super::strip_quotes;

    #[test]
    fn it_returns_unquoted_string_unchanged() {
      assert_eq!(strip_quotes("hello"), "hello");
    }

    #[test]
    fn it_strips_surrounding_double_quotes() {
      assert_eq!(strip_quotes("\"hello\""), "hello");
    }
  }

  mod wildcard_match {
    use super::super::wildcard_match;

    #[test]
    fn it_matches_exact_string() {
      assert!(wildcard_match("hello", "hello"));
      assert!(!wildcard_match("hello", "world"));
    }

    #[test]
    fn it_matches_question_mark_wildcard() {
      assert!(wildcard_match("hello", "hell?"));
      assert!(!wildcard_match("hello", "hel?"));
    }

    #[test]
    fn it_matches_star_wildcard() {
      assert!(wildcard_match("my-project", "my-*"));
      assert!(wildcard_match("my-project", "*project"));
      assert!(wildcard_match("my-project", "*"));
    }
  }
}