libimagentrycategory: 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 49ec0702fd
commit a8daeb851d
7 changed files with 59 additions and 123 deletions

View file

@ -20,10 +20,10 @@ is-it-maintained-open-issues = { repository = "matthiasbeyer/imag" }
maintenance = { status = "actively-developed" } maintenance = { status = "actively-developed" }
[dependencies] [dependencies]
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" }
error-chain = "0.12" 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

@ -26,9 +26,9 @@ use libimagentrylink::internal::InternalLinker;
use toml_query::read::TomlValueReadTypeExt; use toml_query::read::TomlValueReadTypeExt;
use error::Result; use failure::Fallible as Result;
use error::CategoryError as CE; use failure::Error;
use error::CategoryErrorKind as CEK; use failure::err_msg;
use store::CATEGORY_REGISTER_NAME_FIELD_PATH; use store::CATEGORY_REGISTER_NAME_FIELD_PATH;
use iter::CategoryEntryIterator; use iter::CategoryEntryIterator;
@ -42,15 +42,15 @@ pub trait Category {
impl Category for Entry { impl Category for Entry {
fn is_category(&self) -> Result<bool> { fn is_category(&self) -> Result<bool> {
self.is::<IsCategory>().map_err(CE::from) self.is::<IsCategory>()
} }
fn get_name(&self) -> Result<String> { fn get_name(&self) -> Result<String> {
trace!("Getting category name of '{:?}'", self.get_location()); trace!("Getting category name of '{:?}'", self.get_location());
self.get_header() self.get_header()
.read_string(CATEGORY_REGISTER_NAME_FIELD_PATH) .read_string(CATEGORY_REGISTER_NAME_FIELD_PATH)
.map_err(CE::from)? .map_err(Error::from)?
.ok_or_else(|| CE::from_kind(CEK::CategoryNameMissing)) .ok_or_else(|| Error::from(err_msg("Category name missing")))
} }
fn get_entries<'a>(&self, store: &'a Store) -> Result<CategoryEntryIterator<'a>> { fn get_entries<'a>(&self, store: &'a Store) -> Result<CategoryEntryIterator<'a>> {

View file

@ -24,11 +24,12 @@ use toml::Value;
use libimagstore::store::Entry; use libimagstore::store::Entry;
use libimagentrylink::internal::InternalLinker; use libimagentrylink::internal::InternalLinker;
use libimagerror::errors::ErrorMsg as EM;
use error::CategoryErrorKind as CEK; use failure::Fallible as Result;
use error::CategoryError as CE; use failure::ResultExt;
use error::ResultExt; use failure::Error;
use error::Result; use failure::err_msg;
use store::CategoryStore; use store::CategoryStore;
pub trait EntryCategory { pub trait EntryCategory {
@ -51,7 +52,9 @@ impl EntryCategory for Entry {
trace!("Setting category '{}' UNCHECKED", s); trace!("Setting category '{}' UNCHECKED", s);
self.get_header_mut() self.get_header_mut()
.insert(&String::from("category.value"), Value::String(s.to_string())) .insert(&String::from("category.value"), Value::String(s.to_string()))
.chain_err(|| CEK::HeaderWriteError) .map_err(Error::from)
.context(EM::EntryHeaderWriteError)
.map_err(Error::from)
.map(|_| ()) .map(|_| ())
} }
@ -62,7 +65,7 @@ impl EntryCategory for Entry {
trace!("Setting category '{}' checked", s); trace!("Setting category '{}' checked", s);
let mut category = register let mut category = register
.get_category_by_name(s)? .get_category_by_name(s)?
.ok_or_else(|| CE::from_kind(CEK::CategoryDoesNotExist))?; .ok_or_else(|| Error::from(err_msg("Category does not exist")))?;
let _ = self.set_category(s)?; let _ = self.set_category(s)?;
let _ = self.add_internal_link(&mut category)?; let _ = self.add_internal_link(&mut category)?;
@ -74,13 +77,16 @@ impl EntryCategory for Entry {
trace!("Getting category from '{}'", self.get_location()); trace!("Getting category from '{}'", self.get_location());
self.get_header() self.get_header()
.read_string("category.value")? .read_string("category.value")?
.ok_or_else(|| CE::from_kind(CEK::CategoryNameMissing)) .ok_or_else(|| Error::from(err_msg("Category name missing")))
} }
fn has_category(&self) -> Result<bool> { fn has_category(&self) -> Result<bool> {
trace!("Has category? '{}'", self.get_location()); trace!("Has category? '{}'", self.get_location());
self.get_header().read("category.value") self.get_header()
.chain_err(|| CEK::HeaderReadError) .read("category.value")
.map_err(Error::from)
.context(EM::EntryHeaderReadError)
.map_err(Error::from)
.map(|x| x.is_some()) .map(|x| x.is_some())
} }
@ -95,9 +101,10 @@ impl EntryCategory for Entry {
self.get_header_mut() self.get_header_mut()
.delete("category.value") .delete("category.value")
.chain_err(|| CEK::HeaderWriteError) .map_err(Error::from)
.context(EM::EntryHeaderWriteError)
.map_err(Error::from)
.map(|_| ()) .map(|_| ())
} }
} }

View file

@ -1,77 +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 {
CategoryError, CategoryErrorKind, ResultExt, Result;
}
links {
StoreError(::libimagstore::error::StoreError, ::libimagstore::error::StoreErrorKind);
LinkError(::libimagentrylink::error::LinkError, ::libimagentrylink::error::LinkErrorKind);
EntryUtilError(::libimagentryutil::error::EntryUtilError, ::libimagentryutil::error::EntryUtilErrorKind);
}
foreign_links {
TomlQueryError(::toml_query::error::Error);
}
errors {
StoreReadError {
description("Store Read error")
display("Store Read error")
}
StoreWriteError {
description("Store Write error")
display("Store Write error")
}
StoreIdHandlingError {
description("StoreId handling error")
display("StoreId handling error")
}
HeaderReadError {
description("Header read error")
display("Header read error")
}
HeaderWriteError {
description("Header write error")
display("Header write error")
}
CategoryDoesNotExist {
description("Category does not exist")
display("Category does not exist")
}
TypeError {
description("Type Error")
display("Type Error")
}
CategoryNameMissing {
description("Category name is missing")
display("Category name is missing")
}
}
}

View file

@ -20,15 +20,16 @@
use libimagstore::storeid::StoreIdIterator; use libimagstore::storeid::StoreIdIterator;
use libimagstore::store::Store; use libimagstore::store::Store;
use libimagstore::store::FileLockEntry; use libimagstore::store::FileLockEntry;
use libimagerror::errors::ErrorMsg as EM;
use toml_query::read::TomlValueReadTypeExt; use toml_query::read::TomlValueReadTypeExt;
use error::Result; use failure::Fallible as Result;
use error::CategoryError as CE; use failure::ResultExt;
use error::CategoryErrorKind as CEK; use failure::Error;
use failure::err_msg;
use store::CATEGORY_REGISTER_NAME_FIELD_PATH; use store::CATEGORY_REGISTER_NAME_FIELD_PATH;
use entry::EntryCategory; use entry::EntryCategory;
use error::ResultExt;
/// Iterator for Category names /// Iterator for Category names
/// ///
@ -61,17 +62,18 @@ impl<'a> Iterator for CategoryNameIter<'a> {
while let Some(sid) = self.1.next() { while let Some(sid) = self.1.next() {
match sid { match sid {
Err(e) => return Some(Err(e).map_err(CE::from)), Err(e) => return Some(Err(e).map_err(Error::from)),
Ok(sid) => { Ok(sid) => {
if sid.is_in_collection(&["category"]) { if sid.is_in_collection(&["category"]) {
let func = |store: &Store| { // hack for returning Some(Result<_, _>) let func = |store: &Store| { // hack for returning Some(Result<_, _>)
store store
.get(sid)? .get(sid)?
.ok_or_else(|| CE::from_kind(CEK::StoreReadError))? .ok_or_else(|| Error::from(err_msg("Store read error")))?
.get_header() .get_header()
.read_string(query) .read_string(query)
.chain_err(|| CEK::HeaderReadError)? .map_err(Error::from)
.ok_or_else(|| CE::from_kind(CEK::StoreReadError)) .context(EM::EntryHeaderReadError)?
.ok_or_else(|| Error::from(err_msg("Store read error")))
}; };
return Some(func(&self.0)) return Some(func(&self.0))
@ -98,12 +100,12 @@ impl<'a> Iterator for CategoryEntryIterator<'a> {
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
while let Some(next) = self.1.next() { while let Some(next) = self.1.next() {
match next { match next {
Err(e) => return Some(Err(e).map_err(CE::from)), Err(e) => return Some(Err(e).map_err(Error::from)),
Ok(next) => { Ok(next) => {
let getter = |next| -> Result<(String, FileLockEntry<'a>)> { let getter = |next| -> Result<(String, FileLockEntry<'a>)> {
let entry = self.0 let entry = self.0
.get(next)? .get(next)?
.ok_or_else(|| CE::from_kind(CEK::StoreReadError))?; .ok_or_else(|| Error::from(err_msg("Store read error")))?;
Ok((entry.get_category()?, entry)) Ok((entry.get_category()?, entry))
}; };

View file

@ -39,7 +39,7 @@ extern crate toml_query;
extern crate toml; extern crate toml;
#[macro_use] #[macro_use]
extern crate log; extern crate log;
#[macro_use] extern crate error_chain; extern crate failure;
extern crate libimagerror; extern crate libimagerror;
#[macro_use] extern crate libimagstore; #[macro_use] extern crate libimagstore;
@ -49,7 +49,6 @@ extern crate libimagentrylink;
pub mod category; pub mod category;
pub mod entry; pub mod entry;
pub mod error;
pub mod store; pub mod store;
pub mod iter; pub mod iter;

View file

@ -27,11 +27,12 @@ use libimagstore::store::Store;
use libimagstore::store::FileLockEntry; use libimagstore::store::FileLockEntry;
use libimagstore::storeid::StoreId; use libimagstore::storeid::StoreId;
use libimagentryutil::isa::Is; use libimagentryutil::isa::Is;
use libimagerror::errors::ErrorMsg as EM;
use error::CategoryErrorKind as CEK; use failure::Fallible as Result;
use error::CategoryError as CE; use failure::ResultExt;
use error::ResultExt; use failure::Error;
use error::Result; use failure::err_msg;
use iter::CategoryNameIter; use iter::CategoryNameIter;
use category::IsCategory; use category::IsCategory;
@ -93,8 +94,8 @@ impl CategoryStore for Store {
{ {
let mut category = self.get(sid.clone())? let mut category = self.get(sid.clone())?
.ok_or_else(|| CEK::CategoryDoesNotExist) .ok_or_else(|| Error::from(err_msg("Category does not exist")))
.map_err(CE::from_kind)?; .map_err(Error::from)?;
for entry in category.get_entries(self)? { for entry in category.get_entries(self)? {
let mut entry = entry?; let mut entry = entry?;
@ -102,7 +103,7 @@ impl CategoryStore for Store {
} }
} }
self.delete(sid).map_err(CE::from) self.delete(sid)
} }
/// Get all category names /// Get all category names
@ -120,7 +121,8 @@ impl CategoryStore for Store {
let sid = mk_category_storeid(self.path().clone(), name)?; let sid = mk_category_storeid(self.path().clone(), name)?;
self.get(sid) self.get(sid)
.chain_err(|| CEK::StoreWriteError) .context(err_msg("Store write error"))
.map_err(Error::from)
} }
} }
@ -215,23 +217,26 @@ fn mk_category_storeid(base: PathBuf, s: &str) -> Result<StoreId> {
::module_path::ModuleEntryPath::new(s) ::module_path::ModuleEntryPath::new(s)
.into_storeid() .into_storeid()
.map(|id| id.with_base(base)) .map(|id| id.with_base(base))
.chain_err(|| CEK::StoreIdHandlingError) .context(err_msg("Store id handling error"))
.map_err(Error::from)
} }
#[inline] #[inline]
fn represents_category(store: &Store, sid: StoreId, name: &str) -> Result<bool> { fn represents_category(store: &Store, sid: StoreId, name: &str) -> Result<bool> {
sid.exists() sid.exists()
.chain_err(|| CEK::StoreIdHandlingError) .context(err_msg("Store id handling error"))
.map_err(Error::from)
.and_then(|bl| { .and_then(|bl| {
if bl { if bl {
store.get(sid) store.get(sid)
.chain_err(|| CEK::StoreReadError) .context(err_msg("Store read error"))
.map_err(Error::from)
.and_then(|fle| { .and_then(|fle| {
if let Some(fle) = fle { if let Some(fle) = fle {
fle.get_header() fle.get_header()
.read_string(&String::from(CATEGORY_REGISTER_NAME_FIELD_PATH)) .read_string(&String::from(CATEGORY_REGISTER_NAME_FIELD_PATH))
.chain_err(|| CEK::HeaderReadError)? .context(EM::EntryHeaderReadError)?
.ok_or(CE::from_kind(CEK::TypeError)) .ok_or_else(|| Error::from(EM::EntryHeaderTypeError))
.map(|s| s == name) .map(|s| s == name)
} else { } else {
Ok(false) Ok(false)