From fe868ce95ca483d93b75098c4fcb8193d3a82edb Mon Sep 17 00:00:00 2001 From: Luc Perkins Date: Wed, 21 Aug 2024 17:03:10 +0200 Subject: [PATCH] Remove all nulls from the output path tree --- src/cli/cmd/paths.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/cli/cmd/paths.rs b/src/cli/cmd/paths.rs index c82bff0..a63dc43 100644 --- a/src/cli/cmd/paths.rs +++ b/src/cli/cmd/paths.rs @@ -21,7 +21,8 @@ impl CommandExecute for PathsSubcommand { async fn execute(self) -> color_eyre::Result { let release_ref = parse_release_ref(&self.release_ref)?; - let paths = FlakeHubClient::paths(self.api_addr.as_ref(), &release_ref).await?; + let mut paths = FlakeHubClient::paths(self.api_addr.as_ref(), &release_ref).await?; + clear_nulls(&mut paths); tracing::debug!( r#ref = release_ref.to_string(), @@ -59,3 +60,25 @@ impl Serialize for PathNode { } } } + +// Recursively removes any nulls from the output path tree +fn clear_nulls(map: &mut HashMap) { + let keys_to_remove: Vec = map + .iter_mut() + .filter_map(|(key, value)| match value { + PathNode::PathMap(ref mut inner_map) => { + clear_nulls(inner_map); + if inner_map.is_empty() { + Some(key.clone()) + } else { + None + } + } + _ => None, + }) + .collect(); + + for key in keys_to_remove { + map.remove(&key); + } +}