From 571822f8eef9155934d973f25cac2983eddf48cb Mon Sep 17 00:00:00 2001 From: simonsan <14062932+simonsan@users.noreply.github.com> Date: Sat, 2 Mar 2024 16:10:05 +0100 Subject: [PATCH] refactor: reimplement logic to end and activity for in-memory storage to make it easier for error handling Signed-off-by: simonsan <14062932+simonsan@users.noreply.github.com> --- crates/core/src/error.rs | 2 +- crates/core/src/storage/in_memory.rs | 32 ++++++++++++++-------------- src/commands/resume.rs | 4 ++-- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/crates/core/src/error.rs b/crates/core/src/error.rs index a52b7f3d..967443bd 100644 --- a/crates/core/src/error.rs +++ b/crates/core/src/error.rs @@ -96,7 +96,7 @@ pub enum PaceErrorKind { #[error(transparent)] ChronoParse(#[from] chrono::ParseError), - /// Chrono duration is negative: {0} + /// Time chosen is not valid, because it lays before the current activity's beginning: {0} #[error(transparent)] ChronoDurationIsNegative(#[from] chrono::OutOfRangeError), diff --git a/crates/core/src/storage/in_memory.rs b/crates/core/src/storage/in_memory.rs index 73f50910..b77d897d 100644 --- a/crates/core/src/storage/in_memory.rs +++ b/crates/core/src/storage/in_memory.rs @@ -232,24 +232,24 @@ impl ActivityStateManagement for InMemoryActivityStorage { activity_id: ActivityGuid, end_opts: EndOptions, ) -> PaceResult { - let mut activities = self.log.write(); + let activities = self.log.read(); - let _ = activities.entry(activity_id).and_modify(|activity| { - match calculate_duration(activity.begin(), *end_opts.end_time()) { - Ok(duration) => { - let end_opts = ActivityEndOptions::new(*end_opts.end_time(), duration); - activity.end_activity(end_opts); - } - Err(_) => { - log::warn!( - "Activity {} ends before it began. That's impossible. Skipping \ - activity. Please fix manually and run the command again.", - activity - ); - } - } - }); + let begin_time = *activities + .get(&activity_id) + .ok_or(ActivityLogErrorKind::ActivityNotFound(activity_id))? + .begin(); + + drop(activities); + let end_opts = ActivityEndOptions::new( + *end_opts.end_time(), + calculate_duration(&begin_time, *end_opts.end_time())?, + ); + + let mut activities = self.log.write(); + let _ = activities + .entry(activity_id) + .and_modify(|activity| activity.end_activity(end_opts)); drop(activities); self.read_activity(activity_id) diff --git a/src/commands/resume.rs b/src/commands/resume.rs index 412bfbe5..deebddae 100644 --- a/src/commands/resume.rs +++ b/src/commands/resume.rs @@ -51,8 +51,8 @@ impl ResumeCmd { // parse time from string or get now let date_time = extract_time_or_now(self.resume_opts.at())?.is_future()?; - let resumed = if let Ok(Some(resumed_activity)) = activity_store - .resume_most_recent_activity(ResumeOptions::builder().resume_time(date_time).build()) + let resumed = if let Some(resumed_activity) = activity_store + .resume_most_recent_activity(ResumeOptions::builder().resume_time(date_time).build())? { println!("Resumed {}", resumed_activity.activity()); true