From 6f6db73a126057514912c5e6b372ef225a1065b7 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Fri, 10 Jun 2022 10:59:47 +0200 Subject: moved code to another crate --- Cargo.lock | 12 +++++++++ Cargo.toml | 2 +- karlc/Cargo.toml | 5 ++-- karlcommon/Cargo.toml | 7 +++++ karlcommon/src/lib.rs | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++ karld/Cargo.toml | 2 ++ karld/src/condition.rs | 58 ++++++----------------------------------- karld/src/interface.rs | 2 +- karld/src/main.rs | 8 +----- karld/src/protocol.rs | 34 ------------------------ 10 files changed, 105 insertions(+), 95 deletions(-) create mode 100644 karlcommon/Cargo.toml create mode 100644 karlcommon/src/lib.rs delete mode 100644 karld/src/protocol.rs diff --git a/Cargo.lock b/Cargo.lock index 7cdf259..17bfef6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", diff --git a/Cargo.toml b/Cargo.toml index f392c53..f2246f1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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/karlcommon/src/lib.rs b/karlcommon/src/lib.rs new file mode 100644 index 0000000..809a036 --- /dev/null +++ b/karlcommon/src/lib.rs @@ -0,0 +1,70 @@ +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), +} + +#[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, + pub priority: f64, + + pub completed: Option, + pub scheduled: Option, + + pub occurence: Option, + pub deadline: Option, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum Condition { + From(Box), + + Or(Vec), + And(Vec), + Invert(Box), + + Equal { + prop: Property, + value: i64, + modulus: Option, + }, + Range { + prop: Property, + min: i64, + max: i64, + modulus: Option, + }, +} + +#[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,46 +1,10 @@ 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), - - Or(Vec), - And(Vec), - Invert(Box), - - Equal { - prop: Property, - value: i64, - modulus: Option, - }, - Range { - prop: Property, - min: i64, - max: i64, - modulus: Option, - }, -} - -#[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); impl Range { @@ -60,13 +24,12 @@ pub enum Direction { Backward, } -impl Condition { - pub fn find( - &self, - edge: Edge, - dir: Direction, - mut from: NaiveDateTime, - ) -> Option { +trait ConditionFind { + fn find(&self, edge: Edge, dir: Direction, from: NaiveDateTime) -> Option; +} + +impl ConditionFind for Condition { + fn find(&self, edge: Edge, dir: Direction, mut from: NaiveDateTime) -> Option { 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), -} - -#[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, - pub priority: f64, - - pub completed: Option, - pub scheduled: Option, - - pub occurence: Option, - pub deadline: Option, -} -- cgit v1.2.3-70-g09d2