diff options
author | Malte Brandy <malte.brandy@maralorn.de> | 2018-04-21 03:13:48 +0200 |
---|---|---|
committer | Malte Brandy <malte.brandy@maralorn.de> | 2018-04-22 23:05:30 +0200 |
commit | fa1c2acbf6745eeef3c19d4945fe6da3c8c446da (patch) | |
tree | 03960cd78b5415ff9c0a3098fbe4511823d7c76c /src/import.rs | |
parent | 146ed281c586af4dc1337b2d1f72546130b4c127 (diff) |
Refactor error handling to use error_chain
Diffstat (limited to 'src/import.rs')
-rw-r--r-- | src/import.rs | 17 |
1 files changed, 5 insertions, 12 deletions
diff --git a/src/import.rs b/src/import.rs index 89d7806..adf9285 100644 --- a/src/import.rs +++ b/src/import.rs @@ -11,23 +11,19 @@ use std::io::Read; use serde_json; -use result::Result; use task::Task; -use error::{TaskError, TaskErrorKind}; +use error::{Result, ResultExt}; +use error::ErrorKind::*; /// Import taskwarrior-exported JSON. This expects an JSON Array of objects, as exported by /// taskwarrior. pub fn import<R: Read>(r: R) -> Result<Vec<Task>> { - serde_json::from_reader(r).map_err(|e| { - TaskError::new(TaskErrorKind::ParserError, Some(Box::new(e))) - }) + serde_json::from_reader(r).chain_err(|| ParserError) } /// Import a single JSON-formatted Task pub fn import_task(s: &str) -> Result<Task> { - serde_json::from_str(s).map_err(|e| { - TaskError::new(TaskErrorKind::ParserError, Some(Box::new(e))) - }) + serde_json::from_str(s).chain_err(|| ParserError) } /// Reads line by line and tries to parse a task-object per line. @@ -35,10 +31,7 @@ pub fn import_tasks<BR: BufRead>(r: BR) -> Vec<Result<Task>> { 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())), - ))); + vt.push(Err(line.unwrap_err()).chain_err(|| ReaderError)); continue; } // Unwrap is safe because of continue above |