libimagentrytag: 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:52 +01:00
parent 32e8c43ccb
commit 0cbc6741a7
5 changed files with 29 additions and 71 deletions

View file

@ -26,8 +26,8 @@ toml = "0.4"
itertools = "0.7" itertools = "0.7"
is-match = "0.1" is-match = "0.1"
filters = "0.3" filters = "0.3"
toml-query = "0.7" toml-query = { git = "https://github.com/matthiasbeyer/toml-query", branch = "failure" }
error-chain = "0.12" failure = "0.1"
libimagstore = { version = "0.9.0", path = "../../../lib/core/libimagstore" } libimagstore = { version = "0.9.0", path = "../../../lib/core/libimagstore" }
libimagerror = { version = "0.9.0", path = "../../../lib/core/libimagerror" } libimagerror = { version = "0.9.0", path = "../../../lib/core/libimagerror" }

View file

@ -1,48 +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 {
TagError, TagErrorKind, ResultExt, Result;
}
errors {
TagTypeError {
description("Entry Header Tag Type wrong")
display("Entry Header Tag Type wrong")
}
HeaderReadError {
description("Error while reading entry header")
display("Error while reading entry header")
}
HeaderWriteError {
description("Error while writing entry header")
display("Error while writing entry header")
}
NotATag {
description("String is not a tag")
display("String is not a tag")
}
}
}

View file

@ -43,12 +43,11 @@ extern crate toml;
extern crate toml_query; extern crate toml_query;
#[macro_use] extern crate is_match; #[macro_use] extern crate is_match;
extern crate filters; extern crate filters;
#[macro_use] extern crate error_chain; #[macro_use] extern crate failure;
extern crate libimagstore; extern crate libimagstore;
extern crate libimagerror; extern crate libimagerror;
pub mod error;
pub mod tag; pub mod tag;
pub mod tagable; pub mod tagable;

View file

@ -17,6 +17,8 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// //
use std::result::Result;
use regex::Regex; use regex::Regex;
pub type Tag = String; pub type Tag = String;

View file

@ -20,14 +20,15 @@
use itertools::Itertools; use itertools::Itertools;
use libimagstore::store::Entry; use libimagstore::store::Entry;
use libimagerror::errors::ErrorMsg as EM;
use toml_query::read::TomlValueReadExt; use toml_query::read::TomlValueReadExt;
use toml_query::insert::TomlValueInsertExt; use toml_query::insert::TomlValueInsertExt;
use error::TagErrorKind; use failure::Error;
use error::TagError as TE; use failure::ResultExt;
use error::ResultExt; use failure::Fallible as Result;
use error::Result; use failure::err_msg;
use tag::{Tag, TagSlice}; use tag::{Tag, TagSlice};
use tag::is_tag_str; use tag::is_tag_str;
@ -50,23 +51,22 @@ impl Tagable for Value {
fn get_tags(&self) -> Result<Vec<Tag>> { fn get_tags(&self) -> Result<Vec<Tag>> {
self.read("tag.values") self.read("tag.values")
.chain_err(|| TagErrorKind::HeaderReadError)? .map_err(Error::from)
.context(EM::EntryHeaderReadError)?
.map(|val| { .map(|val| {
debug!("Got Value of tags..."); debug!("Got Value of tags...");
val.as_array() val.as_array()
.map(|tags| { .map(|tags| {
debug!("Got Array<T> of tags..."); debug!("Got Array<T> of tags...");
if !tags.iter().all(|t| is_match!(*t, Value::String(_))) { if !tags.iter().all(|t| is_match!(*t, Value::String(_))) {
debug!("Got Array<T>, T != String of tags: {:?}", tags); return Err(format_err!("Tag type error: Got Array<T> where T is not a String: {:?}", tags));
return Err(TagErrorKind::TagTypeError.into());
} }
debug!("Got Array<String> of tags..."); debug!("Got Array<String> of tags...");
if tags.iter().any(|t| match *t { if tags.iter().any(|t| match *t {
Value::String(ref s) => !is_tag_str(s).is_ok(), Value::String(ref s) => !is_tag_str(s).is_ok(),
_ => unreachable!()}) _ => unreachable!()})
{ {
debug!("At least one tag is not a valid tag string"); return Err(format_err!("At least one tag is not a valid tag string"));
return Err(TagErrorKind::NotATag.into());
} }
Ok(tags.iter() Ok(tags.iter()
@ -86,21 +86,23 @@ impl Tagable for Value {
fn set_tags(&mut self, ts: &[Tag]) -> Result<()> { fn set_tags(&mut self, ts: &[Tag]) -> Result<()> {
if ts.iter().any(|tag| !is_tag_str(tag).is_ok()) { if ts.iter().any(|tag| !is_tag_str(tag).is_ok()) {
debug!("Not a tag: '{}'", ts.iter().filter(|t| !is_tag_str(t).is_ok()).next().unwrap()); let not_tag = ts.iter().filter(|t| !is_tag_str(t).is_ok()).next().unwrap();
return Err(TagErrorKind::NotATag.into()); return Err(format_err!("Not a tag: '{}'", not_tag));
} }
let a = ts.iter().unique().map(|t| Value::String(t.clone())).collect(); let a = ts.iter().unique().map(|t| Value::String(t.clone())).collect();
debug!("Setting tags = {:?}", a); debug!("Setting tags = {:?}", a);
self.insert("tag.values", Value::Array(a)) self.insert("tag.values", Value::Array(a))
.map(|_| ()) .map(|_| ())
.chain_err(|| TagErrorKind::HeaderWriteError) .map_err(|_| Error::from(EM::EntryHeaderWriteError))
} }
fn add_tag(&mut self, t: Tag) -> Result<()> { fn add_tag(&mut self, t: Tag) -> Result<()> {
if !is_tag_str(&t).map(|_| true).map_err(|_| TE::from_kind(TagErrorKind::NotATag))? { if !is_tag_str(&t).map(|_| true)
debug!("Not a tag: '{}'", t); .map_err(|s| format_err!("{}", s))
return Err(TagErrorKind::NotATag.into()); .context(err_msg("Not a tag"))?
{
return Err(format_err!("Not a tag: '{}'", t));
} }
self.get_tags() self.get_tags()
@ -113,9 +115,12 @@ impl Tagable for Value {
} }
fn remove_tag(&mut self, t: Tag) -> Result<()> { fn remove_tag(&mut self, t: Tag) -> Result<()> {
if !is_tag_str(&t).map(|_| true).map_err(|_| TE::from_kind(TagErrorKind::NotATag))? { if !is_tag_str(&t).map(|_| true)
.map_err(|s| format_err!("{}", s))
.context(err_msg("Not a tag"))?
{
debug!("Not a tag: '{}'", t); debug!("Not a tag: '{}'", t);
return Err(TagErrorKind::NotATag.into()); return Err(format_err!("Not a tag: '{}'", t));
} }
self.get_tags() self.get_tags()
@ -127,10 +132,10 @@ impl Tagable for Value {
} }
fn has_tag(&self, t: TagSlice) -> Result<bool> { fn has_tag(&self, t: TagSlice) -> Result<bool> {
let tags = self.read("tag.values").chain_err(|| TagErrorKind::HeaderReadError)?; let tags = self.read("tag.values").context(EM::EntryHeaderReadError)?;
if !tags.iter().all(|t| is_match!(*t, &Value::String(_))) { if !tags.iter().all(|t| is_match!(*t, &Value::String(_))) {
return Err(TagErrorKind::TagTypeError.into()); return Err(err_msg("Tag type error"))
} }
Ok(tags Ok(tags