summaryrefslogtreecommitdiffstats
path: root/src/bucketable.rs
blob: e0f743a5b2220017ff02a757bcc2ef93c4bf6612 (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
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
use chrono::{Local, Date, Datelike, Duration};
use std::collections::HashMap;
use std::{hash, cmp};

use icalwrap::{IcalVEvent, IcalVCalendar};
use utils::misc;

pub trait Bucketable {
  fn get_buckets(&self) -> Result<HashMap<String, Vec<String>>, String>;

  fn buckets_for_interval(mut start: Date<Local>, end: Date<Local>) -> Vec<String> {
    let mut buckets = Vec::new();

    while start.iso_week() <= end.iso_week() {
      let bucket = misc::get_bucket_for_date(start);
      buckets.push(bucket);
      start = start.checked_add_signed(Duration::days(7)).unwrap();
    }
    buckets
  }
}

impl Bucketable for IcalVEvent {
  fn get_buckets(&self) -> Result<HashMap<String, Vec<String>>, String> {
    let mut result:  HashMap<String, Vec<String>> = HashMap::new();

    let start_date = self.get_dtstart_date().ok_or_else(|| format!("Invalid DTSTART in {}", self.get_uid()))?;
    let mut end_date = self.get_dtend_date().unwrap_or(start_date);

    // end-dtimes are non-inclusive
    // so in case of date-only events, the last day of the event is dtend-1
    if self.is_allday() {
      end_date = end_date.pred();
    }

    let buckets = Self::buckets_for_interval(start_date, end_date);
    for bucketid in buckets {
      result
        .entry(bucketid)
        .and_modify(|items| items.push(self.get_khaleesi_line().unwrap()))
        .or_insert_with(|| vec!(self.get_khaleesi_line().unwrap()));
    }

    if self.has_recur() {
      for instance in self.get_recur_instances() {
        let recur_buckets = instance.get_buckets()?;
        result.merge(recur_buckets)
      }
    }

    for vec in result.values_mut() {
      vec.dedup()
    }
    Ok(result)
  }
}

impl Bucketable for IcalVCalendar {
  fn get_buckets(&self) -> Result<HashMap<String, Vec<String>>, String> {
    let mut result:  HashMap<String, Vec<String>> = HashMap::new();
    for event in self.events_iter() {
      let recur_buckets = event.get_buckets()?;
      result.merge(recur_buckets);
    }
    Ok(result)
  }
}

pub trait Merge<K>
where K: cmp::Eq + hash::Hash
{
  fn merge(&mut self, other: HashMap<K, Vec<String>>);
}

impl<K, S> Merge<K> for HashMap<K, Vec<String>, S>
where K: cmp::Eq + hash::Hash,
  S: std::hash::BuildHasher
{
  fn merge(&mut self, other: HashMap<K, Vec<String>>) {
    for (key, mut lines) in other.into_iter() {
      self
        .entry(key)
        .and_modify(|items| items.append(&mut lines))
        .or_insert(lines);
    }
  }
}

#[test]
fn merge_test() {
  let mut map_a: HashMap<&str, Vec<String>> = HashMap::new();
  let mut map_b: HashMap<&str, Vec<String>> = HashMap::new();

  let key = "key";
  map_a.insert(&key, vec!["a".to_string(), "b".to_string()]);
  map_b.insert(&key, vec!["c".to_string(), "d".to_string()]);

  map_a.merge(map_b);
  assert_eq!(map_a.get(&key).unwrap(), &vec!["a".to_string(), "b".to_string(), "c".to_string(), "d".to_string()]);
}

#[test]
fn buckets_multi_day_allday() {
  use testdata;
  use std::path::PathBuf;

  let path = PathBuf::from("test/path");
  let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_MULTIDAY_ALLDAY, Some(&path)).unwrap();

  let event_buckets = cal.get_principal_event().get_buckets().unwrap();

  assert_eq!(2, event_buckets.len());

  let mut bucket_names = event_buckets.keys().collect::<Vec<&String>>();
  bucket_names.sort_unstable();
  assert_eq!(vec!("2007-W26", "2007-W27"), bucket_names);

  let cal_buckets = cal.get_buckets().unwrap();
  assert_eq!(event_buckets, cal_buckets);
}

#[test]
fn buckets_single_event() {
  use testdata;
  use std::path::PathBuf;

  let path = PathBuf::from("test/path");
  let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_ONE_MEETING, Some(&path)).unwrap();

  let comp_buckets = cal.get_buckets().unwrap();
  assert_eq!(vec!("1997-W13"), comp_buckets.keys().collect::<Vec<&String>>());
}

#[test]
fn buckets_simple_recurring_event() {
  use testdata;
  use std::path::PathBuf;

  let path = PathBuf::from("test/path");
  let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_RECUR, Some(&path)).unwrap();

  let event = cal.get_principal_event();
  let event_buckets = event.get_buckets().unwrap();
  let cal_buckets = cal.get_buckets().unwrap();
  assert_eq!(event_buckets, cal_buckets);
  let mut cal_bucket_names = cal_buckets.keys().collect::<Vec<&String>>();
  cal_bucket_names.sort_unstable();
  assert_eq!(vec!("2018-W41", "2018-W42", "2018-W43", "2018-W44", "2018-W45", "2018-W46", "2018-W47", "2018-W48", "2018-W49", "2018-W50"), cal_bucket_names);
}