Move range type to own module
This commit is contained in:
parent
ac36e7aee8
commit
fb228c65b1
4 changed files with 93 additions and 18 deletions
|
@ -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<Option<Result<NaiveDateTime>>>;
|
||||
|
||||
fn delete_date_range(&mut self) -> Result<()>;
|
||||
fn read_date_range(&self) -> Result<DateRange>;
|
||||
fn set_date_range(&mut self, start: NaiveDateTime, end: NaiveDateTime) -> Result<Option<Result<DateRange>>>;
|
||||
fn read_date_range(&self) -> Result<DateTimeRange>;
|
||||
fn set_date_range(&mut self, start: NaiveDateTime, end: NaiveDateTime) -> Result<Option<Result<DateTimeRange>>>;
|
||||
|
||||
}
|
||||
|
||||
|
@ -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<DateRange> {
|
||||
fn read_date_range(&self) -> Result<DateTimeRange> {
|
||||
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::<NaiveDateTime>()
|
||||
|
@ -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::<NaiveDateTime>()
|
||||
|
@ -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<Option<Result<DateRange>>>
|
||||
-> Result<Option<Result<DateTimeRange>>>
|
||||
{
|
||||
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),
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -29,5 +29,6 @@ extern crate libimagutil;
|
|||
pub mod datepath;
|
||||
pub mod datetime;
|
||||
pub mod error;
|
||||
pub mod range;
|
||||
pub mod result;
|
||||
|
||||
|
|
68
libimagentrydatetime/src/range.rs
Normal file
68
libimagentrydatetime/src/range.rs
Normal file
|
@ -0,0 +1,68 @@
|
|||
//
|
||||
// imag - the personal information management suite for the commandline
|
||||
// Copyright (C) 2015, 2016 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 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<T> = RResult<T, DateTimeRangeError>;
|
||||
}
|
||||
|
||||
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<DateTimeRange> {
|
||||
use self::error::DateTimeRangeErrorKind as DTREK;
|
||||
if start < end {
|
||||
Ok(DateTimeRange(start, end))
|
||||
} else {
|
||||
Err(DTREK::EndDateTimeBeforeStartDateTime.into_error())
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in a new issue