libimagentrydatetime: Replace read with typed read

This commit is contained in:
Matthias Beyer 2018-01-12 16:32:18 +01:00
parent 7176c130ff
commit 6c36c97895

View file

@ -20,7 +20,7 @@
use chrono::naive::NaiveDateTime; use chrono::naive::NaiveDateTime;
use toml_query::delete::TomlValueDeleteExt; use toml_query::delete::TomlValueDeleteExt;
use toml_query::insert::TomlValueInsertExt; use toml_query::insert::TomlValueInsertExt;
use toml_query::read::TomlValueReadExt; use toml_query::read::TomlValueReadTypeExt;
use toml::Value; use toml::Value;
use libimagstore::store::Entry; use libimagstore::store::Entry;
@ -60,15 +60,11 @@ impl EntryDate for Entry {
fn read_date(&self) -> Result<NaiveDateTime> { fn read_date(&self) -> Result<NaiveDateTime> {
self.get_header() self.get_header()
.read(&DATE_HEADER_LOCATION) .read_string(&DATE_HEADER_LOCATION)
.chain_err(|| DEK::ReadDateError) .chain_err(|| DEK::ReadDateError)?
.and_then(|v| { .ok_or(DE::from_kind(DEK::ReadDateError))?
v.ok_or(DE::from_kind(DEK::ReadDateError))? .parse::<NaiveDateTime>()
.as_str() .chain_err(|| DEK::DateTimeParsingError)
.ok_or(DE::from_kind(DEK::DateHeaderFieldTypeError))?
.parse::<NaiveDateTime>()
.chain_err(|| DEK::DateTimeParsingError)
})
} }
/// Set a Date for this entry /// Set a Date for this entry
@ -125,15 +121,17 @@ impl EntryDate for Entry {
fn read_date_range(&self) -> Result<DateTimeRange> { fn read_date_range(&self) -> Result<DateTimeRange> {
let start = self let start = self
.get_header() .get_header()
.read(&DATE_RANGE_START_HEADER_LOCATION) .read_string(&DATE_RANGE_START_HEADER_LOCATION)
.chain_err(|| DEK::ReadDateTimeRangeError) .chain_err(|| DEK::ReadDateTimeRangeError)?
.and_then(|v| str_to_ndt(v.ok_or(DE::from_kind(DEK::ReadDateError))?))?; .ok_or_else(|| DE::from_kind(DEK::ReadDateError))
.and_then(str_to_ndt)?;
let end = self let end = self
.get_header() .get_header()
.read(&DATE_RANGE_START_HEADER_LOCATION) .read_string(&DATE_RANGE_START_HEADER_LOCATION)
.chain_err(|| DEK::ReadDateTimeRangeError) .chain_err(|| DEK::ReadDateTimeRangeError)?
.and_then(|v| str_to_ndt(v.ok_or(DE::from_kind(DEK::ReadDateError))?))?; .ok_or_else(|| DE::from_kind(DEK::ReadDateError))
.and_then(str_to_ndt)?;
DateTimeRange::new(start, end).chain_err(|| DEK::DateTimeRangeError) DateTimeRange::new(start, end).chain_err(|| DEK::DateTimeRangeError)
} }
@ -154,13 +152,13 @@ impl EntryDate for Entry {
let opt_old_start = self let opt_old_start = self
.get_header_mut() .get_header_mut()
.insert(&DATE_RANGE_START_HEADER_LOCATION, Value::String(start)) .insert(&DATE_RANGE_START_HEADER_LOCATION, Value::String(start))
.map(|opt| opt.as_ref().map(str_to_ndt)) .map(|opt| opt.as_ref().map(val_to_ndt))
.chain_err(|| DEK::SetDateTimeRangeError)?; .chain_err(|| DEK::SetDateTimeRangeError)?;
let opt_old_end = self let opt_old_end = self
.get_header_mut() .get_header_mut()
.insert(&DATE_RANGE_END_HEADER_LOCATION, Value::String(end)) .insert(&DATE_RANGE_END_HEADER_LOCATION, Value::String(end))
.map(|opt| opt.as_ref().map(str_to_ndt)) .map(|opt| opt.as_ref().map(val_to_ndt))
.chain_err(|| DEK::SetDateTimeRangeError)?; .chain_err(|| DEK::SetDateTimeRangeError)?;
match (opt_old_start, opt_old_end) { match (opt_old_start, opt_old_end) {
@ -181,7 +179,13 @@ impl EntryDate for Entry {
} }
fn str_to_ndt(v: &Value) -> Result<NaiveDateTime> { #[inline]
fn str_to_ndt(v: String) -> Result<NaiveDateTime> {
v.parse::<NaiveDateTime>().chain_err(|| DEK::DateTimeParsingError)
}
#[inline]
fn val_to_ndt(v: &Value) -> Result<NaiveDateTime> {
v.as_str() v.as_str()
.ok_or(DE::from_kind(DEK::DateHeaderFieldTypeError))? .ok_or(DE::from_kind(DEK::DateHeaderFieldTypeError))?
.parse::<NaiveDateTime>() .parse::<NaiveDateTime>()
@ -191,6 +195,7 @@ fn str_to_ndt(v: &Value) -> Result<NaiveDateTime> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::path::PathBuf; use std::path::PathBuf;
use toml_query::read::TomlValueReadExt;
use super::*; use super::*;