diff --git a/src/resource_interval.rs b/src/resource_interval.rs index 9c1d4ff..2c0d049 100644 --- a/src/resource_interval.rs +++ b/src/resource_interval.rs @@ -13,7 +13,14 @@ impl ResourceInterval { ResourceInterval(HashMap::new()) } - pub fn insert(&mut self, other: ResourceInterval) { + pub fn insert(&mut self, resource: &Resource, intervals: &IntervalSet) { + self.0 + .entry(resource.clone()) + .or_insert(IntervalSet::new()) + .merge(intervals); + } + + pub fn union(&mut self, other: ResourceInterval) { for (res, is) in other.iter() { self.0 .entry(res.clone()) @@ -22,7 +29,7 @@ impl ResourceInterval { } } - pub fn remove(&mut self, other: ResourceInterval) { + pub fn subtract(&mut self, other: ResourceInterval) { for (res, is) in other.iter() { let avail = self.0.entry(res.clone()).or_insert(IntervalSet::new()); *avail = avail.difference(is); diff --git a/src/task.rs b/src/task.rs index 009d43c..33fbefa 100644 --- a/src/task.rs +++ b/src/task.rs @@ -68,7 +68,7 @@ impl TaskDefinition { requires: self.requires.clone(), schedule: schedule, - valid_over: IntervalSet::from(vec![Interval::new(start, actual_end)]), + valid_over: IntervalSet::from(Interval::new(start, actual_end)), timezone: self.timezone, } } @@ -293,5 +293,13 @@ mod tests { panic!("{:?}", e); } }; + + // Ensure that the intervals generated over the valid period + // exactly cover the valid period + let mut theoretical = ResourceInterval::new(); + theoretical.insert(&"resource_a".to_owned(), &task.valid_over); + theoretical.insert(&"resource_b".to_owned(), &task.valid_over); + let generated = IntervalSet::from(task.generate_intervals(&theoretical).unwrap()); + assert_eq!(task.valid_over, generated); } }