make taskdb.apply for create/delete not fail if already exists/doesn't exist

This commit is contained in:
Dustin J. Mitchell
2022-01-05 02:49:04 +00:00
parent d6efad06ee
commit e3f438d9fa
2 changed files with 41 additions and 27 deletions

View File

@@ -99,18 +99,25 @@ impl Replica {
.map(move |tm| Task::new(uuid, tm)))
}
/// Create a new task. The task must not already exist.
/// Create a new task.
pub fn new_task(&mut self, status: Status, description: String) -> anyhow::Result<Task> {
self.add_undo_point(false)?;
let uuid = Uuid::new_v4();
let taskmap = self.taskdb.apply(SyncOp::Create { uuid })?;
trace!("task {} created", uuid);
let mut task = Task::new(uuid, taskmap).into_mut(self);
let mut task = self.create_task(uuid)?.into_mut(self);
task.set_description(description)?;
task.set_status(status)?;
trace!("task {} created", uuid);
Ok(task.into_immut())
}
/// Create a new, empty task with the given UUID. This is useful for importing tasks, but
/// otherwise should be avoided in favor of `new_task`. If the task already exists, this
/// does nothing and returns the existing task.
pub fn create_task(&mut self, uuid: Uuid) -> anyhow::Result<Task> {
self.add_undo_point(false)?;
let taskmap = self.taskdb.apply(SyncOp::Create { uuid })?;
Ok(Task::new(uuid, taskmap))
}
/// Delete a task. The task must exist. Note that this is different from setting status to
/// Deleted; this is the final purge of the task. This is not a public method as deletion
/// should only occur through expiration.