aboutsummaryrefslogtreecommitdiff
path: root/karld/src/condition.rs
diff options
context:
space:
mode:
authormetamuffin <yvchraiqi@protonmail.com>2022-06-10 10:59:47 +0200
committermetamuffin <yvchraiqi@protonmail.com>2022-06-10 10:59:47 +0200
commit6f6db73a126057514912c5e6b372ef225a1065b7 (patch)
tree22518de3c0213d85166aa53f0fa476a6d8e2d780 /karld/src/condition.rs
parent3238f8517097745032e19b3e26f57f0465a00b28 (diff)
downloadkarlender-6f6db73a126057514912c5e6b372ef225a1065b7.tar
karlender-6f6db73a126057514912c5e6b372ef225a1065b7.tar.bz2
karlender-6f6db73a126057514912c5e6b372ef225a1065b7.tar.zst
moved code to another crate
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));