added error types for task string parsing

This commit is contained in:
Nikolas Schmidt-Voigt 2021-03-28 22:36:17 +02:00
parent e583c5b4cf
commit 7f366ee0d8
3 changed files with 81 additions and 6 deletions

56
Cargo.lock generated
View file

@ -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"

View file

@ -9,3 +9,4 @@ license = "GPL-3.0-or-later"
[dependencies]
chrono = "0.4"
clap = "~2.33"
thiserror = "1.0"

View file

@ -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<NaiveDateTime>,
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<Self, Self::Err> {
let parts : Vec<String> = 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 {
@ -240,4 +250,12 @@ mod tests {
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)));
}
}