summaryrefslogtreecommitdiffstats
path: root/src/selectors/cal.rs
blob: 4fe86d73bb9865a34e5a78f21cd740c59659b6cf (plain)
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
use super::*;

//use crate::icalwrap::IcalVEvent;
use crate::khevent::KhEvent;

pub struct CalendarFilter {
  cal_names: Vec<String>
}

impl SelectFilter for CalendarFilter {
  fn add_term(&mut self, it: &mut dyn Iterator<Item = &&str>) {
    let term = it.next().unwrap();
    self.cal_names.push(term.to_lowercase());
  }

  fn is_not_empty(&self) -> bool {
    !self.cal_names.is_empty()
  }

  fn includes(&self, event: &KhEvent) -> bool {
    event.get_path()
      .and_then(|path| path.parent())
      .map(|path| path.to_string_lossy().to_lowercase())
      .map(|path| self.cal_names.iter().any(|cal| path.contains(cal)) )
      .unwrap_or(false)
  }
}

impl Default for CalendarFilter {
  fn default() -> CalendarFilter {
    CalendarFilter { cal_names: Vec::new() }
  }
}

#[cfg(test)]
mod tests {
  use super::test::test_filter_event;
  use crate::testdata;
  use std::path::PathBuf;

  #[test]
  fn test_cal_first() {
    let path1 = PathBuf::from("test/cal1/event1.ics");
    let filtered = test_filter_event(&testdata::TEST_EVENT_MULTIDAY, Some(&path1), &["cal", "cal1", "cal", "cal2"]);
    assert!(filtered);
  }

  #[test]
  fn test_cal_second() {
    let path2 = PathBuf::from("test/cal2/event2.ics");
    let filtered = test_filter_event(&testdata::TEST_EVENT_MULTIDAY, Some(&path2), &["cal", "cal1", "cal", "cal2"]);
    assert!(filtered);
  }

  #[test]
  fn test_cal_negative() {
    let path3 = PathBuf::from("test/cal3/event3.ics");
    let filtered = test_filter_event(&testdata::TEST_EVENT_MULTIDAY, Some(&path3), &["cal", "cal1", "cal", "cal2"]);
    assert!(!filtered);
  }
}