diff options
author | mario <mario-krehl@gmx.de> | 2016-06-30 15:05:30 +0200 |
---|---|---|
committer | mario <mario-krehl@gmx.de> | 2016-06-30 15:05:30 +0200 |
commit | a552234d5906a83aab2291def005007515c35198 (patch) | |
tree | e196bf8500deedc3284da143d49bb7ba70cfc334 | |
parent | a771d00c5b69e141ec5a18e3c3e9d31071a4c5c0 (diff) |
impl import_tasks
-rw-r--r-- | src/import.rs | 26 |
1 files changed, 26 insertions, 0 deletions
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<Task> { }) } +/// Reads line by line and tries to parse a task-object per line. +pub fn import_tasks<BR: BufRead>(r : BR) -> Result<Vec<Task>> { + 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); +} + |