From d3c082618815df6eb2d3189bb89e641d4ebbcae8 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 30 Oct 2018 18:40:51 +0100 Subject: [PATCH] libimagtodo: Move from error-chain to failure Signed-off-by: Matthias Beyer --- lib/domain/libimagtodo/Cargo.toml | 6 +- lib/domain/libimagtodo/src/error.rs | 70 ----------------------- lib/domain/libimagtodo/src/iter.rs | 5 +- lib/domain/libimagtodo/src/lib.rs | 3 +- lib/domain/libimagtodo/src/task.rs | 15 +++-- lib/domain/libimagtodo/src/taskstore.rs | 76 +++++++++++-------------- 6 files changed, 48 insertions(+), 127 deletions(-) delete mode 100644 lib/domain/libimagtodo/src/error.rs diff --git a/lib/domain/libimagtodo/Cargo.toml b/lib/domain/libimagtodo/Cargo.toml index 696f5978..a7f88e46 100644 --- a/lib/domain/libimagtodo/Cargo.toml +++ b/lib/domain/libimagtodo/Cargo.toml @@ -20,13 +20,13 @@ is-it-maintained-open-issues = { repository = "matthiasbeyer/imag" } maintenance = { status = "actively-developed" } [dependencies] -task-hookrs = { git = "https://github.com/matthiasbeyer/task-hookrs", branch = "master" } +task-hookrs = { git = "https://github.com/matthiasbeyer/task-hookrs", branch = "failure" } uuid = "0.7" toml = "0.4" -toml-query = "0.7" +toml-query = { git = "https://github.com/matthiasbeyer/toml-query", branch = "failure" } log = "0.4.0" serde_json = "1" -error-chain = "0.12" +failure = "0.1" libimagstore = { version = "0.9.0", path = "../../../lib/core/libimagstore" } libimagerror = { version = "0.9.0", path = "../../../lib/core/libimagerror" } diff --git a/lib/domain/libimagtodo/src/error.rs b/lib/domain/libimagtodo/src/error.rs deleted file mode 100644 index 7498d387..00000000 --- a/lib/domain/libimagtodo/src/error.rs +++ /dev/null @@ -1,70 +0,0 @@ -// -// imag - the personal information management suite for the commandline -// Copyright (C) 2015-2018 Matthias Beyer and contributors -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Lesser General Public -// License as published by the Free Software Foundation; version -// 2.1 of the License. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public -// License along with this library; if not, write to the Free Software -// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -// - -error_chain! { - types { - TodoError, TodoErrorKind, ResultExt, Result; - } - - links { - StoreError(::libimagstore::error::StoreError, ::libimagstore::error::StoreErrorKind); - } - - foreign_links { - TomlQueryError(::toml_query::error::Error); - } - - errors { - ConversionError { - description("Conversion Error") - display("Conversion Error") - } - - StoreIdError { - description("Store Id handling error") - display("Store Id handling error") - } - - ImportError { - description("Error importing") - display("Error importing") - } - - UTF8Error { - description("Encountered non-UTF8 characters while reading input") - display("Encountered non-UTF8 characters while reading input") - } - - HeaderFieldMissing { - description("Header field missing") - display("Header field missing") - } - - HeaderTypeError { - description("Header field type error") - display("Header field type error") - } - - UuidParserError { - description("Uuid parser error") - display("Uuid parser error") - } - } -} - diff --git a/lib/domain/libimagtodo/src/iter.rs b/lib/domain/libimagtodo/src/iter.rs index c5c63943..1d7b7494 100644 --- a/lib/domain/libimagtodo/src/iter.rs +++ b/lib/domain/libimagtodo/src/iter.rs @@ -20,8 +20,7 @@ use libimagstore::storeid::StoreIdIterator; use libimagstore::storeid::StoreId; -use error::Result; -use error::TodoError as TE; +use failure::Fallible as Result; pub struct TaskIdIterator(StoreIdIterator); @@ -40,7 +39,7 @@ impl Iterator for TaskIdIterator { loop { match self.0.next() { None => return None, - Some(Err(e)) => return Some(Err(e).map_err(TE::from)), + Some(Err(e)) => return Some(Err(e)), Some(Ok(n)) => if n.is_in_collection(&["todo", "taskwarrior"]) { return Some(Ok(n)) }, // else continue diff --git a/lib/domain/libimagtodo/src/lib.rs b/lib/domain/libimagtodo/src/lib.rs index 09927b79..b28b2831 100644 --- a/lib/domain/libimagtodo/src/lib.rs +++ b/lib/domain/libimagtodo/src/lib.rs @@ -40,7 +40,7 @@ extern crate toml; extern crate toml_query; #[macro_use] extern crate log; extern crate serde_json; -#[macro_use] extern crate error_chain; +extern crate failure; #[macro_use] extern crate libimagstore; extern crate libimagerror; @@ -48,7 +48,6 @@ extern crate task_hookrs; module_entry_path_mod!("todo"); -pub mod error; pub mod task; pub mod taskstore; pub mod iter; diff --git a/lib/domain/libimagtodo/src/task.rs b/lib/domain/libimagtodo/src/task.rs index b16cec06..9df96e27 100644 --- a/lib/domain/libimagtodo/src/task.rs +++ b/lib/domain/libimagtodo/src/task.rs @@ -17,12 +17,13 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // -use error::TodoError as TE; -use error::TodoErrorKind as TEK; -use error::ResultExt; -use error::Result; +use failure::ResultExt; +use failure::Error; +use failure::err_msg; +use failure::Fallible as Result; use libimagstore::store::Entry; +use libimagerror::errors::ErrorMsg as EM; use uuid::Uuid; use toml_query::read::TomlValueReadTypeExt; @@ -35,8 +36,10 @@ impl Task for Entry { fn get_uuid(&self) -> Result { self.get_header() .read_string("todo.uuid")? - .ok_or(TE::from_kind(TEK::HeaderFieldMissing)) - .and_then(|u| Uuid::parse_str(&u).chain_err(|| TEK::UuidParserError)) + .ok_or_else(|| Error::from(EM::EntryHeaderFieldMissing("todo.uuid"))) + .and_then(|u| { + Uuid::parse_str(&u).context(err_msg("UUID Parser error")).map_err(Error::from) + }) } } diff --git a/lib/domain/libimagtodo/src/taskstore.rs b/lib/domain/libimagtodo/src/taskstore.rs index a85696e4..a66f9516 100644 --- a/lib/domain/libimagtodo/src/taskstore.rs +++ b/lib/domain/libimagtodo/src/taskstore.rs @@ -26,15 +26,16 @@ use uuid::Uuid; use task_hookrs::task::Task as TTask; use task_hookrs::import::{import_task, import_tasks}; +use failure::Fallible as Result; +use failure::ResultExt; +use failure::Error; +use failure::err_msg; use libimagstore::store::{FileLockEntry, Store}; use libimagstore::storeid::IntoStoreId; +use libimagerror::errors::ErrorMsg as EM; use module_path::ModuleEntryPath; -use error::TodoErrorKind as TEK; -use error::TodoError as TE; -use error::Result; -use error::ResultExt; use iter::TaskIdIterator; /// Task struct containing a `FileLockEntry` @@ -55,9 +56,10 @@ impl<'a> TaskStore<'a> for Store { fn import_task_from_reader(&'a self, mut r: R) -> Result<(FileLockEntry<'a>, String, Uuid)> { let mut line = String::new(); - r.read_line(&mut line).map_err(|_| TE::from_kind(TEK::UTF8Error))?; + r.read_line(&mut line).context(EM::UTF8Error)?; import_task(&line.as_str()) - .map_err(|_| TE::from_kind(TEK::ImportError)) + .context(err_msg("Error importing")) + .map_err(Error::from) .and_then(|t| { let uuid = t.uuid().clone(); self.new_from_twtask(t).map(|t| (t, line, uuid)) @@ -75,7 +77,7 @@ impl<'a> TaskStore<'a> for Store { /// fn get_task_from_import(&'a self, mut r: R) -> Result, String>> { let mut line = String::new(); - r.read_line(&mut line).chain_err(|| TEK::UTF8Error)?; + r.read_line(&mut line).context(EM::UTF8Error)?; self.get_task_from_string(line) } @@ -85,7 +87,8 @@ impl<'a> TaskStore<'a> for Store { /// For an explanation on the return values see `Task::get_from_import()`. fn get_task_from_string(&'a self, s: String) -> Result, String>> { import_task(s.as_str()) - .map_err(|_| TE::from_kind(TEK::ImportError)) + .context(err_msg("Import error")) + .map_err(Error::from) .map(|t| t.uuid().clone()) .and_then(|uuid| self.get_task_from_uuid(uuid)) .and_then(|o| match o { @@ -101,14 +104,13 @@ impl<'a> TaskStore<'a> for Store { ModuleEntryPath::new(format!("taskwarrior/{}", uuid)) .into_storeid() .and_then(|store_id| self.get(store_id)) - .map_err(TE::from) } /// Same as Task::get_from_import() but uses Store::retrieve() rather than Store::get(), to /// implicitely create the task if it does not exist. fn retrieve_task_from_import(&'a self, mut r: R) -> Result> { let mut line = String::new(); - r.read_line(&mut line).chain_err(|| TEK::UTF8Error)?; + r.read_line(&mut line).context(EM::UTF8Error)?; self.retrieve_task_from_string(line) } @@ -119,7 +121,8 @@ impl<'a> TaskStore<'a> for Store { .and_then(|opt| match opt { Ok(task) => Ok(task), Err(string) => import_task(string.as_str()) - .map_err(|_| TE::from_kind(TEK::ImportError)) + .context(err_msg("Import error")) + .map_err(Error::from) .and_then(|t| self.new_from_twtask(t)), }) } @@ -129,33 +132,25 @@ impl<'a> TaskStore<'a> for Store { use task_hookrs::status::TaskStatus; for (counter, res_ttask) in import_tasks(r).into_iter().enumerate() { - match res_ttask { - Ok(ttask) => { - if counter % 2 == 1 { - // Only every second task is needed, the first one is the - // task before the change, and the second one after - // the change. The (maybe modified) second one is - // expected by taskwarrior. - match serde_to_string(&ttask).chain_err(|| TEK::ImportError) { - // use println!() here, as we talk with TW - Ok(val) => println!("{}", val), - Err(e) => return Err(e), - } + let ttask = res_ttask.context(err_msg("Import error"))?; - // Taskwarrior does not have the concept of deleted tasks, but only modified - // ones. - // - // Here we check if the status of a task is deleted and if yes, we delete it - // from the store. - if *ttask.status() == TaskStatus::Deleted { - match self.delete_task_by_uuid(*ttask.uuid()) { - Ok(_) => info!("Deleted task {}", *ttask.uuid()), - Err(e) => return Err(e), - } - } - } // end if c % 2 - }, - Err(_) => return Err(TE::from_kind(TEK::ImportError)), + if counter % 2 == 1 { + // Only every second task is needed, the first one is the + // task before the change, and the second one after + // the change. The (maybe modified) second one is + // expected by taskwarrior. + let val = serde_to_string(&ttask).context(err_msg("Import error"))?; + println!("{}", val); + + // Taskwarrior does not have the concept of deleted tasks, but only modified + // ones. + // + // Here we check if the status of a task is deleted and if yes, we delete it + // from the store. + if *ttask.status() == TaskStatus::Deleted { + let _ = self.delete_task_by_uuid(*ttask.uuid())?; + info!("Deleted task {}", *ttask.uuid()); + } } } Ok(()) @@ -165,13 +160,10 @@ impl<'a> TaskStore<'a> for Store { ModuleEntryPath::new(format!("taskwarrior/{}", uuid)) .into_storeid() .and_then(|id| self.delete(id)) - .map_err(TE::from) } fn all_tasks(&self) -> Result { - self.entries() - .map(|i| TaskIdIterator::new(i.without_store())) - .map_err(TE::from) + self.entries().map(|i| TaskIdIterator::new(i.without_store())) } fn new_from_twtask(&'a self, task: TTask) -> Result> { @@ -181,10 +173,8 @@ impl<'a> TaskStore<'a> for Store { let uuid = task.uuid(); ModuleEntryPath::new(format!("taskwarrior/{}", uuid)) .into_storeid() - .chain_err(|| TEK::StoreIdError) .and_then(|id| { self.retrieve(id) - .map_err(TE::from) .and_then(|mut fle| { { let hdr = fle.get_header_mut();