libimaghabit: Move from error-chain to failure

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
This commit is contained in:
Matthias Beyer 2018-10-30 18:40:51 +01:00
parent c11e971139
commit ede70581f3
9 changed files with 34 additions and 119 deletions

View file

@ -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" }

View file

@ -1,60 +0,0 @@
//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015-2018 Matthias Beyer <mail@beyermatthias.de> 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)
}
}
}

View file

@ -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<FileLockEntry<'a>> {
#[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");

View file

@ -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;

View file

@ -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

View file

@ -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;

View file

@ -1,25 +0,0 @@
//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015-2018 Matthias Beyer <mail@beyermatthias.de> 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<T> = RResult<T, HabitError>;

View file

@ -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;

View file

@ -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<String> {
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)
}