From 1cfd4814849f3298c3b42292b83cf32712d855ce Mon Sep 17 00:00:00 2001 From: aaron-jack-manning Date: Wed, 14 Sep 2022 20:37:36 +1000 Subject: [PATCH] bug fixed with dependencies on deleted notes --- src/graph.rs | 16 +++++++++++----- src/main.rs | 9 ++++++++- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/graph.rs b/src/graph.rs index b9bba68..24cdce0 100755 --- a/src/graph.rs +++ b/src/graph.rs @@ -48,15 +48,21 @@ impl Graph { } } - pub fn remove_node(&mut self, node : Id) -> bool { + /// Returns a boolean indicating if the graph was changed (it contained the node) and a + /// set of all dependents on the removed node. + pub fn remove_node(&mut self, node : Id) -> (bool, HashSet) { + let mut dependents = HashSet::new(); + if self.edges.remove(&node).is_some() { - for outgoing in self.edges.values_mut() { - outgoing.remove(&node); + for (&dependent, outgoing) in &mut self.edges { + if outgoing.remove(&node) { + dependents.insert(dependent); + } } - true + (true, dependents) } else { - false + (false, dependents) } } diff --git a/src/main.rs b/src/main.rs index b5515dc..5274ad2 100755 --- a/src/main.rs +++ b/src/main.rs @@ -127,7 +127,14 @@ fn program() -> Result<(), error::Error> { let task = tasks::Task::load(id, vault_folder, false)?; let name = task.data.name.clone(); state.data.index.remove(task.data.name.clone(), task.data.id); - state.data.deps.remove_node(task.data.id); + // Removing the task from others which list it as a dependency. + if let (true, dependents) = state.data.deps.remove_node(task.data.id) { + for dependent in dependents { + let mut task = tasks::Task::load(dependent, vault_folder, false)?; + task.data.dependencies.remove(&id); + task.save()?; + } + } task.delete()?; println!("Deleted task {} (ID: {})", format::task(&name), format::id(id));