add dependency support to taskchampion

This commit is contained in:
Dustin J. Mitchell
2022-02-21 21:22:18 +00:00
parent a030053dae
commit bf73cc4cc7
4 changed files with 154 additions and 1 deletions

View File

@@ -243,10 +243,27 @@ impl Task {
.map(|(p, v)| (p.as_ref(), v.as_ref()))
}
/// Get the modification time for this task.
pub fn get_modified(&self) -> Option<DateTime<Utc>> {
self.get_timestamp(Prop::Modified.as_ref())
}
/// Get the UUIDs of tasks on which this task depends.
///
/// This includes all dependencies, regardless of their status. In fact, it may include
/// dependencies that do not exist.
pub fn get_dependencies(&self) -> impl Iterator<Item = Uuid> + '_ {
self.taskmap.iter().filter_map(|(p, _)| {
if let Some(dep_str) = p.strip_prefix("dep_") {
if let Ok(u) = Uuid::parse_str(dep_str) {
return Some(u);
}
// (un-parseable dep_.. properties are ignored)
}
None
})
}
// -- utility functions
fn is_known_key(key: &str) -> bool {
@@ -423,6 +440,18 @@ impl<'r> TaskMut<'r> {
self.set_string(key, None)
}
/// Add a dependency.
pub fn add_dependency(&mut self, dep: Uuid) -> anyhow::Result<()> {
let key = format!("dep_{}", dep);
self.set_string(key, Some("".to_string()))
}
/// Remove a dependency.
pub fn remove_dependency(&mut self, dep: Uuid) -> anyhow::Result<()> {
let key = format!("dep_{}", dep);
self.set_string(key, None)
}
// -- utility functions
fn update_modified(&mut self) -> anyhow::Result<()> {
@@ -1011,4 +1040,25 @@ mod test {
assert!(task.remove_legacy_uda("tag_abc").is_err());
})
}
#[test]
fn test_dependencies() {
with_mut_task(|mut task| {
assert_eq!(task.get_dependencies().collect::<Vec<_>>(), vec![]);
let dep1 = Uuid::new_v4();
let dep2 = Uuid::new_v4();
task.add_dependency(dep1).unwrap();
assert_eq!(task.get_dependencies().collect::<Vec<_>>(), vec![dep1]);
task.add_dependency(dep1).unwrap(); // add twice is ok
task.add_dependency(dep2).unwrap();
let deps = task.get_dependencies().collect::<Vec<_>>();
assert!(deps.contains(&dep1));
assert!(deps.contains(&dep2));
task.remove_dependency(dep1).unwrap();
assert_eq!(task.get_dependencies().collect::<Vec<_>>(), vec![dep2]);
})
}
}