From ecba4e670fff317b319b072cce7c1a50bf2802f9 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 7 Jun 2016 20:20:27 +0200 Subject: [PATCH] Add Diary::new_entry_by_id() to generate a new entry from a DiaryId object. Rewrite Diary::new_entry_today() to use this new functionality for more DRY code. --- libimagdiary/src/diary.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/libimagdiary/src/diary.rs b/libimagdiary/src/diary.rs index 952c9900..a9939d56 100644 --- a/libimagdiary/src/diary.rs +++ b/libimagdiary/src/diary.rs @@ -34,13 +34,14 @@ impl<'a> Diary<'a> { // create or get a new entry for today pub fn new_entry_today(&self) -> Result { - let dt = Local::now(); + let dt = Local::now(); let ndt = dt.naive_local(); + let id = DiaryId::new(String::from(self.name), ndt.year(), ndt.month(), ndt.day(), 0, 0); + self.new_entry_by_id(id) + } - // Currenty we only have support for per-day entries - let id = DiaryId::new(String::from(self.name), ndt.year(), ndt.month(), ndt.day(), 0, 0); - - self.retrieve(id) + pub fn new_entry_by_id(&self, id: DiaryId) -> Result { + self.retrieve(id.with_diary_name(String::from(self.name))) } pub fn retrieve(&self, id: DiaryId) -> Result {