diff options
author | metamuffin <yvchraiqi@protonmail.com> | 2022-06-07 11:42:46 +0200 |
---|---|---|
committer | metamuffin <yvchraiqi@protonmail.com> | 2022-06-07 11:42:46 +0200 |
commit | 20030e38a921d2ba00ba7b67d0e466aafd591b4d (patch) | |
tree | a7d50b0882b724c3fc0611f671127823a1cd35ff /src | |
parent | bce701919dad8fb3ce0e06ada4d4892240d483e2 (diff) | |
download | karlender-20030e38a921d2ba00ba7b67d0e466aafd591b4d.tar karlender-20030e38a921d2ba00ba7b67d0e466aafd591b4d.tar.bz2 karlender-20030e38a921d2ba00ba7b67d0e466aafd591b4d.tar.zst |
a
Diffstat (limited to 'src')
-rw-r--r-- | src/condition.rs | 156 | ||||
-rw-r--r-- | src/lib.rs | 2 | ||||
-rw-r--r-- | src/main.rs | 3 | ||||
-rw-r--r-- | src/occurence.rs | 25 | ||||
-rw-r--r-- | src/protocol.rs | 2 |
5 files changed, 159 insertions, 29 deletions
diff --git a/src/condition.rs b/src/condition.rs new file mode 100644 index 0000000..7a873a8 --- /dev/null +++ b/src/condition.rs @@ -0,0 +1,156 @@ +use std::cmp::{max, min}; + +use chrono::{Datelike, NaiveDate, NaiveDateTime, NaiveTime}; +use serde::{Deserialize, Serialize}; +use Direction::*; +use Edge::*; + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub enum Condition { + From(Box<Condition>), + + Or(Vec<Condition>), + And(Vec<Condition>), + Invert(Box<Condition>), + + Equal { + prop: Property, + value: i32, + modulus: Option<i32>, + }, + Range { + prop: Property, + min: i32, + max: i32, + modulus: Option<i32>, + }, +} + +#[derive(Debug, Clone, Copy, Serialize, Deserialize)] +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 { + a > self.0 && a < self.1 + } +} + +#[derive(Debug, Clone, Copy)] +pub enum Edge { + Start, + End, +} +#[derive(Debug, Clone, Copy)] +pub enum Direction { + Forward, + Backward, +} + +impl Condition { + pub fn find(&self, edge: Edge, dir: Direction, from: NaiveDateTime) -> Option<NaiveDateTime> { + match self { + Condition::And(_) => todo!(), + Condition::Or(cs) => { + cs.iter() + .filter_map(|c| c.find(edge, dir, from)) + .reduce(match dir { + Forward => min, + Backward => max, + }) + } + Condition::From(c) => todo!(), + Condition::Invert(c) => c.find(edge.invert(), dir, from), + Condition::Equal { + prop, + value, + modulus, + } => { + let geq = match dir { + Forward => |a: i32, b: i32| a >= b, + Backward => |a: i32, b: i32| a < b, + }; + let off = match edge { + Start => 0, + End => 1, + }; + match prop { + Property::Year => { + if geq(from.year(), *value + off) { + None + } else { + Some(NaiveDateTime::new( + NaiveDate::from_ymd(*value + off, 1, 1), + NaiveTime::from_hms(0, 0, 0), + )) + } + } + Property::Monthofyear => todo!(), + Property::Weekofmonth => todo!(), + Property::Dayofyear => todo!(), + Property::Dayofmonth => todo!(), + Property::Dayofweek => todo!(), + Property::Hour => todo!(), + Property::Minute => todo!(), + Property::Second => todo!(), + Property::Unix => todo!(), + } + } + Condition::Range { + prop, + min, + max, + modulus, + } => todo!(), + } + } +} + +impl Edge { + pub fn invert(self) -> Self { + match self { + Edge::Start => Edge::End, + Edge::End => Edge::Start, + } + } +} + +#[cfg(test)] +mod test { + use super::{Condition, Direction, Edge, Property}; + use chrono::NaiveDateTime; + use std::str::FromStr; + + #[test] + fn a() { + let cond = Condition::Equal { + modulus: None, + prop: Property::Year, + value: 2023, + }; + + let dt = NaiveDateTime::from_str("2022-06-07T13:37:48").unwrap(); + assert_eq!( + cond.find(Edge::Start, Direction::Forward, dt).unwrap(), + NaiveDateTime::from_str("2023-01-01T00:00:00").unwrap(), + ); + assert_eq!( + cond.find(Edge::End, Direction::Forward, dt).unwrap(), + NaiveDateTime::from_str("2024-01-01T00:00:00").unwrap(), + ); + assert_eq!(cond.find(Edge::End, Direction::Backward, dt), None); + assert_eq!(cond.find(Edge::Start, Direction::Backward, dt), None); + } +} @@ -1,3 +1,3 @@ pub mod protocol; -pub mod occurence; +pub mod condition; pub mod interface; diff --git a/src/main.rs b/src/main.rs index fb975d6..7a36cb1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,9 @@ -use std::time::Duration; - use calender_thing::{ interface::network_loop, protocol::{ClientboundPacket, ServerboundPacket}, }; use crossbeam_channel::{Receiver, Sender}; +use std::time::Duration; fn main() { let (s, r) = crossbeam_channel::unbounded(); diff --git a/src/occurence.rs b/src/occurence.rs deleted file mode 100644 index 104438a..0000000 --- a/src/occurence.rs +++ /dev/null @@ -1,25 +0,0 @@ -use serde::{Deserialize, Serialize}; - -#[derive(Debug, Serialize, Deserialize)] -pub enum Condition { - And(Vec<Condition>), - Or(Vec<Condition>), - From(Box<Condition>), - To(Box<Condition>), - - Year(Range), - Monthofyear(Range), - Weekofmonth(Range), - Dayofyear(Range), - Dayofmonth(Range), - Dayofweek(Range), - - Hour(Range), - Minute(Range), - Second(Range), - - Unix(Range), -} - -#[derive(Debug, Serialize, Deserialize)] -pub struct Range(u64, u64); diff --git a/src/protocol.rs b/src/protocol.rs index a09f879..e7527eb 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Serialize}; -use crate::occurence::Condition; +use crate::condition::Condition; #[derive(Debug, Serialize, Deserialize)] #[serde(tag = "type", content = "data", rename_all = "snake_case")] |