Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: HugrMut::remove_metadata #1619

Merged
merged 2 commits into from
Oct 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions hugr-core/src/hugr/hugrmut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ pub trait HugrMut: HugrMutInternals {
*entry = metadata.into();
}

/// Remove a metadata entry associated with a node.
///
/// # Panics
///
/// If the node is not in the graph.
fn remove_metadata(&mut self, node: Node, key: impl AsRef<str>) {
panic_invalid_node(self, node);
let node_meta = self.hugr_mut().metadata.get_mut(node.pg_index());
if let Some(node_meta) = node_meta {
node_meta.remove(key.as_ref());
}
}

/// Retrieve the complete metadata map for a node.
fn take_node_metadata(&mut self, node: Node) -> Option<NodeMetadataMap> {
if !self.valid_node(node) {
Expand Down Expand Up @@ -551,4 +564,23 @@ mod test {

Ok(())
}

#[test]
fn metadata() {
let mut hugr = Hugr::default();

// Create the root module definition
let root: Node = hugr.root();

assert_eq!(hugr.get_metadata(root, "meta"), None);

*hugr.get_metadata_mut(root, "meta") = "test".into();
assert_eq!(hugr.get_metadata(root, "meta"), Some(&"test".into()));

hugr.set_metadata(root, "meta", "new");
assert_eq!(hugr.get_metadata(root, "meta"), Some(&"new".into()));

hugr.remove_metadata(root, "meta");
assert_eq!(hugr.get_metadata(root, "meta"), None);
}
}
Loading