imag-diary: 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:53 +01:00
parent 8fb35b5fe1
commit 4486abed84
5 changed files with 20 additions and 11 deletions

View file

@ -25,8 +25,9 @@ maintenance = { status = "actively-developed" }
chrono = "0.4" chrono = "0.4"
log = "0.4.0" log = "0.4.0"
toml = "0.4" toml = "0.4"
toml-query = "0.7" toml-query = { git = "https://github.com/matthiasbeyer/toml-query", branch = "failure" }
itertools = "0.7" itertools = "0.7"
failure = "0.1"
libimagerror = { version = "0.9.0", path = "../../../lib/core/libimagerror" } libimagerror = { version = "0.9.0", path = "../../../lib/core/libimagerror" }
libimagstore = { version = "0.9.0", path = "../../../lib/core/libimagstore" } libimagstore = { version = "0.9.0", path = "../../../lib/core/libimagstore" }

View file

@ -21,10 +21,11 @@ use clap::ArgMatches;
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use chrono::Local; use chrono::Local;
use chrono::Timelike; use chrono::Timelike;
use failure::Error;
use failure::ResultExt;
use failure::err_msg;
use libimagdiary::diary::Diary; use libimagdiary::diary::Diary;
use libimagdiary::error::DiaryErrorKind as DEK;
use libimagdiary::error::ResultExt;
use libimagentryedit::edit::Edit; use libimagentryedit::edit::Edit;
use libimagrt::runtime::Runtime; use libimagrt::runtime::Runtime;
use libimagerror::trace::MapErrTrace; use libimagerror::trace::MapErrTrace;
@ -47,8 +48,7 @@ pub fn create(rt: &Runtime) {
Ok(()) Ok(())
} else { } else {
debug!("Editing new diary entry"); debug!("Editing new diary entry");
entry.edit_content(rt) entry.edit_content(rt).context(err_msg("Diary edit error")).map_err(Error::from)
.chain_err(|| DEK::DiaryEditError)
}; };
let _ = res.map_err_trace_exit_unwrap(1); let _ = res.map_err_trace_exit_unwrap(1);
@ -75,7 +75,9 @@ fn create_entry<'a>(diary: &'a Store, diaryname: &str, rt: &Runtime) -> FileLock
}) })
.map(|timed| { .map(|timed| {
let time = create_id_from_clispec(&create, &diaryname, timed); let time = create_id_from_clispec(&create, &diaryname, timed);
diary.new_entry_at(&diaryname, &time).chain_err(|| DEK::StoreWriteError) diary.new_entry_at(&diaryname, &time)
.context(err_msg("Store write error"))
.map_err(Error::from)
}) })
.unwrap_or_else(|| { .unwrap_or_else(|| {
debug!("Creating non-timed entry"); debug!("Creating non-timed entry");

View file

@ -29,9 +29,10 @@ use libimagerror::exit::ExitUnwrap;
use libimagutil::debug_result::*; use libimagutil::debug_result::*;
use libimagdiary::diaryid::DiaryId; use libimagdiary::diaryid::DiaryId;
use libimagdiary::diaryid::FromStoreId; use libimagdiary::diaryid::FromStoreId;
use libimagdiary::error::Result;
use libimagstore::storeid::IntoStoreId; use libimagstore::storeid::IntoStoreId;
use failure::Fallible as Result;
use util::get_diary_name; use util::get_diary_name;
pub fn list(rt: &Runtime) { pub fn list(rt: &Runtime) {

View file

@ -33,6 +33,7 @@
)] )]
#[macro_use] extern crate log; #[macro_use] extern crate log;
#[macro_use] extern crate failure;
extern crate clap; extern crate clap;
extern crate chrono; extern crate chrono;
extern crate toml; extern crate toml;

View file

@ -18,10 +18,13 @@
// //
use libimagrt::runtime::Runtime; use libimagrt::runtime::Runtime;
use libimagdiary::error::*; use libimagerror::errors::ErrorMsg as EM;
use toml::Value; use toml::Value;
use toml_query::read::TomlValueReadExt; use toml_query::read::TomlValueReadExt;
use failure::Error;
use failure::Fallible as Result;
use failure::ResultExt;
pub fn get_diary_name(rt: &Runtime) -> Option<String> { pub fn get_diary_name(rt: &Runtime) -> Option<String> {
use libimagdiary::config::get_default_diary_name; use libimagdiary::config::get_default_diary_name;
@ -58,14 +61,15 @@ pub fn get_diary_timed_config(rt: &Runtime, diary_name: &str) -> Result<Option<T
Some(cfg) => { Some(cfg) => {
let v = cfg let v = cfg
.read(&format!("diary.diaries.{}.timed", diary_name)) .read(&format!("diary.diaries.{}.timed", diary_name))
.chain_err(|| DiaryErrorKind::IOError); .context(EM::IO)
.map_err(Error::from);
match v { match v {
Ok(Some(&Value::String(ref s))) => parse_timed_string(s, diary_name).map(Some), Ok(Some(&Value::String(ref s))) => parse_timed_string(s, diary_name).map(Some),
Ok(Some(_)) => { Ok(Some(_)) => {
let s = format!("Type error at 'diary.diaryies.{}.timed': should be either 'd'/'daily', 'h'/'hourly', 'm'/'minutely' or 's'/'secondly'", diary_name); let s = format!("Type error at 'diary.diaryies.{}.timed': should be either 'd'/'daily', 'h'/'hourly', 'm'/'minutely' or 's'/'secondly'", diary_name);
Err(s).map_err(From::from) Err(format_err!("{}", s))
}, },
Ok(None) => Ok(None), Ok(None) => Ok(None),
@ -87,6 +91,6 @@ pub fn parse_timed_string(s: &str, diary_name: &str) -> Result<Timed> {
} else { } else {
let s = format!("Cannot parse config: 'diary.diaries.{}.timed = {}'", let s = format!("Cannot parse config: 'diary.diaries.{}.timed = {}'",
diary_name, s); diary_name, s);
Err(s).map_err(From::from) Err(format_err!("{}", s))
} }
} }