From 2232f0153fe36460938b12a7fb3c5e91ac6add20 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 22 Mar 2018 13:54:15 +0100 Subject: [PATCH] Dedup code --- lib/domain/libimaghabit/src/habit.rs | 38 +++++++++++++--------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/lib/domain/libimaghabit/src/habit.rs b/lib/domain/libimaghabit/src/habit.rs index f5d984fb..ea8ed7d4 100644 --- a/lib/domain/libimaghabit/src/habit.rs +++ b/lib/domain/libimaghabit/src/habit.rs @@ -102,16 +102,7 @@ impl HabitTemplate for Entry { store.create(id) .map_err(From::from) - .and_then(|mut entry| { - { - let _ = entry.set_isflag::()?; - let hdr = entry.get_header_mut(); - let _ = hdr.insert("habit.instance.name", Value::String(name))?; - let _ = hdr.insert("habit.instance.date", Value::String(date))?; - let _ = hdr.insert("habit.instance.comment", Value::String(comment))?; - } - Ok(entry) - }) + .and_then(|entry| postprocess_instance(entry, name, date, comment)) } fn create_instance_today<'a>(&self, store: &'a Store) -> Result> { @@ -126,16 +117,7 @@ impl HabitTemplate for Entry { store.create(id) .map_err(From::from) - .and_then(|mut entry| { - { - let _ = entry.set_isflag::()?; - let hdr = entry.get_header_mut(); - let _ = hdr.insert("habit.instance.name", Value::String(name))?; - let _ = hdr.insert("habit.instance.date", Value::String(date))?; - let _ = hdr.insert("habit.instance.comment", Value::String(comment))?; - } - Ok(entry) - }) + .and_then(|entry| postprocess_instance(entry, name, date, comment)) } fn retrieve_instance_today<'a>(&self, store: &'a Store) -> Result> { @@ -394,3 +376,19 @@ pub mod builder { } +fn postprocess_instance<'a>(mut entry: FileLockEntry<'a>, + name: String, + date: String, + comment: String) + -> Result> +{ + { + let _ = entry.set_isflag::()?; + let hdr = entry.get_header_mut(); + let _ = hdr.insert("habit.instance.name", Value::String(name))?; + let _ = hdr.insert("habit.instance.date", Value::String(date))?; + let _ = hdr.insert("habit.instance.comment", Value::String(comment))?; + } + Ok(entry) +} +