From fb228c65b12edcb9c41a9713dde8141d62294e96 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 6 Jun 2017 19:28:00 +0200 Subject: [PATCH] Move range type to own module --- libimagentrydatetime/src/datetime.rs | 34 ++++++++------ libimagentrydatetime/src/error.rs | 8 ++-- libimagentrydatetime/src/lib.rs | 1 + libimagentrydatetime/src/range.rs | 68 ++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 18 deletions(-) create mode 100644 libimagentrydatetime/src/range.rs diff --git a/libimagentrydatetime/src/datetime.rs b/libimagentrydatetime/src/datetime.rs index d073e5e2..244aa7a5 100644 --- a/libimagentrydatetime/src/datetime.rs +++ b/libimagentrydatetime/src/datetime.rs @@ -29,9 +29,7 @@ use libimagerror::into::IntoError; use error::DateErrorKind as DEK; use error::*; use result::Result; - -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct DateRange(NaiveDateTime, NaiveDateTime); +use range::DateTimeRange; pub trait EntryDate { @@ -40,8 +38,8 @@ pub trait EntryDate { fn set_date(&mut self, d: NaiveDateTime) -> Result>>; fn delete_date_range(&mut self) -> Result<()>; - fn read_date_range(&self) -> Result; - fn set_date_range(&mut self, start: NaiveDateTime, end: NaiveDateTime) -> Result>>; + fn read_date_range(&self) -> Result; + fn set_date_range(&mut self, start: NaiveDateTime, end: NaiveDateTime) -> Result>>; } @@ -118,19 +116,19 @@ impl EntryDate for Entry { .get_header_mut() .delete(&DATE_RANGE_START_HEADER_LOCATION) .map(|_| ()) - .map_err_into(DEK::DeleteDateRangeError)); + .map_err_into(DEK::DeleteDateTimeRangeError)); self.get_header_mut() .delete(&DATE_RANGE_END_HEADER_LOCATION) .map(|_| ()) - .map_err_into(DEK::DeleteDateRangeError) + .map_err_into(DEK::DeleteDateTimeRangeError) } - fn read_date_range(&self) -> Result { + fn read_date_range(&self) -> Result { let start = try!(self .get_header() .read(&DATE_RANGE_START_HEADER_LOCATION) - .map_err_into(DEK::ReadDateRangeError) + .map_err_into(DEK::ReadDateTimeRangeError) .and_then(|v| { match v { &Value::String(ref s) => s.parse::() @@ -142,7 +140,7 @@ impl EntryDate for Entry { let end = try!(self .get_header() .read(&DATE_RANGE_START_HEADER_LOCATION) - .map_err_into(DEK::ReadDateRangeError) + .map_err_into(DEK::ReadDateTimeRangeError) .and_then(|v| { match v { &Value::String(ref s) => s.parse::() @@ -151,7 +149,8 @@ impl EntryDate for Entry { } })); - Ok(DateRange(start, end)) + DateTimeRange::new(start, end) + .map_err_into(DEK::DateTimeRangeError) } /// Set the date range @@ -162,7 +161,7 @@ impl EntryDate for Entry { /// header in an inconsistent state. /// fn set_date_range(&mut self, start: NaiveDateTime, end: NaiveDateTime) - -> Result>> + -> Result>> { let start = start.format(&DATE_FMT).to_string(); let end = end.format(&DATE_FMT).to_string(); @@ -177,7 +176,7 @@ impl EntryDate for Entry { _ => Err(DEK::DateHeaderFieldTypeError.into_error()), } })) - .map_err_into(DEK::SetDateRangeError)); + .map_err_into(DEK::SetDateTimeRangeError)); let opt_old_end = try!(self .get_header_mut() @@ -189,10 +188,15 @@ impl EntryDate for Entry { _ => Err(DEK::DateHeaderFieldTypeError.into_error()), } })) - .map_err_into(DEK::SetDateRangeError)); + .map_err_into(DEK::SetDateTimeRangeError)); match (opt_old_start, opt_old_end) { - (Some(Ok(old_start)), Some(Ok(old_end))) => Ok(Some(Ok(DateRange(old_start, old_end)))), + (Some(Ok(old_start)), Some(Ok(old_end))) => { + let dr = DateTimeRange::new(old_start, old_end) + .map_err_into(DEK::DateTimeRangeError); + + Ok(Some(dr)) + }, (Some(Err(e)), _) => Err(e), (_, Some(Err(e))) => Err(e), diff --git a/libimagentrydatetime/src/error.rs b/libimagentrydatetime/src/error.rs index 1147be93..0f0166a0 100644 --- a/libimagentrydatetime/src/error.rs +++ b/libimagentrydatetime/src/error.rs @@ -22,9 +22,11 @@ generate_error_module!( DeleteDateError => "Error deleting date", ReadDateError => "Error reading date", SetDateError => "Error setting date", - DeleteDateRangeError => "Error deleting date range", - ReadDateRangeError => "Error reading date range", - SetDateRangeError => "Error setting date range", + DeleteDateTimeRangeError => "Error deleting date-time range", + ReadDateTimeRangeError => "Error reading date-time range", + SetDateTimeRangeError => "Error setting date-time range", + + DateTimeRangeError => "DateTime Range error", DateHeaderFieldTypeError => "Expected the header field in the entry to have type 'String', but have other type", DateTimeParsingError => "Error parsing DateTime" diff --git a/libimagentrydatetime/src/lib.rs b/libimagentrydatetime/src/lib.rs index 89b08bcf..0e1a4006 100644 --- a/libimagentrydatetime/src/lib.rs +++ b/libimagentrydatetime/src/lib.rs @@ -29,5 +29,6 @@ extern crate libimagutil; pub mod datepath; pub mod datetime; pub mod error; +pub mod range; pub mod result; diff --git a/libimagentrydatetime/src/range.rs b/libimagentrydatetime/src/range.rs new file mode 100644 index 00000000..017ee2fc --- /dev/null +++ b/libimagentrydatetime/src/range.rs @@ -0,0 +1,68 @@ +// +// imag - the personal information management suite for the commandline +// Copyright (C) 2015, 2016 Matthias Beyer 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 types for range module +pub mod error { + generate_error_module!( + generate_error_types!(DateTimeRangeError, DateTimeRangeErrorKind, + EndDateTimeBeforeStartDateTime => "End datetime is before start datetime" + ); + ); + + pub use self::error::DateTimeRangeError; + pub use self::error::DateTimeRangeErrorKind; + pub use self::error::MapErrInto; +} + +/// Result type for range module +pub mod result { + use std::result::Result as RResult; + use super::error::DateTimeRangeError; + + pub type Result = RResult; +} + +use chrono::naive::datetime::NaiveDateTime; +use libimagerror::into::IntoError; +use self::result::Result; + +/// A Range between two dates +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct DateTimeRange(NaiveDateTime, NaiveDateTime); + +impl DateTimeRange { + + /// Create a new DateTimeRange object + /// + /// # Return value + /// + /// Ok(DateTimeRange) if start is before end, + /// else Err(DateTimeRangeError) + /// + pub fn new(start: NaiveDateTime, end: NaiveDateTime) -> Result { + use self::error::DateTimeRangeErrorKind as DTREK; + if start < end { + Ok(DateTimeRange(start, end)) + } else { + Err(DTREK::EndDateTimeBeforeStartDateTime.into_error()) + } + } + +} +