diff options
author | metamuffin <yvchraiqi@protonmail.com> | 2022-06-10 10:59:47 +0200 |
---|---|---|
committer | metamuffin <yvchraiqi@protonmail.com> | 2022-06-10 10:59:47 +0200 |
commit | 6f6db73a126057514912c5e6b372ef225a1065b7 (patch) | |
tree | 22518de3c0213d85166aa53f0fa476a6d8e2d780 /karld/src | |
parent | 3238f8517097745032e19b3e26f57f0465a00b28 (diff) | |
download | karlender-6f6db73a126057514912c5e6b372ef225a1065b7.tar karlender-6f6db73a126057514912c5e6b372ef225a1065b7.tar.bz2 karlender-6f6db73a126057514912c5e6b372ef225a1065b7.tar.zst |
moved code to another crate
Diffstat (limited to 'karld/src')
-rw-r--r-- | karld/src/condition.rs | 58 | ||||
-rw-r--r-- | karld/src/interface.rs | 2 | ||||
-rw-r--r-- | karld/src/main.rs | 8 | ||||
-rw-r--r-- | karld/src/protocol.rs | 34 |
4 files changed, 10 insertions, 92 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)); diff --git a/karld/src/interface.rs b/karld/src/interface.rs index e3d2ba3..884aca2 100644 --- a/karld/src/interface.rs +++ b/karld/src/interface.rs @@ -1,5 +1,5 @@ -use super::protocol::{ClientboundPacket, ServerboundPacket}; use crate::handle_packet; +use karlcommon::{ClientboundPacket, ServerboundPacket}; use log::{debug, error, info, warn}; use std::io; use std::io::{BufRead, BufReader, ErrorKind, Write}; diff --git a/karld/src/main.rs b/karld/src/main.rs index ae49ad3..6cf902e 100644 --- a/karld/src/main.rs +++ b/karld/src/main.rs @@ -1,16 +1,10 @@ pub mod condition; pub mod interface; -pub mod protocol; use std::{collections::HashMap, sync::RwLock}; - -use crate::{ - condition::{Condition, Property}, - protocol::Task, -}; use crossbeam_channel::Sender; use interface::network_loop; -use protocol::{ClientboundPacket, ServerboundPacket}; +use karlcommon::{Task, Condition, Property, ServerboundPacket, ClientboundPacket}; fn main() { env_logger::init(); diff --git a/karld/src/protocol.rs b/karld/src/protocol.rs deleted file mode 100644 index 40ab0b2..0000000 --- a/karld/src/protocol.rs +++ /dev/null @@ -1,34 +0,0 @@ -use crate::condition::Condition; -use serde::{Deserialize, Serialize}; - -#[derive(Debug, Clone, Serialize, Deserialize)] -#[serde(tag = "type", content = "data", rename_all = "snake_case")] -pub enum ClientboundPacket { - Handshake { version: String }, - Error(String), - DownloadResponse(Vec<Task>), -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -#[serde(tag = "type", content = "data", rename_all = "snake_case")] -pub enum ServerboundPacket { - Download, - UpdateTask(Task), - RemoveTask(u64), -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct Task { - pub id: u64, - pub name: String, - pub description: String, - - pub tags: Vec<String>, - pub priority: f64, - - pub completed: Option<u64>, - pub scheduled: Option<u64>, - - pub occurence: Option<Condition>, - pub deadline: Option<Condition>, -} |