From ede70581f38529a3d949a5b1d295c7b3406013a7 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 30 Oct 2018 18:40:51 +0100 Subject: [PATCH] libimaghabit: Move from error-chain to failure Signed-off-by: Matthias Beyer --- lib/domain/libimaghabit/Cargo.toml | 6 +-- lib/domain/libimaghabit/src/error.rs | 60 ------------------------- lib/domain/libimaghabit/src/habit.rs | 36 +++++++-------- lib/domain/libimaghabit/src/instance.rs | 2 +- lib/domain/libimaghabit/src/iter.rs | 9 ++-- lib/domain/libimaghabit/src/lib.rs | 4 +- lib/domain/libimaghabit/src/result.rs | 25 ----------- lib/domain/libimaghabit/src/store.rs | 3 +- lib/domain/libimaghabit/src/util.rs | 8 ++-- 9 files changed, 34 insertions(+), 119 deletions(-) delete mode 100644 lib/domain/libimaghabit/src/error.rs delete mode 100644 lib/domain/libimaghabit/src/result.rs diff --git a/lib/domain/libimaghabit/Cargo.toml b/lib/domain/libimaghabit/Cargo.toml index 5e4eb9b6..451a7a94 100644 --- a/lib/domain/libimaghabit/Cargo.toml +++ b/lib/domain/libimaghabit/Cargo.toml @@ -23,9 +23,9 @@ maintenance = { status = "actively-developed" } chrono = "0.4" log = "0.4" toml = "0.4" -toml-query = "0.7" -error-chain = "0.12" -kairos = { git = "https://github.com/matthiasbeyer/kairos", branch = "master" } +toml-query = { git = "https://github.com/matthiasbeyer/toml-query", branch = "failure" } +kairos = { git = "https://github.com/matthiasbeyer/kairos", branch = "failure" } +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/libimaghabit/src/error.rs b/lib/domain/libimaghabit/src/error.rs deleted file mode 100644 index cc552eb0..00000000 --- a/lib/domain/libimaghabit/src/error.rs +++ /dev/null @@ -1,60 +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 { - HabitError, HabitErrorKind, ResultExt, Result; - } - - links { - StoreError(::libimagstore::error::StoreError, ::libimagstore::error::StoreErrorKind); - LinkError(::libimagentrylink::error::LinkError, ::libimagentrylink::error::LinkErrorKind); - KairosError(::kairos::error::KairosError, ::kairos::error::KairosErrorKind); - EntryUtilError(::libimagentryutil::error::EntryUtilError, ::libimagentryutil::error::EntryUtilErrorKind); - } - - foreign_links { - TomlError(::toml_query::error::Error); - ChronoError(::chrono::format::ParseError); - } - - errors { - HabitBuilderMissing(variable_name: &'static str) { - description("Habit builder has not all required information") - display("Habit builder misses {}", variable_name) - } - - HabitBuilderLogicError(text: &'static str) { - description("Logic error in Habit builder") - display("Logic error: {}", text) - } - - HeaderFieldMissing(path: &'static str) { - description("Header field missing") - display("Header field missing: {}", path) - } - - HeaderTypeError(path: &'static str, required_type: &'static str) { - description("Header type error") - display("Header type error: Expected {} at {}, found other type", required_type, path) - } - - } -} - diff --git a/lib/domain/libimaghabit/src/habit.rs b/lib/domain/libimaghabit/src/habit.rs index cd53c290..3c19ac21 100644 --- a/lib/domain/libimaghabit/src/habit.rs +++ b/lib/domain/libimaghabit/src/habit.rs @@ -23,10 +23,10 @@ use toml_query::insert::TomlValueInsertExt; use chrono::NaiveDateTime; use chrono::Local; use chrono::NaiveDate; +use failure::Error; +use failure::Fallible as Result; +use failure::err_msg; -use error::HabitError as HE; -use error::HabitErrorKind as HEK; -use error::*; use iter::HabitInstanceStoreIdIterator; use util::IsHabitCheck; use util::get_string_header_from_entry; @@ -148,7 +148,7 @@ impl HabitTemplate for Entry { match parse(&r)? { Parsed::TimeType(tt) => Ok(tt), Parsed::Iterator(_) => { - Err(format!("'{}' yields an iterator. Cannot use.", r).into()) + Err(format_err!("'{}' yields an iterator. Cannot use.", r)) }, } }; @@ -166,10 +166,7 @@ impl HabitTemplate for Entry { .calculate()? .get_moment() .map(Clone::clone) - .ok_or_else(|| { - let kind : HEK = "until-date seems to have non-date value".to_owned().into(); - HE::from_kind(kind) - }) + .ok_or_else(|| Error::from(err_msg("until-date seems to have non-date value"))) }); debug!("Until-Date is {:?}", basedate); @@ -192,7 +189,7 @@ impl HabitTemplate for Entry { } } } else { - return Err("Iterator seems to return bogus values.".to_owned().into()); + return Err(err_msg("Iterator seems to return bogus values.")); } } @@ -265,7 +262,7 @@ fn instance_id_for_name_and_datestr(habit_name: &String, habit_date: &String) -> ModuleEntryPath::new(format!("instance/{}-{}", habit_name, habit_date)) .into_storeid() - .map_err(HE::from) + .map_err(Error::from) } pub mod builder { @@ -280,9 +277,10 @@ pub mod builder { use libimagentryutil::isa::Is; use libimagutil::debug_result::DebugResult; - use error::HabitError as HE; - use error::HabitErrorKind as HEK; - use error::*; + use failure::Error; + use failure::Fallible as Result; + use failure::err_msg; + use libimagutil::date::date_to_string; use habit::IsHabitTemplate; @@ -324,8 +322,8 @@ pub mod builder { pub fn build<'a>(self, store: &'a Store) -> Result> { #[inline] - fn mkerr(s: &'static str) -> HE { - HE::from_kind(HEK::HabitBuilderMissing(s)) + fn mkerr(s: &'static str) -> Error { + Error::from(format_err!("Habit builder missing: {}", s)) } let name = self.name @@ -336,21 +334,21 @@ pub mod builder { .ok_or_else(|| mkerr("date")) .map_dbg_str("Success: Date present")?; - let recur = self.recurspec + let recur : String = self.recurspec .ok_or_else(|| mkerr("recurspec")) .map_dbg_str("Success: Recurr spec present")?; if let Some(until) = self.untildate { debug!("Success: Until-Date present"); if dateobj > until { - let e = HE::from_kind(HEK::HabitBuilderLogicError("until-date before start date")); + let e = Error::from(err_msg("Habit builder logic error: until-date before start date")); return Err(e); } } - if let Err(e) = ::kairos::parser::parse(&recur) { + if let Err(e) = ::kairos::parser::parse(&recur).map_err(Error::from) { debug!("Kairos failed: {:?}", e); - return Err(e).map_err(From::from); + return Err(e) } let date = date_to_string(&dateobj); debug!("Success: Date valid"); diff --git a/lib/domain/libimaghabit/src/instance.rs b/lib/domain/libimaghabit/src/instance.rs index 3b088bbb..f04c46c9 100644 --- a/lib/domain/libimaghabit/src/instance.rs +++ b/lib/domain/libimaghabit/src/instance.rs @@ -20,8 +20,8 @@ use chrono::NaiveDate; use toml::Value; use toml_query::set::TomlValueSetExt; +use failure::Fallible as Result; -use error::*; use util::*; use libimagstore::store::Entry; diff --git a/lib/domain/libimaghabit/src/iter.rs b/lib/domain/libimaghabit/src/iter.rs index 6a1af765..cdd65e9c 100644 --- a/lib/domain/libimaghabit/src/iter.rs +++ b/lib/domain/libimaghabit/src/iter.rs @@ -17,13 +17,14 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // +use failure::Error; +use failure::Fallible as Result; + use libimagstore::storeid::StoreIdIterator; use libimagstore::storeid::StoreIdIteratorWithStore; use libimagstore::storeid::StoreId; use util::IsHabitCheck; -use error::Result; -use error::HabitError as HE; pub struct HabitTemplateStoreIdIterator(StoreIdIterator); @@ -36,7 +37,7 @@ impl Iterator for HabitTemplateStoreIdIterator { Ok(n) => if n.is_habit_template() { return Some(Ok(n)) }, - Err(e) => return Some(Err(e).map_err(HE::from)), + Err(e) => return Some(Err(e).map_err(Error::from)), } } None @@ -72,7 +73,7 @@ impl Iterator for HabitInstanceStoreIdIterator { Ok(n) => if n.is_habit_instance() { return Some(Ok(n)); }, - Err(e) => return Some(Err(e).map_err(HE::from)), + Err(e) => return Some(Err(e).map_err(Error::from)), } } None diff --git a/lib/domain/libimaghabit/src/lib.rs b/lib/domain/libimaghabit/src/lib.rs index 5272cc6b..c230756f 100644 --- a/lib/domain/libimaghabit/src/lib.rs +++ b/lib/domain/libimaghabit/src/lib.rs @@ -38,7 +38,7 @@ extern crate toml; extern crate toml_query; extern crate kairos; #[macro_use] extern crate log; -#[macro_use] extern crate error_chain; +#[macro_use] extern crate failure; #[macro_use] extern crate libimagstore; extern crate libimagerror; @@ -49,11 +49,9 @@ extern crate libimagutil; module_entry_path_mod!("habit"); -pub mod error; pub mod habit; pub mod instance; pub mod iter; -pub mod result; pub mod store; pub mod util; diff --git a/lib/domain/libimaghabit/src/result.rs b/lib/domain/libimaghabit/src/result.rs deleted file mode 100644 index 904b1de0..00000000 --- a/lib/domain/libimaghabit/src/result.rs +++ /dev/null @@ -1,25 +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 -// - -use std::result::Result as RResult; - -use error::HabitError; - -pub type Result = RResult; - diff --git a/lib/domain/libimaghabit/src/store.rs b/lib/domain/libimaghabit/src/store.rs index b4f89bfd..489f7413 100644 --- a/lib/domain/libimaghabit/src/store.rs +++ b/lib/domain/libimaghabit/src/store.rs @@ -17,7 +17,8 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // -use error::Result; +use failure::Fallible as Result; + use habit::builder::HabitBuilder; use iter::HabitTemplateStoreIdIterator; use iter::HabitInstanceStoreIdIterator; diff --git a/lib/domain/libimaghabit/src/util.rs b/lib/domain/libimaghabit/src/util.rs index d9d0e964..bb5b2140 100644 --- a/lib/domain/libimaghabit/src/util.rs +++ b/lib/domain/libimaghabit/src/util.rs @@ -19,13 +19,15 @@ use std::ops::BitXor; -use error::Result; +use failure::Error; +use failure::Fallible as Result; use habit::HabitTemplate; use instance::HabitInstance; use libimagstore::storeid::StoreId; use libimagstore::store::Entry; +use libimagerror::errors::ErrorMsg as EM; /// Helper trait to check whether a object which can be a habit instance and a habit template is /// actually a valid object, whereas "valid" is defined that it is _either_ an instance or a @@ -81,11 +83,11 @@ impl IsHabitCheck for Entry { #[inline] pub fn get_string_header_from_entry(e: &Entry, path: &'static str) -> Result { - use error::HabitErrorKind as HEK; use toml_query::read::TomlValueReadTypeExt; e.get_header() .read_string(path)? - .ok_or(HEK::HeaderFieldMissing(path).into()) + .ok_or_else(|| EM::EntryHeaderFieldMissing(path)) + .map_err(Error::from) }