From 7f366ee0d8ee7c5758ea6258b197b56d1a531d16 Mon Sep 17 00:00:00 2001 From: Nikolas Schmidt-Voigt Date: Sun, 28 Mar 2021 22:36:17 +0200 Subject: [PATCH] added error types for task string parsing --- Cargo.lock | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 3 ++- src/task.rs | 28 ++++++++++++++++++++++----- 3 files changed, 81 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a3937bf..be346a1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -32,6 +32,7 @@ version = "0.1.0" dependencies = [ "chrono", "clap", + "thiserror", ] [[package]] @@ -102,12 +103,41 @@ dependencies = [ "autocfg", ] +[[package]] +name = "proc-macro2" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" +dependencies = [ + "proc-macro2", +] + [[package]] name = "strsim" version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" +[[package]] +name = "syn" +version = "1.0.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3a1d708c221c5a612956ef9f75b37e454e88d1f7b899fbd3a18d4252012d663" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + [[package]] name = "textwrap" version = "0.11.0" @@ -117,6 +147,26 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "thiserror" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0f4a65597094d4483ddaed134f409b2cb7c1beccf25201a9f73c719254fa98e" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7765189610d8241a44529806d6fd1f2e0a08734313a35d5b3a556f92b381f3c0" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "time" version = "0.1.44" @@ -134,6 +184,12 @@ version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" +[[package]] +name = "unicode-xid" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" + [[package]] name = "vec_map" version = "0.8.2" diff --git a/Cargo.toml b/Cargo.toml index 38297b6..01bc833 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,4 +8,5 @@ license = "GPL-3.0-or-later" [dependencies] chrono = "0.4" -clap = "~2.33" \ No newline at end of file +clap = "~2.33" +thiserror = "1.0" \ No newline at end of file diff --git a/src/task.rs b/src/task.rs index 743283e..8c9eec3 100644 --- a/src/task.rs +++ b/src/task.rs @@ -1,6 +1,7 @@ use chrono::{NaiveDateTime, Local}; use std::fmt; use std::str::{FromStr, Chars}; +use thiserror::Error; use crate::project; use crate::conf; @@ -9,10 +10,19 @@ use crate::conf; pub struct Task { start : NaiveDateTime, end : Option, + project : project::Project, description : String } +#[derive(Error,Debug)] +pub enum TaskError { + #[error("could not parse date or time of task")] + DateTimeParseError, + #[error("could not parse task")] + GeneralParseError +} + impl Task { pub fn start(project : project::Project, description : String) -> Task { Task { @@ -46,7 +56,7 @@ fn escape_special_chars(s: &str) -> String { } impl FromStr for Task { - type Err = (); + type Err = TaskError; fn from_str(s: &str) -> Result { let parts : Vec = split_with_escaped_delimeter(s).collect(); @@ -54,7 +64,7 @@ impl FromStr for Task { println!("{:?}", parts); if parts.len() < 2 { - return Err(()); + return Err(TaskError::GeneralParseError); } let time_parts : Vec<&str> = parts[0].split(" - ").collect(); @@ -62,7 +72,7 @@ impl FromStr for Task { let starttime = match NaiveDateTime::parse_from_str(time_parts[0].trim(), conf::FORMAT_DATETIME) { Ok(t) => t, Err(_) => { - return Err(()) + return Err(TaskError::DateTimeParseError) } }; @@ -72,7 +82,7 @@ impl FromStr for Task { endtime = match NaiveDateTime::parse_from_str(time_parts[1].trim(), conf::FORMAT_DATETIME) { Ok(t) => Some(t), Err(_) => { - return Err(()) + return Err(TaskError::DateTimeParseError) } } } else { @@ -239,5 +249,13 @@ mod tests { assert_eq!(t.project.0, t2.project.0); assert_eq!(t.description, t2.description); } - + + #[test] + fn from_str_errors() { + let t = Task::from_str("2021 test project"); + assert!(matches!(t, Err(TaskError::GeneralParseError))); + + let t = Task::from_str("asb - 2021- | project"); + assert!(matches!(t, Err(TaskError::DateTimeParseError))); + } } \ No newline at end of file