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 | |
parent | 3238f8517097745032e19b3e26f57f0465a00b28 (diff) | |
download | karlender-6f6db73a126057514912c5e6b372ef225a1065b7.tar karlender-6f6db73a126057514912c5e6b372ef225a1065b7.tar.bz2 karlender-6f6db73a126057514912c5e6b372ef225a1065b7.tar.zst |
moved code to another crate
-rw-r--r-- | Cargo.lock | 12 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | karlc/Cargo.toml | 5 | ||||
-rw-r--r-- | karlcommon/Cargo.toml | 7 | ||||
-rw-r--r-- | karlcommon/src/lib.rs (renamed from karld/src/protocol.rs) | 38 | ||||
-rw-r--r-- | karld/Cargo.toml | 2 | ||||
-rw-r--r-- | karld/src/condition.rs | 58 | ||||
-rw-r--r-- | karld/src/interface.rs | 2 | ||||
-rw-r--r-- | karld/src/main.rs | 8 |
9 files changed, 72 insertions, 62 deletions
@@ -110,6 +110,17 @@ checksum = "112c678d4050afce233f4f2852bb2eb519230b3cf12f33585275537d7e41578d" [[package]] name = "karlc" version = "0.1.0" +dependencies = [ + "karlcommon", + "serde", +] + +[[package]] +name = "karlcommon" +version = "0.1.0" +dependencies = [ + "serde", +] [[package]] name = "karld" @@ -119,6 +130,7 @@ dependencies = [ "chrono", "crossbeam-channel", "env_logger", + "karlcommon", "lazy_static", "log", "serde", @@ -1,2 +1,2 @@ [workspace] -members = ["karld","karlc"]
\ No newline at end of file +members = ["karld", "karlc", "karlcommon"] diff --git a/karlc/Cargo.toml b/karlc/Cargo.toml index 7e43bed..e45419c 100644 --- a/karlc/Cargo.toml +++ b/karlc/Cargo.toml @@ -3,6 +3,7 @@ name = "karlc" version = "0.1.0" edition = "2021" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] +karlcommon = { path = "../karlcommon" } + +serde = { version = "1.0.137", features = ["derive"] } diff --git a/karlcommon/Cargo.toml b/karlcommon/Cargo.toml new file mode 100644 index 0000000..6ce571e --- /dev/null +++ b/karlcommon/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "karlcommon" +version = "0.1.0" +edition = "2021" + +[dependencies] +serde = { version = "1.0.137", features = ["derive"] } diff --git a/karld/src/protocol.rs b/karlcommon/src/lib.rs index 40ab0b2..809a036 100644 --- a/karld/src/protocol.rs +++ b/karlcommon/src/lib.rs @@ -1,4 +1,3 @@ -use crate::condition::Condition; use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, Serialize, Deserialize)] @@ -32,3 +31,40 @@ pub struct Task { pub occurence: Option<Condition>, pub deadline: Option<Condition>, } + +#[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, +} diff --git a/karld/Cargo.toml b/karld/Cargo.toml index 4c2221d..0e4d712 100644 --- a/karld/Cargo.toml +++ b/karld/Cargo.toml @@ -4,6 +4,8 @@ version = "0.1.1" edition = "2021" [dependencies] +karlcommon = { path = "../karlcommon" } + serde = { version = "1.0.137", features = ["derive"] } anyhow = "1.0.57" log = "0.4.17" 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(); |