From a771d00c5b69e141ec5a18e3c3e9d31071a4c5c0 Mon Sep 17 00:00:00 2001 From: mario Date: Wed, 29 Jun 2016 16:31:06 +0200 Subject: impl import_task --- src/import.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/import.rs b/src/import.rs index be4760c..f6df79c 100644 --- a/src/import.rs +++ b/src/import.rs @@ -17,6 +17,14 @@ pub fn import(r: R) -> Result> { }) } +/// Import a single JSON-formatted Task +pub fn import_task(s : &str) -> Result { + serde_json::from_str(s) + .map_err(|e| { + TaskError::new(TaskErrorKind::ParserError, Some(Box::new(e))) + }) +} + #[test] fn test_one() { let s = r#" @@ -98,3 +106,27 @@ fn test_two() { let imported = imported.unwrap(); assert!(imported.len() == 3); } + +#[test] +fn test_one_single() { + use status::TaskStatus; + let s = r#" +{ + "id": 1, + "description": "some description", + "entry": "20150619T165438Z", + "modified": "20160327T164007Z", + "project": "someproject", + "status": "waiting", + "tags": ["some", "tags", "are", "here"], + "uuid": "8ca953d5-18b4-4eb9-bd56-18f2e5b752f0", + "wait": "20160508T164007Z", + "urgency": 0.583562 +} +"#; + let imported = import_task(&s); + assert!(imported.is_ok()); + let imported = imported.unwrap(); + assert!(imported.status() == &TaskStatus::Waiting); +} + -- cgit v1.2.3 From a552234d5906a83aab2291def005007515c35198 Mon Sep 17 00:00:00 2001 From: mario Date: Thu, 30 Jun 2016 15:05:30 +0200 Subject: impl import_tasks --- src/import.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/import.rs b/src/import.rs index f6df79c..fb3021b 100644 --- a/src/import.rs +++ b/src/import.rs @@ -1,5 +1,6 @@ //! Module containing the `import()` function +use std::io::BufRead; use std::io::Read; use serde_json; @@ -25,6 +26,16 @@ pub fn import_task(s : &str) -> Result { }) } +/// Reads line by line and tries to parse a task-object per line. +pub fn import_tasks(r : BR) -> Result> { + let vt = r.lines().filter_map(|l| l.ok()) + .filter(|s| s.len() > 0) + .map(|s| import_task(s.as_str())) + .filter_map(|t| t.ok()) + .collect(); + Ok(vt) +} + #[test] fn test_one() { let s = r#" @@ -130,3 +141,18 @@ fn test_one_single() { assert!(imported.status() == &TaskStatus::Waiting); } +#[test] +fn test_two_single() { + use std::io::BufReader; + use status::TaskStatus; + let s = r#" +{"id":1,"description":"some description","entry":"20150619T165438Z","modified":"20160327T164007Z","project":"someproject","status":"waiting","tags":["some","tags","are","here"],"uuid":"8ca953d5-18b4-4eb9-bd56-18f2e5b752f0","wait":"20160508T164007Z","urgency":0.583562} +{"id":1,"description":"some description","entry":"20150619T165438Z","modified":"20160327T164007Z","project":"someproject","status":"waiting","tags":["some","tags","are","here"],"uuid":"8ca953d5-18b4-4eb9-bd56-18f2e5b752f0","wait":"20160508T164007Z","urgency":0.583562}"#; + let imported = import_tasks(BufReader::new(s.as_bytes())); + assert!(imported.is_ok()); + let imported = imported.unwrap(); + assert!(imported.len() == 2); + assert!(imported[0].status() == &TaskStatus::Waiting); + assert!(imported[1].status() == &TaskStatus::Waiting); +} + -- cgit v1.2.3 From e8d2a54607dd9c31327b4cb22288a78f3e7f3c31 Mon Sep 17 00:00:00 2001 From: mario Date: Mon, 4 Jul 2016 15:13:17 +0200 Subject: changed import_tasks to return a Vec of Results: Vec> --- src/import.rs | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/src/import.rs b/src/import.rs index fb3021b..255effc 100644 --- a/src/import.rs +++ b/src/import.rs @@ -27,13 +27,21 @@ pub fn import_task(s : &str) -> Result { } /// Reads line by line and tries to parse a task-object per line. -pub fn import_tasks(r : BR) -> Result> { - let vt = r.lines().filter_map(|l| l.ok()) - .filter(|s| s.len() > 0) - .map(|s| import_task(s.as_str())) - .filter_map(|t| t.ok()) - .collect(); - Ok(vt) +pub fn import_tasks(r : BR) -> Vec> { + let mut vt = Vec::new(); + for line in r.lines() { + if line.is_err() { + vt.push(Err(TaskError::new(TaskErrorKind::ReaderError, Some(Box::new(line.unwrap_err()))))); + continue; + } + // Unwrap is safe because of continue above + if line.as_ref().unwrap().len() <= 0 { + // Empty strings are not usable, and shall be silently ignored + continue; + } + vt.push(import_task(line.unwrap().as_str())); + } + vt } #[test] @@ -149,10 +157,12 @@ fn test_two_single() { {"id":1,"description":"some description","entry":"20150619T165438Z","modified":"20160327T164007Z","project":"someproject","status":"waiting","tags":["some","tags","are","here"],"uuid":"8ca953d5-18b4-4eb9-bd56-18f2e5b752f0","wait":"20160508T164007Z","urgency":0.583562} {"id":1,"description":"some description","entry":"20150619T165438Z","modified":"20160327T164007Z","project":"someproject","status":"waiting","tags":["some","tags","are","here"],"uuid":"8ca953d5-18b4-4eb9-bd56-18f2e5b752f0","wait":"20160508T164007Z","urgency":0.583562}"#; let imported = import_tasks(BufReader::new(s.as_bytes())); - assert!(imported.is_ok()); - let imported = imported.unwrap(); assert!(imported.len() == 2); - assert!(imported[0].status() == &TaskStatus::Waiting); - assert!(imported[1].status() == &TaskStatus::Waiting); + assert!(imported[0].is_ok()); + assert!(imported[1].is_ok()); + let import0 = imported[0].as_ref().unwrap(); + let import1 = imported[1].as_ref().unwrap(); + assert!(import0.status() == &TaskStatus::Waiting); + assert!(import1.status() == &TaskStatus::Waiting); } -- cgit v1.2.3 From 7f11757d514bbc322a8671df9dcebe45a3861e74 Mon Sep 17 00:00:00 2001 From: mario Date: Mon, 4 Jul 2016 15:14:09 +0200 Subject: added TaskErrorKind::ReaderError for Error Handling in import_tasks() --- src/error.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/error.rs b/src/error.rs index 78eaddc..c2aac38 100644 --- a/src/error.rs +++ b/src/error.rs @@ -15,12 +15,16 @@ pub enum TaskErrorKind { /// Error kind indicating that the Status of a task is missing NoStatus, + + /// Error kind indicating that the Reader failed to read something + ReaderError, } fn store_error_type_as_str(e: &TaskErrorKind) -> &'static str { match e { &TaskErrorKind::ParserError => "Parser Error", &TaskErrorKind::NoStatus => "Task status is missing", + &TaskErrorKind::ReaderError => "Reader Error", } } -- cgit v1.2.3 From a363ce60df7efc1302109a8d3dd3fffe2fa84cc5 Mon Sep 17 00:00:00 2001 From: mario Date: Mon, 4 Jul 2016 15:38:03 +0200 Subject: expanded the test for a single task from a string. import_tasks uses the tested function import_task, so we should only need to check if the Vector has the correct length and the items are ok. --- src/import.rs | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/import.rs b/src/import.rs index 255effc..c921830 100644 --- a/src/import.rs +++ b/src/import.rs @@ -129,6 +129,14 @@ fn test_two() { #[test] fn test_one_single() { use status::TaskStatus; + use date::Date; + use date::TASKWARRIOR_DATETIME_TEMPLATE; + use uuid::Uuid; + use chrono::naive::datetime::NaiveDateTime; + fn mkdate(s: &str) -> Date { + let n = NaiveDateTime::parse_from_str(s, TASKWARRIOR_DATETIME_TEMPLATE); + Date::from(n.unwrap()) + } let s = r#" { "id": 1, @@ -145,8 +153,26 @@ fn test_one_single() { "#; let imported = import_task(&s); assert!(imported.is_ok()); - let imported = imported.unwrap(); - assert!(imported.status() == &TaskStatus::Waiting); + + // Check for every information + let task = imported.unwrap(); + assert!(task.status() == &TaskStatus::Waiting); + assert!(task.description() == "some description"); + assert!(task.entry().clone() == mkdate("20150619T165438Z")); + assert!(task.uuid().clone() == Uuid::parse_str("8ca953d5-18b4-4eb9-bd56-18f2e5b752f0").unwrap()); + assert!(task.modified() == Some(&mkdate("20160327T164007Z"))); + assert!(task.project() == Some(&String::from("someproject"))); + if let Some(tags) = task.tags() { + for tag in tags { + let any_tag = [ "some", "tags", "are", "here", ] + .into_iter().any(|t| tag == *t); + assert!(any_tag, "Tag {} missing", tag); + } + } else { + assert!(false, "Tags completely missing"); + } + + assert!(task.wait() == Some(&mkdate("20160508T164007Z"))); } #[test] -- cgit v1.2.3