Skip to content

Commit

Permalink
refactor: reimplement logic to end and activity for in-memory storage…
Browse files Browse the repository at this point in the history
… to make it easier for error handling

Signed-off-by: simonsan <14062932+simonsan@users.noreply.github.com>
  • Loading branch information
simonsan committed Mar 2, 2024
1 parent 65429f2 commit 571822f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion crates/core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),

Expand Down
32 changes: 16 additions & 16 deletions crates/core/src/storage/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,24 +232,24 @@ impl ActivityStateManagement for InMemoryActivityStorage {
activity_id: ActivityGuid,
end_opts: EndOptions,
) -> PaceResult<ActivityItem> {
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)
Expand Down
4 changes: 2 additions & 2 deletions src/commands/resume.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 571822f

Please sign in to comment.