From 624aec5dd1298d1a57baf727b576a41f1beb9ac9 Mon Sep 17 00:00:00 2001 From: daladim Date: Thu, 25 Feb 2021 00:50:49 +0100 Subject: [PATCH] Task: doc and more methods --- Cargo.lock | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ src/task.rs | 43 ++++++++++++++++++++++-- 3 files changed, 136 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 81ed039..2a6c0a3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9,6 +9,17 @@ dependencies = [ "memchr", ] +[[package]] +name = "async-trait" +version = "0.1.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d3a45e77e34375a7923b1e8febb049bb011f064714a8e17a1a616fef01da13d" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "atty" version = "0.2.14" @@ -62,6 +73,20 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chrono" +version = "0.4.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73" +dependencies = [ + "libc", + "num-integer", + "num-traits", + "serde", + "time", + "winapi", +] + [[package]] name = "core-foundation" version = "0.9.1" @@ -409,13 +434,18 @@ dependencies = [ name = "my-tasks" version = "0.1.0" dependencies = [ + "async-trait", "bitflags", + "chrono", "env_logger", "log", "minidom", "reqwest", + "serde", + "serde_json", "tokio", "url", + "uuid", ] [[package]] @@ -445,6 +475,35 @@ dependencies = [ "winapi", ] +[[package]] +name = "num-integer" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" +dependencies = [ + "autocfg", +] + +[[package]] +name = "num_cpus" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3" +dependencies = [ + "hermit-abi", + "libc", +] + [[package]] name = "once_cell" version = "1.5.2" @@ -715,6 +774,20 @@ name = "serde" version = "1.0.123" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "92d5161132722baa40d802cc70b15262b98258453e85e5d1d365c757c73869ae" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.123" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9391c295d64fc0abb2c556bad848f33cb8296276b1ad2677d1ae1ace4f258f31" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] [[package]] name = "serde_json" @@ -799,6 +872,16 @@ dependencies = [ "once_cell", ] +[[package]] +name = "time" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "tinyvec" version = "1.1.1" @@ -825,6 +908,7 @@ dependencies = [ "libc", "memchr", "mio", + "num_cpus", "pin-project-lite", "tokio-macros", ] @@ -940,6 +1024,16 @@ dependencies = [ "idna", "matches", "percent-encoding", + "serde", +] + +[[package]] +name = "uuid" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7" +dependencies = [ + "getrandom", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index b19c11b..e513727 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,3 +17,5 @@ bitflags = "1.2" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" async-trait = "0.1" +chrono = { version = "0.4", features = ["serde"] } +uuid = { version = "0.8", features = ["v4"] } diff --git a/src/task.rs b/src/task.rs index d590ea8..a3d95ab 100644 --- a/src/task.rs +++ b/src/task.rs @@ -1,22 +1,59 @@ +use chrono::{Utc, DateTime}; + use serde::{Deserialize, Serialize}; -pub type TaskId = String; // This is an HTML "etag" +// TODO: turn into this one day +// pub type TaskId = String; // This is an HTML "etag" +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct TaskId { + content: String, +} +impl TaskId{ + pub fn new() -> Self { + let u = uuid::Uuid::new_v4().to_hyphenated().to_string(); + Self { content:u } + } +} /// A to-do task #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub struct Task { id: TaskId, name: String, + last_modified: DateTime, completed: bool, } impl Task { + /// Create a new Task + pub fn new(name: String, last_modified: DateTime) -> Self { + Self { + name, last_modified, + id: TaskId::new(), + completed: false + } + } + pub fn id(&self) -> &TaskId { &self.id } pub fn name(&self) -> &str { &self.name } pub fn completed(&self) -> bool { self.completed } + pub fn last_modified(&self) -> DateTime { self.last_modified } - pub fn set_completed(&mut self) { + fn update_last_modified(&mut self) { + self.last_modified = Utc::now(); + } + + /// Rename a task. + /// This updates its "last modified" field + pub fn set_name(&mut self, new_name: String) { + self.update_last_modified(); + self.name = new_name; + } + + pub fn set_completed(&mut self, new_value: bool) { // TODO: either require a reference to the DataSource, so that it is aware // or change a flag here, and the DataSource will be able to check the flags of all its content (but then the Calendar should only give a reference/Arc, not a clone) + self.update_last_modified(); + self.completed = new_value; } -} \ No newline at end of file +}