Move common functionality to utility module

This commit is contained in:
Matthias Beyer 2018-01-04 20:08:10 +01:00
parent 6a2dce2b0f
commit 57cf5003bd
3 changed files with 22 additions and 31 deletions

View file

@ -30,6 +30,7 @@ use error::*;
use iter::HabitInstanceStoreIdIterator;
use util::date_to_string;
use util::IsHabitCheck;
use util::get_string_header_from_entry;
use libimagentrylink::internal::InternalLinker;
use libimagstore::store::Store;
@ -202,19 +203,19 @@ impl HabitTemplate for Entry {
}
fn habit_name(&self) -> Result<String> {
get_string_header_from_habit(self, "habit.template.name")
get_string_header_from_entry(self, "habit.template.name")
}
fn habit_basedate(&self) -> Result<String> {
get_string_header_from_habit(self, "habit.template.basedate")
get_string_header_from_entry(self, "habit.template.basedate")
}
fn habit_recur_spec(&self) -> Result<String> {
get_string_header_from_habit(self, "habit.template.recurspec")
get_string_header_from_entry(self, "habit.template.recurspec")
}
fn habit_comment(&self) -> Result<String> {
get_string_header_from_habit(self, "habit.template.comment")
get_string_header_from_entry(self, "habit.template.comment")
}
fn habit_until_date(&self) -> Result<Option<String>> {
@ -239,15 +240,6 @@ fn instance_id_for_name_and_datestr(habit_name: &String, habit_date: &String) ->
.map_err(HE::from)
}
#[inline]
fn get_string_header_from_habit(e: &Entry, path: &'static str) -> Result<String> {
match e.get_header().read(path)? {
Some(&Value::String(ref s)) => Ok(s.clone()),
Some(_) => Err(HEK::HeaderTypeError(path, "String").into()),
None => Err(HEK::HeaderFieldMissing(path).into()),
}
}
pub mod builder {
use toml::Value;
use toml_query::insert::TomlValueInsertExt;

View file

@ -22,7 +22,6 @@ use toml::Value;
use toml_query::read::TomlValueReadExt;
use toml_query::set::TomlValueSetExt;
use error::HabitErrorKind as HEK;
use error::*;
use util::*;
@ -61,11 +60,8 @@ impl HabitInstance for Entry {
}
fn get_date(&self) -> Result<NaiveDate> {
match self.get_header().read("habit.instance.date")? {
Some(&Value::String(ref s)) => date_from_string(s),
Some(_) => Err(HEK::HeaderTypeError("habit.instance.date", "String").into()),
None => Err(HEK::HeaderFieldMissing("habit.instance.date").into()),
}
use util::date_from_string;
get_string_header_from_entry(self, "habit.instance.date").and_then(date_from_string)
}
fn set_date(&mut self, n: &NaiveDate) -> Result<()> {
@ -77,11 +73,7 @@ impl HabitInstance for Entry {
}
fn get_comment(&self) -> Result<String> {
match self.get_header().read("habit.instance.comment")? {
Some(&Value::String(ref s)) => Ok(s.clone()),
Some(_) => Err(HEK::HeaderTypeError("habit.instance.comment", "String").into()),
None => Err(HEK::HeaderFieldMissing("habit.instance.comment").into()),
}
get_string_header_from_entry(self, "habit.instance.comment")
}
fn set_comment(&mut self, c: String) -> Result<()> {
@ -93,11 +85,7 @@ impl HabitInstance for Entry {
}
fn get_template_name(&self) -> Result<String> {
match self.get_header().read("habit.instance.name")? {
Some(&Value::String(ref s)) => Ok(s.clone()),
Some(_) => Err(HEK::HeaderTypeError("habit.instance.name", "String").into()),
None => Err(HEK::HeaderFieldMissing("habit.instance.name").into()),
}
get_string_header_from_entry(self, "habit.instance.name")
}
}

View file

@ -34,8 +34,8 @@ pub fn date_to_string(ndt: &NaiveDate) -> String {
ndt.format(NAIVE_DATE_STRING_FORMAT).to_string()
}
pub fn date_from_string(s: &str) -> Result<NaiveDate> {
NaiveDate::parse_from_str(s, NAIVE_DATE_STRING_FORMAT).map_err(From::from)
pub fn date_from_string(s: String) -> Result<NaiveDate> {
NaiveDate::parse_from_str(&s, NAIVE_DATE_STRING_FORMAT).map_err(From::from)
}
/// Helper trait to check whether a object which can be a habit instance and a habit template is
@ -90,3 +90,14 @@ impl IsHabitCheck for Entry {
}
}
#[inline]
pub fn get_string_header_from_entry(e: &Entry, path: &'static str) -> Result<String> {
use error::HabitErrorKind as HEK;
use toml_query::read::TomlValueReadExt;
e.get_header()
.read(path)?
.ok_or(HEK::HeaderFieldMissing(path).into())
.and_then(|o| o.as_str().map(String::from).ok_or(HEK::HeaderTypeError(path, "String").into()))
}