libimagentrycategory: Move from error-chain to failure
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
This commit is contained in:
parent
49ec0702fd
commit
a8daeb851d
7 changed files with 59 additions and 123 deletions
|
@ -20,10 +20,10 @@ is-it-maintained-open-issues = { repository = "matthiasbeyer/imag" }
|
|||
maintenance = { status = "actively-developed" }
|
||||
|
||||
[dependencies]
|
||||
log = "0.4.0"
|
||||
toml = "0.4"
|
||||
toml-query = "0.7"
|
||||
error-chain = "0.12"
|
||||
log = "0.4.0"
|
||||
toml = "0.4"
|
||||
toml-query = { git = "https://github.com/matthiasbeyer/toml-query", branch = "failure" }
|
||||
failure = "0.1"
|
||||
|
||||
libimagerror = { version = "0.9.0", path = "../../../lib/core/libimagerror" }
|
||||
libimagstore = { version = "0.9.0", path = "../../../lib/core/libimagstore" }
|
||||
|
|
|
@ -26,9 +26,9 @@ use libimagentrylink::internal::InternalLinker;
|
|||
|
||||
use toml_query::read::TomlValueReadTypeExt;
|
||||
|
||||
use error::Result;
|
||||
use error::CategoryError as CE;
|
||||
use error::CategoryErrorKind as CEK;
|
||||
use failure::Fallible as Result;
|
||||
use failure::Error;
|
||||
use failure::err_msg;
|
||||
use store::CATEGORY_REGISTER_NAME_FIELD_PATH;
|
||||
use iter::CategoryEntryIterator;
|
||||
|
||||
|
@ -42,15 +42,15 @@ pub trait Category {
|
|||
|
||||
impl Category for Entry {
|
||||
fn is_category(&self) -> Result<bool> {
|
||||
self.is::<IsCategory>().map_err(CE::from)
|
||||
self.is::<IsCategory>()
|
||||
}
|
||||
|
||||
fn get_name(&self) -> Result<String> {
|
||||
trace!("Getting category name of '{:?}'", self.get_location());
|
||||
self.get_header()
|
||||
.read_string(CATEGORY_REGISTER_NAME_FIELD_PATH)
|
||||
.map_err(CE::from)?
|
||||
.ok_or_else(|| CE::from_kind(CEK::CategoryNameMissing))
|
||||
.map_err(Error::from)?
|
||||
.ok_or_else(|| Error::from(err_msg("Category name missing")))
|
||||
}
|
||||
|
||||
fn get_entries<'a>(&self, store: &'a Store) -> Result<CategoryEntryIterator<'a>> {
|
||||
|
|
|
@ -24,11 +24,12 @@ use toml::Value;
|
|||
|
||||
use libimagstore::store::Entry;
|
||||
use libimagentrylink::internal::InternalLinker;
|
||||
use libimagerror::errors::ErrorMsg as EM;
|
||||
|
||||
use error::CategoryErrorKind as CEK;
|
||||
use error::CategoryError as CE;
|
||||
use error::ResultExt;
|
||||
use error::Result;
|
||||
use failure::Fallible as Result;
|
||||
use failure::ResultExt;
|
||||
use failure::Error;
|
||||
use failure::err_msg;
|
||||
use store::CategoryStore;
|
||||
|
||||
pub trait EntryCategory {
|
||||
|
@ -51,7 +52,9 @@ impl EntryCategory for Entry {
|
|||
trace!("Setting category '{}' UNCHECKED", s);
|
||||
self.get_header_mut()
|
||||
.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(|_| ())
|
||||
}
|
||||
|
||||
|
@ -62,7 +65,7 @@ impl EntryCategory for Entry {
|
|||
trace!("Setting category '{}' checked", s);
|
||||
let mut category = register
|
||||
.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.add_internal_link(&mut category)?;
|
||||
|
@ -74,13 +77,16 @@ impl EntryCategory for Entry {
|
|||
trace!("Getting category from '{}'", self.get_location());
|
||||
self.get_header()
|
||||
.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> {
|
||||
trace!("Has category? '{}'", self.get_location());
|
||||
self.get_header().read("category.value")
|
||||
.chain_err(|| CEK::HeaderReadError)
|
||||
self.get_header()
|
||||
.read("category.value")
|
||||
.map_err(Error::from)
|
||||
.context(EM::EntryHeaderReadError)
|
||||
.map_err(Error::from)
|
||||
.map(|x| x.is_some())
|
||||
}
|
||||
|
||||
|
@ -95,9 +101,10 @@ impl EntryCategory for Entry {
|
|||
|
||||
self.get_header_mut()
|
||||
.delete("category.value")
|
||||
.chain_err(|| CEK::HeaderWriteError)
|
||||
.map_err(Error::from)
|
||||
.context(EM::EntryHeaderWriteError)
|
||||
.map_err(Error::from)
|
||||
.map(|_| ())
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -20,15 +20,16 @@
|
|||
use libimagstore::storeid::StoreIdIterator;
|
||||
use libimagstore::store::Store;
|
||||
use libimagstore::store::FileLockEntry;
|
||||
use libimagerror::errors::ErrorMsg as EM;
|
||||
|
||||
use toml_query::read::TomlValueReadTypeExt;
|
||||
|
||||
use error::Result;
|
||||
use error::CategoryError as CE;
|
||||
use error::CategoryErrorKind as CEK;
|
||||
use failure::Fallible as Result;
|
||||
use failure::ResultExt;
|
||||
use failure::Error;
|
||||
use failure::err_msg;
|
||||
use store::CATEGORY_REGISTER_NAME_FIELD_PATH;
|
||||
use entry::EntryCategory;
|
||||
use error::ResultExt;
|
||||
|
||||
/// Iterator for Category names
|
||||
///
|
||||
|
@ -61,17 +62,18 @@ impl<'a> Iterator for CategoryNameIter<'a> {
|
|||
|
||||
while let Some(sid) = self.1.next() {
|
||||
match sid {
|
||||
Err(e) => return Some(Err(e).map_err(CE::from)),
|
||||
Err(e) => return Some(Err(e).map_err(Error::from)),
|
||||
Ok(sid) => {
|
||||
if sid.is_in_collection(&["category"]) {
|
||||
let func = |store: &Store| { // hack for returning Some(Result<_, _>)
|
||||
store
|
||||
.get(sid)?
|
||||
.ok_or_else(|| CE::from_kind(CEK::StoreReadError))?
|
||||
.ok_or_else(|| Error::from(err_msg("Store read error")))?
|
||||
.get_header()
|
||||
.read_string(query)
|
||||
.chain_err(|| CEK::HeaderReadError)?
|
||||
.ok_or_else(|| CE::from_kind(CEK::StoreReadError))
|
||||
.map_err(Error::from)
|
||||
.context(EM::EntryHeaderReadError)?
|
||||
.ok_or_else(|| Error::from(err_msg("Store read error")))
|
||||
};
|
||||
|
||||
return Some(func(&self.0))
|
||||
|
@ -98,12 +100,12 @@ impl<'a> Iterator for CategoryEntryIterator<'a> {
|
|||
fn next(&mut self) -> Option<Self::Item> {
|
||||
while let Some(next) = self.1.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) => {
|
||||
let getter = |next| -> Result<(String, FileLockEntry<'a>)> {
|
||||
let entry = self.0
|
||||
.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))
|
||||
};
|
||||
|
||||
|
|
|
@ -39,7 +39,7 @@ extern crate toml_query;
|
|||
extern crate toml;
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
#[macro_use] extern crate error_chain;
|
||||
extern crate failure;
|
||||
|
||||
extern crate libimagerror;
|
||||
#[macro_use] extern crate libimagstore;
|
||||
|
@ -49,7 +49,6 @@ extern crate libimagentrylink;
|
|||
|
||||
pub mod category;
|
||||
pub mod entry;
|
||||
pub mod error;
|
||||
pub mod store;
|
||||
pub mod iter;
|
||||
|
||||
|
|
|
@ -27,11 +27,12 @@ use libimagstore::store::Store;
|
|||
use libimagstore::store::FileLockEntry;
|
||||
use libimagstore::storeid::StoreId;
|
||||
use libimagentryutil::isa::Is;
|
||||
use libimagerror::errors::ErrorMsg as EM;
|
||||
|
||||
use error::CategoryErrorKind as CEK;
|
||||
use error::CategoryError as CE;
|
||||
use error::ResultExt;
|
||||
use error::Result;
|
||||
use failure::Fallible as Result;
|
||||
use failure::ResultExt;
|
||||
use failure::Error;
|
||||
use failure::err_msg;
|
||||
use iter::CategoryNameIter;
|
||||
use category::IsCategory;
|
||||
|
||||
|
@ -93,8 +94,8 @@ impl CategoryStore for Store {
|
|||
|
||||
{
|
||||
let mut category = self.get(sid.clone())?
|
||||
.ok_or_else(|| CEK::CategoryDoesNotExist)
|
||||
.map_err(CE::from_kind)?;
|
||||
.ok_or_else(|| Error::from(err_msg("Category does not exist")))
|
||||
.map_err(Error::from)?;
|
||||
|
||||
for entry in category.get_entries(self)? {
|
||||
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
|
||||
|
@ -120,7 +121,8 @@ impl CategoryStore for Store {
|
|||
let sid = mk_category_storeid(self.path().clone(), name)?;
|
||||
|
||||
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)
|
||||
.into_storeid()
|
||||
.map(|id| id.with_base(base))
|
||||
.chain_err(|| CEK::StoreIdHandlingError)
|
||||
.context(err_msg("Store id handling error"))
|
||||
.map_err(Error::from)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn represents_category(store: &Store, sid: StoreId, name: &str) -> Result<bool> {
|
||||
sid.exists()
|
||||
.chain_err(|| CEK::StoreIdHandlingError)
|
||||
.context(err_msg("Store id handling error"))
|
||||
.map_err(Error::from)
|
||||
.and_then(|bl| {
|
||||
if bl {
|
||||
store.get(sid)
|
||||
.chain_err(|| CEK::StoreReadError)
|
||||
.context(err_msg("Store read error"))
|
||||
.map_err(Error::from)
|
||||
.and_then(|fle| {
|
||||
if let Some(fle) = fle {
|
||||
fle.get_header()
|
||||
.read_string(&String::from(CATEGORY_REGISTER_NAME_FIELD_PATH))
|
||||
.chain_err(|| CEK::HeaderReadError)?
|
||||
.ok_or(CE::from_kind(CEK::TypeError))
|
||||
.context(EM::EntryHeaderReadError)?
|
||||
.ok_or_else(|| Error::from(EM::EntryHeaderTypeError))
|
||||
.map(|s| s == name)
|
||||
} else {
|
||||
Ok(false)
|
||||
|
|
Loading…
Reference in a new issue