Fix: Until date is still optional

This commit is contained in:
Matthias Beyer 2017-12-22 13:20:11 +01:00
parent e2787c0087
commit 6246d25c9d

View file

@ -76,7 +76,7 @@ pub trait HabitTemplate : Sized {
fn habit_basedate(&self) -> Result<String>; fn habit_basedate(&self) -> Result<String>;
fn habit_recur_spec(&self) -> Result<String>; fn habit_recur_spec(&self) -> Result<String>;
fn habit_comment(&self) -> Result<String>; fn habit_comment(&self) -> Result<String>;
fn habit_until_date(&self) -> Result<String>; fn habit_until_date(&self) -> Result<Option<String>>;
/// Create a StoreId for a habit name and a date the habit should be instantiated for /// Create a StoreId for a habit name and a date the habit should be instantiated for
fn instance_id_for(habit_name: &String, habit_date: &NaiveDate) -> Result<StoreId>; fn instance_id_for(habit_name: &String, habit_date: &NaiveDate) -> Result<StoreId>;
@ -140,14 +140,17 @@ impl HabitTemplate for Entry {
let increment = date_from_s(self.habit_recur_spec()?)?; let increment = date_from_s(self.habit_recur_spec()?)?;
debug!("Increment is {:?}", increment); debug!("Increment is {:?}", increment);
let until = date_from_s(self.habit_until_date()?)? let until = self.habit_until_date()?.map(|s| -> Result<_> {
.calculate()? try!(date_from_s(s))
.get_moment() .calculate()?
.map(Clone::clone) .get_moment()
.ok_or_else(|| { .map(Clone::clone)
let kind : HEK = "until-date seems to have non-date value".to_owned().into(); .ok_or_else(|| {
HE::from_kind(kind) let kind : HEK = "until-date seems to have non-date value".to_owned().into();
})?; HE::from_kind(kind)
})
});
debug!("Until-Date is {:?}", basedate); debug!("Until-Date is {:?}", basedate);
for element in basedate.every(increment)? { for element in basedate.every(increment)? {
@ -157,8 +160,12 @@ impl HabitTemplate for Entry {
if let Some(ndt) = element.get_moment() { if let Some(ndt) = element.get_moment() {
if ndt >= base { if ndt >= base {
debug!("-> {:?} >= {:?}", ndt, base); debug!("-> {:?} >= {:?}", ndt, base);
if ndt > &until { if let Some(u) = until {
return Ok(None); if ndt > &(u?) {
return Ok(None);
} else {
return Ok(Some(ndt.date()));
}
} else { } else {
return Ok(Some(ndt.date())); return Ok(Some(ndt.date()));
} }
@ -212,8 +219,12 @@ impl HabitTemplate for Entry {
get_string_header_from_habit(self, "habit.template.comment") get_string_header_from_habit(self, "habit.template.comment")
} }
fn habit_until_date(&self) -> Result<String> { fn habit_until_date(&self) -> Result<Option<String>> {
get_string_header_from_habit(self, "habit.template.until") match self.get_header().read("habit.template.until")? {
Some(&Value::String(ref s)) => Ok(Some(s.clone())),
Some(_) => Err(HEK::HeaderTypeError("habit.template.until", "String").into()),
None => Ok(None),
}
} }
fn instance_id_for(habit_name: &String, habit_date: &NaiveDate) -> Result<StoreId> { fn instance_id_for(habit_name: &String, habit_date: &NaiveDate) -> Result<StoreId> {
@ -305,19 +316,18 @@ pub mod builder {
let recur = try!(self.recurspec.ok_or_else(|| mkerr("recurspec"))); let recur = try!(self.recurspec.ok_or_else(|| mkerr("recurspec")));
debug!("Success: Recurr spec present"); debug!("Success: Recurr spec present");
let until = try!(self.untildate.ok_or_else(|| mkerr("until-date"))); if let Some(until) = self.untildate {
debug!("Success: Until-Date present"); debug!("Success: Until-Date present");
if dateobj > until {
if dateobj > until { let e = HE::from_kind(HEK::HabitBuilderLogicError("until-date before start date"));
let e = HE::from_kind(HEK::HabitBuilderLogicError("until-date before start date")); return Err(e);
return Err(e); }
} }
if let Err(e) = ::kairos::parser::parse(&recur) { if let Err(e) = ::kairos::parser::parse(&recur) {
return Err(e).map_err(From::from); return Err(e).map_err(From::from);
} }
let date = date_to_string(&dateobj); let date = date_to_string(&dateobj);
let until = date_to_string(&until);
debug!("Success: Date valid"); debug!("Success: Date valid");
let comment = self.comment.unwrap_or_else(|| String::new()); let comment = self.comment.unwrap_or_else(|| String::new());
@ -330,7 +340,10 @@ pub mod builder {
try!(entry.get_header_mut().insert("habit.template.basedate", Value::String(date))); try!(entry.get_header_mut().insert("habit.template.basedate", Value::String(date)));
try!(entry.get_header_mut().insert("habit.template.recurspec", Value::String(recur))); try!(entry.get_header_mut().insert("habit.template.recurspec", Value::String(recur)));
try!(entry.get_header_mut().insert("habit.template.comment", Value::String(comment))); try!(entry.get_header_mut().insert("habit.template.comment", Value::String(comment)));
try!(entry.get_header_mut().insert("habit.template.until", Value::String(until))); if let Some(until) = self.untildate {
let until = date_to_string(&until);
try!(entry.get_header_mut().insert("habit.template.until", Value::String(until)));
}
debug!("Success: Created entry in store and set headers"); debug!("Success: Created entry in store and set headers");
Ok(entry) Ok(entry)