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"
is-match = "0.1"
filters = "0.3"
toml-query = "0.7"
error-chain = "0.12"
toml-query = { git = "https://github.com/matthiasbeyer/toml-query", 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,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;
#[macro_use] extern crate is_match;
extern crate filters;
#[macro_use] extern crate error_chain;
#[macro_use] extern crate failure;
extern crate libimagstore;
extern crate libimagerror;
pub mod error;
pub mod tag;
pub mod tagable;

View File

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

View File

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