aboutsummaryrefslogtreecommitdiff
path: root/karld/src/condition.rs
diff options
context:
space:
mode:
Diffstat (limited to 'karld/src/condition.rs')
-rw-r--r--karld/src/condition.rs36
1 files changed, 34 insertions, 2 deletions
diff --git a/karld/src/condition.rs b/karld/src/condition.rs
index a989a25..db1cf1d 100644
--- a/karld/src/condition.rs
+++ b/karld/src/condition.rs
@@ -26,6 +26,29 @@ pub enum Direction {
pub trait ConditionFind {
fn find(&self, edge: Edge, dir: Direction, from: NaiveDateTime) -> Option<NaiveDateTime>;
+
+ fn find_inverse_inclusive(
+ &self,
+ edge: Edge,
+ dir: Direction,
+ from: NaiveDateTime,
+ ) -> Option<NaiveDateTime> {
+ let s = self.find(edge, dir, from);
+ let e = self.find(edge.invert(), dir, from);
+ // we are "inside"
+ match (s, e) {
+ (Some(s), Some(e)) => {
+ if s > e {
+ self.find(edge, dir.invert(), from)
+ } else {
+ Some(s)
+ }
+ }
+ (None, Some(_)) => self.find(edge, dir.invert(), from),
+ (Some(s), None) => Some(s),
+ (None, None) => None,
+ }
+ }
}
impl ConditionFind for Condition {
@@ -35,7 +58,7 @@ impl ConditionFind for Condition {
// TODO improve efficiency for backward search
let last_start = cs
.iter()
- .map(|c| c.find(Start, dir, from))
+ .map(|c| c.find_inverse_inclusive(Start, dir, from))
.reduce(|a, b| Some(max(a?, b?)))?;
let first_end = cs
.iter()
@@ -50,7 +73,7 @@ impl ConditionFind for Condition {
End => end,
});
} else {
- from = start - Duration::seconds(10); // TODO proper fix
+ from = start - Duration::seconds(1); // TODO proper fix
}
}
None => break Some(start),
@@ -242,6 +265,15 @@ impl Edge {
}
}
+impl Direction {
+ pub fn invert(self) -> Self {
+ match self {
+ Direction::Forward => Direction::Backward,
+ Direction::Backward => Direction::Forward,
+ }
+ }
+}
+
#[cfg(test)]
mod test {
use super::{Condition, ConditionFind, Direction, Edge, Property};