-
Notifications
You must be signed in to change notification settings - Fork 7
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: Require the node's metadata to always be a Map #661
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,17 +14,52 @@ use crate::{Hugr, IncomingPort, OutgoingPort, Port, PortIndex}; | |
|
||
use self::sealed::HugrMutInternals; | ||
|
||
use super::NodeMetadataMap; | ||
|
||
/// Functions for low-level building of a HUGR. | ||
pub trait HugrMut: HugrMutInternals { | ||
/// Returns the metadata associated with a node. | ||
fn get_metadata_mut(&mut self, node: Node) -> Result<&mut NodeMetadata, HugrError> { | ||
/// Returns a metadata entry associated with a node. | ||
fn get_metadata_mut( | ||
&mut self, | ||
node: Node, | ||
key: impl AsRef<str>, | ||
) -> Result<&mut NodeMetadata, HugrError> { | ||
self.valid_node(node)?; | ||
Ok(self.hugr_mut().metadata.get_mut(node.pg_index())) | ||
let node_meta = self | ||
.hugr_mut() | ||
.metadata | ||
.get_mut(node.pg_index()) | ||
.get_or_insert_with(Default::default); | ||
Ok(node_meta | ||
.entry(key.as_ref()) | ||
.or_insert(serde_json::Value::Null)) | ||
} | ||
|
||
/// Sets a metadata value associated with a node. | ||
fn set_metadata( | ||
&mut self, | ||
node: Node, | ||
key: impl AsRef<str>, | ||
metadata: impl Into<NodeMetadata>, | ||
) -> Result<(), HugrError> { | ||
*self.get_metadata_mut(node, key)? = metadata.into(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a lot going on on the LHS of an assignment here 👀 |
||
Ok(()) | ||
} | ||
|
||
/// Retrieve the complete metadata map for a node. | ||
fn take_node_metadata(&mut self, node: Node) -> Option<NodeMetadataMap> { | ||
self.valid_node(node).ok()?; | ||
self.hugr_mut().metadata.take(node.pg_index()) | ||
} | ||
|
||
/// Sets the metadata associated with a node. | ||
fn set_metadata(&mut self, node: Node, metadata: NodeMetadata) -> Result<(), HugrError> { | ||
*self.get_metadata_mut(node)? = metadata; | ||
/// Overwrite the complete metadata map for a node. | ||
fn overwrite_node_metadata( | ||
&mut self, | ||
node: Node, | ||
metadata: Option<NodeMetadataMap>, | ||
) -> Result<(), HugrError> { | ||
self.valid_node(node)?; | ||
self.hugr_mut().metadata.set(node.pg_index(), metadata); | ||
Ok(()) | ||
} | ||
|
||
|
@@ -304,7 +339,7 @@ impl<T: RootTagged<RootHandle = Node> + AsMut<Hugr>> HugrMut for T { | |
let optype = other.op_types.take(node); | ||
self.as_mut().op_types.set(new_node, optype); | ||
let meta = other.metadata.take(node); | ||
self.as_mut().set_metadata(new_node.into(), meta).unwrap(); | ||
self.as_mut().metadata.set(new_node, meta); | ||
} | ||
debug_assert_eq!( | ||
Some(&new_root.pg_index()), | ||
|
@@ -326,10 +361,8 @@ impl<T: RootTagged<RootHandle = Node> + AsMut<Hugr>> HugrMut for T { | |
for (&node, &new_node) in node_map.iter() { | ||
let nodetype = other.get_nodetype(node.into()); | ||
self.as_mut().op_types.set(new_node, nodetype.clone()); | ||
let meta = other.get_metadata(node.into()); | ||
self.as_mut() | ||
.set_metadata(new_node.into(), meta.clone()) | ||
.unwrap(); | ||
let meta = other.base_hugr().metadata.get(node); | ||
self.as_mut().metadata.set(new_node, meta.clone()); | ||
} | ||
debug_assert_eq!( | ||
Some(&new_root.pg_index()), | ||
|
@@ -359,10 +392,8 @@ impl<T: RootTagged<RootHandle = Node> + AsMut<Hugr>> HugrMut for T { | |
for (&node, &new_node) in node_map.iter() { | ||
let nodetype = other.get_nodetype(node.into()); | ||
self.as_mut().op_types.set(new_node, nodetype.clone()); | ||
let meta = other.get_metadata(node.into()); | ||
self.as_mut() | ||
.set_metadata(new_node.into(), meta.clone()) | ||
.unwrap(); | ||
let meta = other.base_hugr().metadata.get(node); | ||
self.as_mut().metadata.set(new_node, meta.clone()); | ||
} | ||
Ok(translate_indices(node_map)) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The grammar isn't quite right here