[rust/taskchampion/src/task] Add Task::get_value and TaskMut::set_value
This commit is contained in:
committed by
Dustin J. Mitchell
parent
2889e77d51
commit
dcc285bc4a
@@ -292,6 +292,12 @@ impl Task {
|
||||
})
|
||||
}
|
||||
|
||||
/// Get task's property value by name.
|
||||
pub fn get_value<S: Into<String>>(&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<S: Into<String>>(
|
||||
&mut self,
|
||||
property: S,
|
||||
value: Option<String>,
|
||||
) -> 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": "<timestamp>", 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| {
|
||||
|
||||
Reference in New Issue
Block a user