From ab28cf4fd7f4be0759a132875e2ebdc0e603ad92 Mon Sep 17 00:00:00 2001 From: xeruf <27jf@pm.me> Date: Tue, 23 Jul 2024 13:45:25 +0300 Subject: [PATCH] feat: return Event from task state updates --- src/task.rs | 8 +++++--- src/tasks.rs | 16 ++++++++-------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/task.rs b/src/task.rs index 6575b60..47c2f81 100644 --- a/src/task.rs +++ b/src/task.rs @@ -65,12 +65,14 @@ impl Task { self.state().map_or(State::Open, |s| s.state) } - pub(crate) fn update_state(&mut self, state: State, comment: &str) { - self.props.push(make_event( + pub(crate) fn update_state(&mut self, state: State, comment: &str) -> Event { + let event = make_event( state.kind(), comment, &[Tag::event(self.event.id)], - )) + ); + self.props.push(event.clone()); + event } fn default_state(&self) -> TaskState { diff --git a/src/tasks.rs b/src/tasks.rs index 5399323..3b22e35 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -178,24 +178,24 @@ impl Tasks { } } - pub(crate) fn update_state_for(&mut self, id: &EventId, comment: &str, f: F) + pub(crate) fn update_state_for(&mut self, id: &EventId, comment: &str, f: F) -> Option where F: FnOnce(&Task) -> Option, { - self.tasks.get_mut(id).map(|t| { + self.tasks.get_mut(id).and_then(|t| { f(t).map(|s| { - t.update_state(s, comment); + t.update_state(s, comment) }) - }); + }) } - pub(crate) fn update_state(&mut self, comment: &str, f: F) + pub(crate) fn update_state(&mut self, comment: &str, f: F) -> Option where F: FnOnce(&Task) -> Option, { - self.position.inspect(|id| { - self.update_state_for(id, comment, f); - }); + self.position.and_then(|id| { + self.update_state_for(&id, comment, f) + }) } }