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.rs58
1 files changed, 8 insertions, 50 deletions
diff --git a/karld/src/condition.rs b/karld/src/condition.rs
index 830e8ae..5874e7b 100644
--- a/karld/src/condition.rs
+++ b/karld/src/condition.rs
@@ -1,47 +1,11 @@
use chrono::{Datelike, Duration, NaiveDate, NaiveDateTime, NaiveTime, Timelike};
+use karlcommon::{Condition, Property};
use serde::{Deserialize, Serialize};
use std::cmp::{max, min};
use Direction::*;
use Edge::*;
#[derive(Debug, Clone, Serialize, Deserialize)]
-#[serde(rename_all = "snake_case")]
-pub enum Condition {
- From(Box<Condition>),
-
- Or(Vec<Condition>),
- And(Vec<Condition>),
- Invert(Box<Condition>),
-
- Equal {
- prop: Property,
- value: i64,
- modulus: Option<i64>,
- },
- Range {
- prop: Property,
- min: i64,
- max: i64,
- modulus: Option<i64>,
- },
-}
-
-#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
-#[serde(rename_all = "snake_case")]
-pub enum Property {
- Year,
- Monthofyear,
- Weekofmonth,
- Dayofyear,
- Dayofmonth,
- Dayofweek,
- Hour,
- Minute,
- Second,
- Unix,
-}
-
-#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Range<T>(T, T);
impl<T: PartialOrd + PartialEq> Range<T> {
pub fn includes(&self, a: T) -> bool {
@@ -60,13 +24,12 @@ pub enum Direction {
Backward,
}
-impl Condition {
- pub fn find(
- &self,
- edge: Edge,
- dir: Direction,
- mut from: NaiveDateTime,
- ) -> Option<NaiveDateTime> {
+trait ConditionFind {
+ fn find(&self, edge: Edge, dir: Direction, from: NaiveDateTime) -> Option<NaiveDateTime>;
+}
+
+impl ConditionFind for Condition {
+ fn find(&self, edge: Edge, dir: Direction, mut from: NaiveDateTime) -> Option<NaiveDateTime> {
match self {
Condition::And(cs) => loop {
// TODO improve efficiency for backward search
@@ -281,7 +244,7 @@ impl Edge {
#[cfg(test)]
mod test {
- use super::{Condition, Direction, Edge, Property};
+ use super::{Condition, ConditionFind, Direction, Edge, Property};
use chrono::{NaiveDateTime, Utc};
use std::str::FromStr;
use Direction::*;
@@ -301,11 +264,6 @@ mod test {
value: 12,
},
]);
- // let cond = Condition::Equal {
- // modulus: None,
- // prop: Property::Hour,
- // value: 12,
- // };
let dt = Utc::now().naive_utc();
println!("START FORWARD => {:?}", cond.find(Start, Forward, dt));
println!("END FORWARD => {:?}", cond.find(End, Forward, dt));