libimagentrydatetime: 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 f3e7f677b0
commit a1c65603dc
9 changed files with 45 additions and 152 deletions

View File

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

View File

@ -27,9 +27,7 @@ use libimagstore::storeid::StoreId;
use datepath::accuracy::Accuracy;
use datepath::format::Format;
use datepath::error::Result;
use datepath::error::DatePathCompilerErrorKind as DPCEK;
use datepath::error::ResultExt;
use failure::Fallible as Result;
pub struct DatePathCompiler {
accuracy : Accuracy,
@ -122,7 +120,6 @@ impl DatePathCompiler {
}
StoreId::new_baseless(PathBuf::from(s))
.chain_err(|| DPCEK::StoreIdBuildFailed)
}
}

View File

@ -1,38 +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 {
DatePathCompilerError, DatePathCompilerErrorKind, ResultExt, Result;
}
errors {
UnknownDatePathCompilerError {
description("Unknown DatePathCompiler error")
display("Unknown DatePathCompiler error")
}
StoreIdBuildFailed {
description("Failed building StoreId object")
display("Failed building StoreId object")
}
}
}

View File

@ -19,7 +19,6 @@
pub mod accuracy;
pub mod compiler;
pub mod error;
pub mod format;
pub mod to_store_id;

View File

@ -18,9 +18,9 @@
//
use chrono::naive::NaiveDateTime;
use failure::Fallible as Result;
use libimagstore::storeid::StoreId;
use datepath::error::Result;
use datepath::compiler::DatePathCompiler;
//

View File

@ -24,10 +24,12 @@ use toml_query::read::TomlValueReadTypeExt;
use toml::Value;
use libimagstore::store::Entry;
use libimagerror::errors::ErrorMsg as EM;
use error::DateErrorKind as DEK;
use error::DateError as DE;
use error::*;
use failure::Error;
use failure::Fallible as Result;
use failure::ResultExt;
use failure::err_msg;
use range::DateTimeRange;
pub trait EntryDate {
@ -55,16 +57,18 @@ impl EntryDate for Entry {
self.get_header_mut()
.delete(&DATE_HEADER_LOCATION)
.map(|_| ())
.chain_err(|| DEK::DeleteDateError)
.context("Delete date error")
.map_err(Error::from)
}
fn read_date(&self) -> Result<NaiveDateTime> {
self.get_header()
.read_string(&DATE_HEADER_LOCATION)
.chain_err(|| DEK::ReadDateError)?
.ok_or(DE::from_kind(DEK::ReadDateError))?
.context("Error while reading date")?
.ok_or_else(|| err_msg("Error reading date"))?
.parse::<NaiveDateTime>()
.chain_err(|| DEK::DateTimeParsingError)
.context("Datetime parse error")
.map_err(Error::from)
}
/// Set a Date for this entry
@ -88,13 +92,16 @@ impl EntryDate for Entry {
self.get_header_mut()
.insert(&DATE_HEADER_LOCATION, Value::String(date))
.map_err(Error::from)
.map(|opt| opt.map(|stri| {
stri.as_str()
.ok_or(DE::from_kind(DEK::DateHeaderFieldTypeError))?
.ok_or_else(|| Error::from(EM::EntryHeaderTypeError))?
.parse::<NaiveDateTime>()
.chain_err(|| DEK::DateTimeParsingError)
.context("Datetime parse error")
.map_err(Error::from)
}))
.chain_err(|| DEK::SetDateError)
.context("Error setting date")
.map_err(Error::from)
}
@ -110,30 +117,33 @@ impl EntryDate for Entry {
.get_header_mut()
.delete(&DATE_RANGE_START_HEADER_LOCATION)
.map(|_| ())
.chain_err(|| DEK::DeleteDateTimeRangeError)?;
.context("Delete Datetime range error")?;
self.get_header_mut()
.delete(&DATE_RANGE_END_HEADER_LOCATION)
.map(|_| ())
.chain_err(|| DEK::DeleteDateTimeRangeError)
.context("Delete Datetime range error")
.map_err(Error::from)
}
fn read_date_range(&self) -> Result<DateTimeRange> {
let start = self
.get_header()
.read_string(&DATE_RANGE_START_HEADER_LOCATION)
.chain_err(|| DEK::ReadDateTimeRangeError)?
.ok_or_else(|| DE::from_kind(DEK::ReadDateError))
.context("Error while reading Datetime range")?
.ok_or_else(|| err_msg("Error reading date"))
.and_then(str_to_ndt)?;
let end = self
.get_header()
.read_string(&DATE_RANGE_START_HEADER_LOCATION)
.chain_err(|| DEK::ReadDateTimeRangeError)?
.ok_or_else(|| DE::from_kind(DEK::ReadDateError))
.context("Error reading Datetime range")?
.ok_or_else(|| err_msg("Error reading date"))
.and_then(str_to_ndt)?;
DateTimeRange::new(start, end).chain_err(|| DEK::DateTimeRangeError)
DateTimeRange::new(start, end)
.context("Datetime Range error")
.map_err(Error::from)
}
/// Set the date range
@ -153,18 +163,19 @@ impl EntryDate for Entry {
.get_header_mut()
.insert(&DATE_RANGE_START_HEADER_LOCATION, Value::String(start))
.map(|opt| opt.as_ref().map(val_to_ndt))
.chain_err(|| DEK::SetDateTimeRangeError)?;
.context("Error setting Datetime range")?;
let opt_old_end = self
.get_header_mut()
.insert(&DATE_RANGE_END_HEADER_LOCATION, Value::String(end))
.map(|opt| opt.as_ref().map(val_to_ndt))
.chain_err(|| DEK::SetDateTimeRangeError)?;
.context("Error setting Datetime range")?;
match (opt_old_start, opt_old_end) {
(Some(Ok(old_start)), Some(Ok(old_end))) => {
let dr = DateTimeRange::new(old_start, old_end)
.chain_err(|| DEK::DateTimeRangeError);
.context("Error processing Datetime range")
.map_err(Error::from);
Ok(Some(dr))
},
@ -181,15 +192,18 @@ impl EntryDate for Entry {
#[inline]
fn str_to_ndt(v: String) -> Result<NaiveDateTime> {
v.parse::<NaiveDateTime>().chain_err(|| DEK::DateTimeParsingError)
v.parse::<NaiveDateTime>()
.context("Error parsing Datetime")
.map_err(Error::from)
}
#[inline]
fn val_to_ndt(v: &Value) -> Result<NaiveDateTime> {
v.as_str()
.ok_or(DE::from_kind(DEK::DateHeaderFieldTypeError))?
.ok_or_else(|| Error::from(EM::EntryHeaderTypeError))?
.parse::<NaiveDateTime>()
.chain_err(|| DEK::DateTimeParsingError)
.context("Datetime parsing error")
.map_err(Error::from)
}
#[cfg(test)]

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 {
DateError, DateErrorKind, ResultExt, Result;
}
errors {
DeleteDateError {
description("Error deleting date")
display("Error deleting date")
}
ReadDateError {
description("Error reading date")
display("Error reading date")
}
SetDateError {
description("Error setting date")
display("Error setting date")
}
DeleteDateTimeRangeError {
description("Error deleting date-time range")
display("Error deleting date-time range")
}
ReadDateTimeRangeError {
description("Error reading date-time range")
display("Error reading date-time range")
}
SetDateTimeRangeError {
description("Error setting date-time range")
display("Error setting date-time range")
}
DateTimeRangeError {
description("DateTime Range error")
display("DateTime Range error")
}
DateHeaderFieldTypeError {
description("Expected the header field in the entry to have type 'String', but have other type")
display("Expected the header field in the entry to have type 'String', but have other type")
}
DateTimeParsingError {
description("Error parsing DateTime")
display("Error parsing DateTime")
}
EndDateTimeBeforeStartDateTime {
description("End datetime is before start datetime")
display("End datetime is before start datetime")
}
}
}

View File

@ -39,13 +39,12 @@
extern crate chrono;
extern crate toml_query;
extern crate toml;
#[macro_use] extern crate error_chain;
extern crate failure;
extern crate libimagerror;
extern crate libimagstore;
pub mod datepath;
pub mod datetime;
pub mod error;
pub mod range;

View File

@ -19,9 +19,8 @@
use chrono::naive::NaiveDateTime;
use error::DateErrorKind as DEK;
use error::DateError as DE;
use error::Result;
use failure::Fallible as Result;
use failure::err_msg;
/// A Range between two dates
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
@ -40,7 +39,7 @@ impl DateTimeRange {
if start < end {
Ok(DateTimeRange(start, end))
} else {
Err(DE::from_kind(DEK::EndDateTimeBeforeStartDateTime))
Err(err_msg("End date before start date"))
}
}