libimagentrygps: 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 0cbc6741a7
commit 7f04eb2bff
5 changed files with 27 additions and 109 deletions

View file

@ -21,10 +21,11 @@ maintenance = { status = "actively-developed" }
[dependencies]
toml = "0.4"
toml-query = "0.7"
toml-query = { git = "https://github.com/matthiasbeyer/toml-query", branch = "failure" }
serde_derive = "1"
serde = "1"
error-chain = "0.12"
failure = "0.1"
failure_derive = "0.1"
libimagstore = { version = "0.9.0", path = "../../../lib/core/libimagstore" }
libimagerror = { version = "0.9.0", path = "../../../lib/core/libimagerror" }

View file

@ -17,9 +17,6 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
use error::Result;
use error::GPSErrorKind as GPSEK;
use error::ResultExt;
use types::*;
use libimagstore::store::Entry;
@ -27,6 +24,9 @@ use libimagstore::store::Entry;
use toml_query::read::TomlValueReadExt;
use toml_query::insert::TomlValueInsertExt;
use toml_query::delete::TomlValueDeleteExt;
use failure::ResultExt;
use failure::Fallible as Result;
use failure::Error;
pub trait GPSEntry {
@ -60,11 +60,11 @@ impl GPSEntry for Entry {
self.get_header_mut()
.insert("gps.coordinates", c.into())
.map(|_| ())
.chain_err(|| GPSEK::HeaderWriteError)
.map_err(Error::from)
}
fn get_coordinates(&self) -> Result<Option<Coordinates>> {
match self.get_header().read("gps.coordinates").chain_err(|| GPSEK::HeaderWriteError)? {
match self.get_header().read("gps.coordinates").map_err(Error::from)? {
Some(hdr) => Coordinates::from_value(hdr).map(Some),
None => Ok(None),
}
@ -88,7 +88,9 @@ impl GPSEntry for Entry {
let hdr = self.get_header_mut();
for pattern in patterns.iter() {
let _ = hdr.delete(pattern).chain_err(|| GPSEK::HeaderWriteError)?;
let _ = hdr.delete(pattern)
.map_err(Error::from)
.context("Error writing header")?;
}
match coordinates {

View file

@ -1,87 +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 {
GPSError, GPSErrorKind, ResultExt, Result;
}
errors {
StoreReadError {
description("Store read error")
display("Store read error")
}
StoreWriteError {
description("Store write error")
display("Store write error")
}
HeaderWriteError {
description("Couldn't write Header for annotation")
display("Couldn't write Header for annotation")
}
HeaderReadError {
description("Couldn't read Header of Entry")
display("Couldn't read Header of Entry")
}
HeaderTypeError {
description("Header field has unexpected type")
display("Header field has unexpected type")
}
TypeError {
description("Type Error")
display("Type Error")
}
DegreeMissing {
description("'degree' value missing")
display("'degree' value missing")
}
MinutesMissing {
description("'minutes' value missing")
display("'minutes' value missing")
}
SecondsMissing {
description("'seconds' value missing")
display("'seconds' value missing")
}
LongitudeMissing {
description("'longitude' value missing")
display("'longitude' value missing")
}
LatitudeMissing {
description("'latitude' value missing")
display("'latitude' value missing")
}
NumberConversionError {
description("Cannot convert number to fit into variable")
display("Cannot convert number to fit into variable")
}
}
}

View file

@ -36,7 +36,7 @@
extern crate toml;
extern crate toml_query;
#[macro_use] extern crate serde_derive;
#[macro_use] extern crate error_chain;
extern crate failure;
extern crate libimagstore;
extern crate libimagerror;
@ -45,6 +45,5 @@ extern crate libimagerror;
extern crate env_logger;
pub mod entry;
pub mod error;
pub mod types;

View file

@ -23,10 +23,11 @@ use std::fmt::Formatter;
use std::fmt::Result as FmtResult;
use toml::Value;
use failure::Fallible as Result;
use failure::err_msg;
use failure::Error;
use error::GPSErrorKind as GPSEK;
use error::GPSError as GPSE;
use error::Result;
use libimagerror::errors::ErrorMsg as EM;
pub trait FromValue : Sized {
fn from_value(v: &Value) -> Result<Self>;
@ -80,28 +81,28 @@ impl FromValue for GPSValue {
fn from_value(v: &Value) -> Result<Self> {
let int_to_appropriate_width = |v: &Value| {
v.as_integer()
.ok_or(GPSE::from_kind(GPSEK::HeaderTypeError))
.ok_or_else(|| Error::from(EM::EntryHeaderTypeError))
};
match *v {
Value::Table(ref map) => {
Ok(GPSValue::new(
map.get("degree")
.ok_or_else(|| GPSE::from_kind(GPSEK::DegreeMissing))
.ok_or_else(|| Error::from(err_msg("Degree missing")))
.and_then(&int_to_appropriate_width)?,
map
.get("minutes")
.ok_or_else(|| GPSE::from_kind(GPSEK::MinutesMissing))
.ok_or_else(|| Error::from(err_msg("Minutes missing")))
.and_then(&int_to_appropriate_width)?,
map
.get("seconds")
.ok_or_else(|| GPSE::from_kind(GPSEK::SecondsMissing))
.ok_or_else(|| Error::from(err_msg("Seconds missing")))
.and_then(&int_to_appropriate_width)?
))
}
_ => Err(GPSE::from_kind(GPSEK::TypeError))
_ => Err(Error::from(EM::EntryHeaderTypeError))
}
}
@ -151,15 +152,17 @@ impl Into<Value> for Coordinates {
impl FromValue for Coordinates {
fn from_value(v: &Value) -> Result<Self> {
v.as_table()
.ok_or(GPSE::from_kind(GPSEK::TypeError))
.ok_or_else(|| Error::from(EM::EntryHeaderTypeError))
.and_then(|t| {
let get = |m: &BTreeMap<_, _>, what: &'static str, ek| -> Result<GPSValue> {
m.get(what).ok_or(GPSE::from_kind(ek)).and_then(GPSValue::from_value)
m.get(what)
.ok_or_else(|| Error::from(err_msg(ek)))
.and_then(GPSValue::from_value)
};
Ok(Coordinates::new(
get(t, "longitude", GPSEK::LongitudeMissing)?,
get(t, "latitude", GPSEK::LatitudeMissing)?
get(t, "longitude", "Longitude missing")?,
get(t, "latitude", "Latitude missing")?
))
})
}