From dcc285bc4ac70bc5cbb38419e9c2edf8321cd3e1 Mon Sep 17 00:00:00 2001 From: Pablo Baeyens Date: Thu, 14 Jul 2022 00:12:14 +0200 Subject: [PATCH] [rust/taskchampion/src/task] Add `Task::get_value` and `TaskMut::set_value` --- rust/taskchampion/src/task/task.rs | 50 +++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/rust/taskchampion/src/task/task.rs b/rust/taskchampion/src/task/task.rs index a3f4384ac..55c5c6318 100644 --- a/rust/taskchampion/src/task/task.rs +++ b/rust/taskchampion/src/task/task.rs @@ -292,6 +292,12 @@ impl Task { }) } + /// Get task's property value by name. + pub fn get_value>(&self, property: S) -> Option<&str> { + let property = property.into(); + self.taskmap.get(&property).map(|s| s.as_ref()) + } + // -- utility functions fn is_known_key(key: &str) -> bool { @@ -364,6 +370,27 @@ impl<'r> TaskMut<'r> { self.set_timestamp(Prop::Modified.as_ref(), Some(modified)) } + /// Set a tasks's property by name. + pub fn set_value>( + &mut self, + property: S, + value: Option, + ) -> anyhow::Result<()> { + let property = property.into(); + + if let Some(ref v) = value { + trace!("task {}: set property {}={:?}", self.task.uuid, property, v); + } else { + trace!("task {}: remove property {}", self.task.uuid, property); + } + + self.task.taskmap = self + .replica + .update_task(self.task.uuid, &property, value.as_ref())?; + + Ok(()) + } + /// Start the task by creating "start": "", if the task is not already /// active. pub fn start(&mut self) -> anyhow::Result<()> { @@ -509,17 +536,7 @@ impl<'r> TaskMut<'r> { self.update_modified()?; } - if let Some(ref v) = value { - trace!("task {}: set property {}={:?}", self.task.uuid, property, v); - } else { - trace!("task {}: remove property {}", self.task.uuid, property); - } - - self.task.taskmap = self - .replica - .update_task(self.task.uuid, &property, value.as_ref())?; - - Ok(()) + self.set_value(property, value) } fn set_timestamp( @@ -865,6 +882,17 @@ mod test { }); } + #[test] + fn test_set_get_value() { + with_mut_task(|mut task| { + let property = "property-name"; + task.set_value(property, Some("value".into())).unwrap(); + assert_eq!(task.get_value(property), Some("value".into())); + task.set_value(property, None).unwrap(); + assert_eq!(task.get_value(property), None); + }); + } + #[test] fn test_start() { with_mut_task(|mut task| {