libimagentrydatetime: Rewrite error handling

This commit is contained in:
Matthias Beyer 2017-09-03 15:41:25 +02:00
parent 0b068df84e
commit d443b83b52
6 changed files with 39 additions and 61 deletions

View file

@ -29,7 +29,7 @@ use datepath::accuracy::Accuracy;
use datepath::format::Format;
use datepath::result::Result;
use datepath::error::DatePathCompilerErrorKind as DPCEK;
use datepath::error::MapErrInto;
use datepath::error::ResultExt;
pub struct DatePathCompiler {
accuracy : Accuracy,
@ -122,7 +122,7 @@ impl DatePathCompiler {
}
StoreId::new_baseless(PathBuf::from(s))
.map_err_into(DPCEK::StoreIdBuildFailed)
.chain_err(|| DPCEK::StoreIdBuildFailed)
}
}

View file

@ -17,6 +17,10 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
use std::error::Error;
use libimagerror::into::IntoError;
error_chain! {
types {
DatePathCompilerError, DatePathCompilerErrorKind, ResultExt, Result;
@ -36,8 +40,6 @@ error_chain! {
}
}
pub use self::error::DatePathCompilerError;
impl IntoError for DatePathCompilerErrorKind {
type Target = DatePathCompilerError;
@ -45,7 +47,7 @@ impl IntoError for DatePathCompilerErrorKind {
DatePathCompilerError::from_kind(self)
}
fn into_error_with_cause(self, cause: Box<Error>) -> Self::Target {
fn into_error_with_cause(self, _: Box<Error>) -> Self::Target {
DatePathCompilerError::from_kind(self)
}
}

View file

@ -56,17 +56,17 @@ impl EntryDate for Entry {
self.get_header_mut()
.delete(&DATE_HEADER_LOCATION)
.map(|_| ())
.map_err_into(DEK::DeleteDateError)
.chain_err(|| DEK::DeleteDateError)
}
fn read_date(&self) -> Result<NaiveDateTime> {
self.get_header()
.read(&DATE_HEADER_LOCATION)
.map_err_into(DEK::ReadDateError)
.chain_err(|| DEK::ReadDateError)
.and_then(|v| {
match v {
Some(&Value::String(ref s)) => s.parse::<NaiveDateTime>()
.map_err_into(DEK::DateTimeParsingError),
.chain_err(|| DEK::DateTimeParsingError),
Some(_) => Err(DEK::DateHeaderFieldTypeError.into_error()),
_ => Err(DEK::ReadDateError.into_error()),
}
@ -97,11 +97,11 @@ impl EntryDate for Entry {
.map(|opt| opt.map(|stri| {
match stri {
Value::String(ref s) => s.parse::<NaiveDateTime>()
.map_err_into(DEK::DateTimeParsingError),
.chain_err(|| DEK::DateTimeParsingError),
_ => Err(DEK::DateHeaderFieldTypeError.into_error()),
}
}))
.map_err_into(DEK::SetDateError)
.chain_err(|| DEK::SetDateError)
}
@ -117,23 +117,23 @@ impl EntryDate for Entry {
.get_header_mut()
.delete(&DATE_RANGE_START_HEADER_LOCATION)
.map(|_| ())
.map_err_into(DEK::DeleteDateTimeRangeError));
.chain_err(|| DEK::DeleteDateTimeRangeError));
self.get_header_mut()
.delete(&DATE_RANGE_END_HEADER_LOCATION)
.map(|_| ())
.map_err_into(DEK::DeleteDateTimeRangeError)
.chain_err(|| DEK::DeleteDateTimeRangeError)
}
fn read_date_range(&self) -> Result<DateTimeRange> {
let start = try!(self
.get_header()
.read(&DATE_RANGE_START_HEADER_LOCATION)
.map_err_into(DEK::ReadDateTimeRangeError)
.chain_err(|| DEK::ReadDateTimeRangeError)
.and_then(|v| {
match v {
Some(&Value::String(ref s)) => s.parse::<NaiveDateTime>()
.map_err_into(DEK::DateTimeParsingError),
.chain_err(|| DEK::DateTimeParsingError),
Some(_) => Err(DEK::DateHeaderFieldTypeError.into_error()),
_ => Err(DEK::ReadDateError.into_error()),
}
@ -142,18 +142,18 @@ impl EntryDate for Entry {
let end = try!(self
.get_header()
.read(&DATE_RANGE_START_HEADER_LOCATION)
.map_err_into(DEK::ReadDateTimeRangeError)
.chain_err(|| DEK::ReadDateTimeRangeError)
.and_then(|v| {
match v {
Some(&Value::String(ref s)) => s.parse::<NaiveDateTime>()
.map_err_into(DEK::DateTimeParsingError),
.chain_err(|| DEK::DateTimeParsingError),
Some(_) => Err(DEK::DateHeaderFieldTypeError.into_error()),
_ => Err(DEK::ReadDateError.into_error()),
}
}));
DateTimeRange::new(start, end)
.map_err_into(DEK::DateTimeRangeError)
.chain_err(|| DEK::DateTimeRangeError)
}
/// Set the date range
@ -175,11 +175,11 @@ impl EntryDate for Entry {
.map(|opt| opt.map(|stri| {
match stri {
Value::String(ref s) => s.parse::<NaiveDateTime>()
.map_err_into(DEK::DateTimeParsingError),
.chain_err(|| DEK::DateTimeParsingError),
_ => Err(DEK::DateHeaderFieldTypeError.into_error()),
}
}))
.map_err_into(DEK::SetDateTimeRangeError));
.chain_err(|| DEK::SetDateTimeRangeError));
let opt_old_end = try!(self
.get_header_mut()
@ -187,16 +187,16 @@ impl EntryDate for Entry {
.map(|opt| opt.map(|stri| {
match stri {
Value::String(ref s) => s.parse::<NaiveDateTime>()
.map_err_into(DEK::DateTimeParsingError),
.chain_err(|| DEK::DateTimeParsingError),
_ => Err(DEK::DateHeaderFieldTypeError.into_error()),
}
}))
.map_err_into(DEK::SetDateTimeRangeError));
.chain_err(|| DEK::SetDateTimeRangeError));
match (opt_old_start, opt_old_end) {
(Some(Ok(old_start)), Some(Ok(old_end))) => {
let dr = DateTimeRange::new(old_start, old_end)
.map_err_into(DEK::DateTimeRangeError);
.chain_err(|| DEK::DateTimeRangeError);
Ok(Some(dr))
},

View file

@ -17,6 +17,10 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
use std::error::Error;
use libimagerror::into::IntoError;
error_chain! {
types {
DateError, DateErrorKind, ResultExt, Result;
@ -68,12 +72,12 @@ error_chain! {
display("Error parsing DateTime")
}
EndDateTimeBeforeStartDateTime {
description("End datetime is before start datetime")
display("End datetime is before start datetime")
}
}
}
pub use self::error::DateError;
pub use self::error::DateErrorKind;
pub use self::error::MapErrInto;
impl IntoError for DateErrorKind {
type Target = DateError;
@ -82,7 +86,7 @@ impl IntoError for DateErrorKind {
DateError::from_kind(self)
}
fn into_error_with_cause(self, cause: Box<Error>) -> Self::Target {
fn into_error_with_cause(self, _: Box<Error>) -> Self::Target {
DateError::from_kind(self)
}
}

View file

@ -41,7 +41,7 @@ extern crate toml_query;
extern crate toml;
#[macro_use] extern crate error_chain;
#[macro_use] extern crate libimagerror;
extern crate libimagerror;
extern crate libimagstore;
pub mod datepath;

View file

@ -17,39 +17,12 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
/// Error types for range module
error_chain! {
types {
DateTimeRangeError, DateTimeRangeErrorKind, ResultExt, Result;
}
errors {
EndDateTimeBeforeStartDateTime {
description("End datetime is before start datetime")
display("End datetime is before start datetime")
}
}
}
pub use self::error::DateTimeRangeError;
pub use self::error::DateTimeRangeErrorKind;
pub use self::error::MapErrInto;
impl IntoError for DateTimeRangeErrorKind {
type Target = DateTimeRangeError;
fn into_error(self) -> Self::Target {
DateTimeRangeError::from_kind(self)
}
fn into_error_with_cause(self, cause: Box<Error>) -> Self::Target {
DateTimeRangeError::from_kind(self)
}
}
use chrono::naive::NaiveDateTime;
use error::DateErrorKind as DEK;
use error::Result;
use libimagerror::into::IntoError;
use self::result::Result;
/// A Range between two dates
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
@ -65,11 +38,10 @@ impl DateTimeRange {
/// else Err(DateTimeRangeError)
///
pub fn new(start: NaiveDateTime, end: NaiveDateTime) -> Result<DateTimeRange> {
use self::error::DateTimeRangeErrorKind as DTREK;
if start < end {
Ok(DateTimeRange(start, end))
} else {
Err(DTREK::EndDateTimeBeforeStartDateTime.into_error())
Err(DEK::EndDateTimeBeforeStartDateTime.into_error())
}
}