diff --git a/pttodoest/Cargo.lock b/pttodoest/Cargo.lock index 4ace4f7..9ce4be4 100755 --- a/pttodoest/Cargo.lock +++ b/pttodoest/Cargo.lock @@ -2,6 +2,24 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "aho-corasick" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301" +dependencies = [ + "memchr", +] + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + [[package]] name = "anstream" version = "0.6.21" @@ -52,12 +70,53 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "autocfg" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" + [[package]] name = "bitflags" version = "2.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3" +[[package]] +name = "bumpalo" +version = "3.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46c5e41b57b8bba42a04676d81cb89e9ee8e859a1a66f80a5a72e1cb76b34d43" + +[[package]] +name = "cc" +version = "1.2.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35900b6c8d709fb1d854671ae27aeaa9eec2f8b01b364e1619a40da3e6fe2afe" +dependencies = [ + "find-msvc-tools", + "shlex", +] + +[[package]] +name = "cfg-if" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" + +[[package]] +name = "chrono" +version = "0.4.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "145052bdd345b87320e369255277e3fb5152762ad123a901ef5c262dd38fe8d2" +dependencies = [ + "iana-time-zone", + "js-sys", + "num-traits", + "wasm-bindgen", + "windows-link", +] + [[package]] name = "clap" version = "4.5.51" @@ -104,6 +163,12 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + [[package]] name = "equivalent" version = "1.0.2" @@ -120,6 +185,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "find-msvc-tools" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52051878f80a721bb68ebfbc930e07b65ba72f2da88968ea5c06fd6ca3d3a127" + [[package]] name = "fuchsia-cprng" version = "0.1.1" @@ -148,6 +219,30 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" +[[package]] +name = "iana-time-zone" +version = "0.1.64" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33e57f83510bb73707521ebaffa789ec8caf86f9657cad665b092b581d40e9fb" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "log", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + [[package]] name = "indexmap" version = "2.12.0" @@ -170,6 +265,16 @@ version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" +[[package]] +name = "js-sys" +version = "0.3.82" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b011eec8cc36da2aab2d5cff675ec18454fad408585853910a202391cf9f8e65" +dependencies = [ + "once_cell", + "wasm-bindgen", +] + [[package]] name = "json-patch" version = "4.1.0" @@ -204,12 +309,33 @@ version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df1d3c3b53da64cf5760482273a98e575c651a67eec7f77df96b5b642de8f039" +[[package]] +name = "log" +version = "0.4.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34080505efa8e45a4b816c349525ebe327ceaa8559756f0356cba97ef3bf7432" + [[package]] name = "memchr" version = "2.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273" +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" + [[package]] name = "once_cell_polyfill" version = "1.70.2" @@ -229,10 +355,12 @@ dependencies = [ name = "pttodoest" version = "0.1.0" dependencies = [ + "chrono", "clap", "gethostname", "json-patch", "jsonptr", + "regex", "serde", "serde_json", "serde_yaml", @@ -285,6 +413,35 @@ dependencies = [ "rand_core 0.3.1", ] +[[package]] +name = "regex" +version = "1.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "843bc0191f75f3e22651ae5f1e72939ab2f72a4bc30fa80a066bd66edefc24d4" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5276caf25ac86c8d810222b3dbb938e512c55c6831a10f3e6ed1c93b84041f1c" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a2d987857b319362043e95f5353c0535c1f58eec5336fdfcf626430af7def58" + [[package]] name = "remove_dir_all" version = "0.5.3" @@ -307,6 +464,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "rustversion" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" + [[package]] name = "ryu" version = "1.0.20" @@ -369,6 +532,12 @@ dependencies = [ "unsafe-libyaml", ] +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + [[package]] name = "strsim" version = "0.11.1" @@ -434,6 +603,51 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +[[package]] +name = "wasm-bindgen" +version = "0.2.105" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da95793dfc411fbbd93f5be7715b0578ec61fe87cb1a42b12eb625caa5c5ea60" +dependencies = [ + "cfg-if", + "once_cell", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.105" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04264334509e04a7bf8690f2384ef5265f05143a4bff3889ab7a3269adab59c2" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.105" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "420bc339d9f322e562942d52e115d57e950d12d88983a14c79b86859ee6c7ebc" +dependencies = [ + "bumpalo", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.105" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76f218a38c84bcb33c25ec7059b07847d465ce0e0a76b995e134a45adcb6af76" +dependencies = [ + "unicode-ident", +] + [[package]] name = "winapi" version = "0.3.9" @@ -456,12 +670,65 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows-core" +version = "0.62.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb" +dependencies = [ + "windows-implement", + "windows-interface", + "windows-link", + "windows-result", + "windows-strings", +] + +[[package]] +name = "windows-implement" +version = "0.60.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "windows-interface" +version = "0.59.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "windows-link" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" +[[package]] +name = "windows-result" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-strings" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091" +dependencies = [ + "windows-link", +] + [[package]] name = "windows-sys" version = "0.60.2" diff --git a/pttodoest/Cargo.toml b/pttodoest/Cargo.toml index 9d21ef0..717855e 100755 --- a/pttodoest/Cargo.toml +++ b/pttodoest/Cargo.toml @@ -4,10 +4,12 @@ version = "0.1.0" edition = "2024" [dependencies] +chrono = "0.4.42" clap = { version = "4.5.51", features = ["derive"] } gethostname = "1.1.0" json-patch = "4.1.0" jsonptr = "0.7.1" +regex = "1.12.2" serde = { version = "1.0.228", features = ["serde_derive"] } serde_json = "1.0.145" serde_yaml = "0.9.34" diff --git a/pttodoest/src/main.rs b/pttodoest/src/main.rs index 486d7bb..eb3427a 100755 --- a/pttodoest/src/main.rs +++ b/pttodoest/src/main.rs @@ -502,6 +502,126 @@ impl Delta { #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] struct Task(serde_yaml::Value); +impl Task { + pub fn next_due(&self) -> Option { + match self.schedule() { + Some(schedule) => self.parse_schedule_next(schedule), + None => Some(1), + } + } + + fn parse_schedule_next(&self, schedule: String) -> Option { + let mut schedule = schedule; + + if regex::Regex::new(r"^[0-9]+h$").unwrap().is_match(&schedule) { + let hours = &schedule[..schedule.len() - 1]; + match hours.parse::() { + Ok(hours) => return Some(Delta::now_time() + hours * 60 * 60), + _ => {} + }; + } + + if regex::Regex::new(r"[0-9]{4}-[0-9]{2}-[0-9]{2}$") + .unwrap() + .is_match(&schedule) + { + schedule += "T00"; + } + + eprintln!("0 PARSING DATE HOUR {}", &schedule); + if regex::Regex::new(r"[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}$") + .unwrap() + .is_match(&schedule) + { + eprintln!("1 PARSING DATE HOUR {} like %Y-%m-%dT%H", &schedule); + let date = schedule.clone() + ":00:00"; + match chrono::NaiveDateTime::parse_from_str(&date, "%Y-%m-%dT%H:%M:%S") { + Ok(datehour) => { + eprintln!("2 PARSING DATE HOUR {}", &schedule); + let seconds = datehour.format("%s").to_string(); + match seconds.parse::() { + Ok(n) => return Some(n), + _ => {} + }; + } + Err(msg) => panic!("{}", msg), + }; + } + + None + } + + fn schedule(&self) -> Option { + match &self.0 { + serde_yaml::Value::Mapping(m) => match m.get("schedule".to_string()) { + Some(schedule) => match schedule { + serde_yaml::Value::String(s) => Some(s.clone()), + _ => None, + }, + _ => None, + }, + _ => None, + } + } +} + +#[cfg(test)] +mod test_task { + use super::*; + + #[test] + fn test_unscheduled() { + let task = Task(serde_yaml::Value::String("hello world".to_string())); + assert_eq!(None, task.schedule()); + assert_eq!(Some(1 as u64), task.next_due()); + } + + #[test] + fn test_scheduled_date_before() { + let mut m = serde_yaml::Mapping::new(); + m.insert("schedule".into(), "2006-01-02".into()); + let task = Task(serde_yaml::Value::Mapping(m)); + assert_eq!(Some("2006-01-02".to_string()), task.schedule()); + assert_eq!(Some(1136160000 as u64), task.next_due()); + } + + #[test] + fn test_scheduled_date_after() { + let mut m = serde_yaml::Mapping::new(); + m.insert("schedule".into(), "2036-01-02".into()); + let task = Task(serde_yaml::Value::Mapping(m)); + assert_eq!(Some("2036-01-02".to_string()), task.schedule()); + assert_eq!(Some(2082844800 as u64), task.next_due()); + } + + #[test] + fn test_scheduled_hour_after() { + let mut m = serde_yaml::Mapping::new(); + m.insert("schedule".into(), "2036-01-02T16".into()); + let task = Task(serde_yaml::Value::Mapping(m)); + assert_eq!(Some("2036-01-02T16".to_string()), task.schedule()); + assert_eq!(Some(2082902400 as u64), task.next_due()); + } + + #[test] + fn test_scheduled_duration() { + let mut m = serde_yaml::Mapping::new(); + m.insert("schedule".into(), "1h".into()); + let task = Task(serde_yaml::Value::Mapping(m)); + assert_eq!(Some("1h".to_string()), task.schedule()); + assert_eq!(Some(Delta::now_time() + 60 * 60 as u64), task.next_due()); + } + + #[test] + fn test_scheduled_cron() { + let mut m = serde_yaml::Mapping::new(); + m.insert("schedule".into(), "* * * * *".into()); + let task = Task(serde_yaml::Value::Mapping(m)); + assert_eq!(Some("* * * * *".to_string()), task.schedule()); + assert_eq!(Some((Delta::now_time()) + 60 % 60 as u64), task.next_due()); + } +} + #[derive(Debug, Clone)] struct Events(Vec);