Change HabitInstance implementation to not contain comment in instance

This patch changes the HabitInstance implementation so that the comment
is not expected to exist in the instance object itself.

This is done for less data duplication.

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
This commit is contained in:
Matthias Beyer 2019-06-30 11:24:06 +02:00
parent 57039654ca
commit 191b535479

View file

@ -17,16 +17,21 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// //
use std::ops::Deref;
use chrono::NaiveDate; use chrono::NaiveDate;
use toml::Value; use toml::Value;
use toml_query::set::TomlValueSetExt; use toml_query::set::TomlValueSetExt;
use failure::Fallible as Result; use failure::Fallible as Result;
use crate::util::*; use crate::util::*;
use crate::habit::HabitTemplate;
use libimagstore::store::Entry; use libimagstore::store::Entry;
use libimagstore::store::Store;
use libimagentryutil::isa::Is; use libimagentryutil::isa::Is;
use libimagentryutil::isa::IsKindHeaderPathProvider; use libimagentryutil::isa::IsKindHeaderPathProvider;
use libimagentrylink::linkable::Linkable;
/// An instance of a habit is created for each time a habit is done. /// An instance of a habit is created for each time a habit is done.
/// ///
@ -41,8 +46,7 @@ pub trait HabitInstance {
fn get_date(&self) -> Result<NaiveDate>; fn get_date(&self) -> Result<NaiveDate>;
fn set_date(&mut self, n: &NaiveDate) -> Result<()>; fn set_date(&mut self, n: &NaiveDate) -> Result<()>;
fn get_comment(&self) -> Result<String>; fn get_comment(&self, store: &Store) -> Result<String>;
fn set_comment(&mut self, c: String) -> Result<()>;
fn get_template_name(&self) -> Result<String>; fn get_template_name(&self) -> Result<String>;
} }
@ -68,16 +72,37 @@ impl HabitInstance for Entry {
.map(|_| ()) .map(|_| ())
} }
fn get_comment(&self) -> Result<String> { /// Iterates all internal links, finds the template for this instance and gets the comment from
get_string_header_from_entry(self, "habit.instance.comment") /// it
///
///
/// # Warning
///
/// Internally tries to `Store::get()` the template entry. If this entry is borrowed outside of
/// this function, this fails.
///
/// If multiple templates are linked to this entry, this returns the comment of the first
///
///
/// # Return
///
/// Returns the Comment string from the first template that is linked to this instance.
/// If this is not an instance, this might misbehave.
/// If there is no template linked, this returns an error.
///
fn get_comment(&self, store: &Store) -> Result<String> {
let templ_name = self.get_template_name()?;
for link in self.links()? {
let template = store.get(link.get_store_id().clone())?.ok_or_else(|| {
format_err!("Entry {} is linked to {}, but that entry does not exist",
self.get_location(),
link.get_store_id())
})?;
if HabitTemplate::is_habit_template(template.deref())? && template.habit_name()? == templ_name {
return template.habit_comment()
} }
}
fn set_comment(&mut self, c: String) -> Result<()> { Err(format_err!("Cannot find template entry for {}", self.get_location()))
// Using `set` here because when creating the entry, these headers should be made present.
self.get_header_mut()
.set("habit.instance.comment", Value::String(c))
.map_err(From::from)
.map(|_| ())
} }
fn get_template_name(&self) -> Result<String> { fn get_template_name(&self) -> Result<String> {