Route import and export signature edits through paired store primitives

This commit is contained in:
Dennis Kobert committed 2026-09-22 12:40:48 +00:00
1 parent ea17f690a2
commit 1863e9ea67
3 files changed
+172 -125

No files matched your search

@@ -12,6 +12,7 @@ mod mutations;
mod queries;
mod resolved_types;
pub mod storage_metadata;
mod store;
mod structure;
mod template;
mod types;
@@ -19,6 +20,7 @@ mod types;
mod validation;
mod view;
pub use store::NodeLocator;
pub use template::*;
pub use types::*;
pub use view::{NetworkError, NetworkView};
@@ -105,18 +105,11 @@ impl NodeNetworkInterface {
/// Inserts a new export at insert index. If the insert index is -1 it is inserted at the end. The output_name is used by the encapsulating node.
pub fn add_export(&mut self, default_value: TaggedValue, insert_index: isize, output_name: &str, network_path: &[NodeId]) {
let Some(network) = self.network_mut(network_path) else {
log::error!("Could not get nested network in add_export");
let inserted_index = if insert_index == -1 { self.number_of_exports(network_path) } else { insert_index as usize };
if !self.insert_export_slot(network_path, inserted_index, NodeInput::value(default_value, true), output_name.to_string()) {
return;
};
let input = NodeInput::value(default_value, true);
let inserted_index = if insert_index == -1 { network.exports.len() } else { insert_index as usize };
if insert_index == -1 {
network.exports.push(input);
} else {
network.exports.insert(insert_index as usize, input);
}
self.clear_encapsulating_reference(network_path);
self.transaction_modified();
@@ -129,19 +122,6 @@ impl NodeNetworkInterface {
self.set_to_node_or_layer(&parent_id, &encapsulating_path, false);
};
// There will not be an encapsulating node if the network is the document network
if let Some(encapsulating_node_metadata) = self.encapsulating_node_metadata_mut(network_path) {
if insert_index == -1 {
encapsulating_node_metadata.persistent_metadata.output_names.push(output_name.to_string());
} else {
encapsulating_node_metadata.persistent_metadata.output_names.insert(insert_index as usize, output_name.to_string());
}
// Clear the reference to the nodes definition
if let Some(network_metadata) = encapsulating_node_metadata.persistent_metadata.network_metadata.as_mut() {
network_metadata.persistent_metadata.reference = None
}
};
// Update the export ports and outward wires for the current network
self.unload_import_export_ports(network_path);
self.unload_modify_import_export(network_path);
@@ -173,22 +153,12 @@ impl NodeNetworkInterface {
return;
};
let Some(network) = self.network_mut(&encapsulating_network_path) else {
log::error!("Could not get nested network in insert_input");
let locator = NodeLocator::new(node_id, &encapsulating_network_path);
let inserted_index = if insert_index == -1 { self.number_of_inputs(&node_id, &encapsulating_network_path) } else { insert_index as usize };
if !self.insert_input_slot(locator, inserted_index, NodeInput::value(default_value, exposed), (input_name, input_description).into()) {
return;
};
let Some(node) = network.nodes.get_mut(&node_id) else {
log::error!("Could not get node in insert_input");
return;
};
let input = NodeInput::value(default_value, exposed);
let inserted_index = if insert_index == -1 { node.inputs.len() } else { insert_index as usize };
if insert_index == -1 {
node.inputs.push(input);
} else {
node.inputs.insert(insert_index as usize, input);
}
self.clear_encapsulating_reference(network_path);
self.transaction_modified();
@@ -197,22 +167,6 @@ impl NodeNetworkInterface {
self.set_to_node_or_layer(&node_id, &encapsulating_network_path, false);
}
let Some(node_metadata) = self.node_metadata_mut(&node_id, &encapsulating_network_path) else {
log::error!("Could not get node_metadata in insert_input");
return;
};
let new_input = (input_name, input_description).into();
if insert_index == -1 {
node_metadata.persistent_metadata.input_metadata.push(new_input);
} else {
node_metadata.persistent_metadata.input_metadata.insert(insert_index as usize, new_input);
}
// Clear the reference to the nodes definition
if let Some(network_metadata) = node_metadata.persistent_metadata.network_metadata.as_mut() {
network_metadata.persistent_metadata.reference = None
}
// Update the metadata for the encapsulating node
self.unload_node_click_targets(&node_id, &encapsulating_network_path);
self.unload_all_nodes_bounding_box(&encapsulating_network_path);
@@ -295,23 +249,13 @@ impl NodeNetworkInterface {
}
}
let Some(network) = self.network_mut(network_path) else {
log::error!("Could not get nested network in add_export");
if self.remove_export_slot(network_path, export_index).is_none() {
return;
};
network.exports.remove(export_index);
}
self.clear_encapsulating_reference(network_path);
self.transaction_modified();
let Some(encapsulating_node_metadata) = self.node_metadata_mut(&parent_id, &encapsulating_network_path) else {
log::error!("Could not get encapsulating node metadata in remove_export");
return;
};
encapsulating_node_metadata.persistent_metadata.output_names.remove(export_index);
if let Some(network_metadata) = encapsulating_node_metadata.persistent_metadata.network_metadata.as_mut() {
network_metadata.persistent_metadata.reference = None;
}
self.finish_signature_edit(parent_id, &encapsulating_network_path, network_path);
}
@@ -347,30 +291,16 @@ impl NodeNetworkInterface {
self.create_wire(&output_connector, &input_wire, network_path);
}
let Some(network) = self.network_mut(encapsulating_network_path) else {
log::error!("Could not get parent node in remove_import");
let parent_id = *parent_id;
let encapsulating_network_path = encapsulating_network_path.to_vec();
if self.remove_input_slot(NodeLocator::new(parent_id, &encapsulating_network_path), import_index).is_none() {
return;
};
let Some(node) = network.nodes.get_mut(parent_id) else {
log::error!("Could not get node in remove_import");
return;
};
node.inputs.remove(import_index);
}
self.clear_encapsulating_reference(network_path);
self.transaction_modified();
// There will not be an encapsulating node if the network is the document network
let Some(encapsulating_node_metadata) = self.node_metadata_mut(parent_id, encapsulating_network_path) else {
log::error!("Could not get encapsulating node metadata in remove_export");
return;
};
encapsulating_node_metadata.persistent_metadata.input_metadata.remove(import_index);
if let Some(network_metadata) = encapsulating_node_metadata.persistent_metadata.network_metadata.as_mut() {
network_metadata.persistent_metadata.reference = None;
}
self.finish_signature_edit(*parent_id, encapsulating_network_path, network_path);
self.finish_signature_edit(parent_id, &encapsulating_network_path, network_path);
}
/// The end index is before the export is removed, so moving to the end is the length of the current exports
@@ -381,29 +311,16 @@ impl NodeNetworkInterface {
return;
};
let Some(network) = self.network_mut(network_path) else {
log::error!("Could not get nested network in reorder_export");
return;
};
if end_index > start_index {
end_index -= 1;
}
let export = network.exports.remove(start_index);
network.exports.insert(end_index, export);
if !self.move_export_slot(network_path, start_index, end_index) {
return;
}
self.clear_encapsulating_reference(network_path);
self.transaction_modified();
let Some(encapsulating_node_metadata) = self.node_metadata_mut(&parent_id, &encapsulating_network_path) else {
log::error!("Could not get encapsulating network_metadata in reorder_export");
return;
};
let name = encapsulating_node_metadata.persistent_metadata.output_names.remove(start_index);
encapsulating_node_metadata.persistent_metadata.output_names.insert(end_index, name);
if let Some(network_metadata) = encapsulating_node_metadata.persistent_metadata.network_metadata.as_mut() {
network_metadata.persistent_metadata.reference = None;
}
// Update the metadata for the encapsulating network
self.unload_outward_wires(&encapsulating_network_path);
self.unload_stack_dependents(&encapsulating_network_path);
@@ -470,34 +387,16 @@ impl NodeNetworkInterface {
return;
};
let Some(encapsulating_network) = self.network_mut(&encapsulating_network_path) else {
log::error!("Could not get nested network in reorder_import");
return;
};
let Some(encapsulating_node) = encapsulating_network.nodes.get_mut(&parent_id) else {
log::error!("Could not get encapsulating node in reorder_import");
return;
};
if end_index > start_index {
end_index -= 1;
}
let import = encapsulating_node.inputs.remove(start_index);
encapsulating_node.inputs.insert(end_index, import);
if !self.move_input_slot(NodeLocator::new(parent_id, &encapsulating_network_path), start_index, end_index) {
return;
}
self.clear_encapsulating_reference(network_path);
self.transaction_modified();
let Some(encapsulating_node_metadata) = self.node_metadata_mut(&parent_id, &encapsulating_network_path) else {
log::error!("Could not get encapsulating network_metadata in reorder_import");
return;
};
let properties_row = encapsulating_node_metadata.persistent_metadata.input_metadata.remove(start_index);
encapsulating_node_metadata.persistent_metadata.input_metadata.insert(end_index, properties_row);
if let Some(network_metadata) = encapsulating_node_metadata.persistent_metadata.network_metadata.as_mut() {
network_metadata.persistent_metadata.reference = None;
}
// Update the metadata for the outer network
self.unload_outward_wires(&encapsulating_network_path);
self.unload_stack_dependents(&encapsulating_network_path);
@@ -0,0 +1,146 @@
use super::*;
/// Addresses one node: the network it lives in, and its ID within that network.
///
/// The store is the only place node identity is used for addressing, so this is the one type that
/// changes when identity stops being derived from the node's location.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct NodeLocator<'a> {
pub node_id: NodeId,
pub network_path: &'a [NodeId],
}
impl<'a> NodeLocator<'a> {
pub fn new(node_id: NodeId, network_path: &'a [NodeId]) -> Self {
Self { node_id, network_path }
}
/// The path of the network this node owns, if it implements one.
pub fn owned_network_path(&self) -> Vec<NodeId> {
[self.network_path, &[self.node_id]].concat()
}
}
/// The graph and metadata halves of one node, resolved together so the two parallel trees are never
/// reached separately and cannot drift apart.
pub(crate) struct NodeMut<'a> {
pub node: &'a mut DocumentNode,
pub metadata: &'a mut DocumentNodePersistentMetadata,
}
// The store: the only writer of the node graph and its parallel metadata tree. Every write keeps the
// two halves in step by construction, so the invariants checked by `validate_invariants` hold without
// each caller restating them.
impl NodeNetworkInterface {
/// Both halves of a node, or `None` if either is missing.
pub(crate) fn node_mut(&mut self, locator: NodeLocator) -> Option<NodeMut<'_>> {
let node = self.network.network_mut().nested_network_mut(locator.network_path)?.nodes.get_mut(&locator.node_id)?;
let metadata = self
.network_metadata
.nested_metadata_mut(locator.network_path)?
.persistent_metadata
.node_metadata
.get_mut(&locator.node_id)?;
Some(NodeMut {
node,
metadata: &mut metadata.persistent_metadata,
})
}
/// Inserts an input and its metadata at `index`, clamped to the end. Returns whether the node was found.
pub(crate) fn insert_input_slot(&mut self, locator: NodeLocator, index: usize, input: NodeInput, metadata: InputMetadata) -> bool {
let Some(node) = self.node_mut(locator) else {
log::error!("Could not get node {} in insert_input_slot", locator.node_id);
return false;
};
let index = index.min(node.node.inputs.len());
node.node.inputs.insert(index, input);
node.metadata.input_metadata.insert(index.min(node.metadata.input_metadata.len()), metadata);
true
}
/// Removes the input at `index` together with its metadata, returning both.
pub(crate) fn remove_input_slot(&mut self, locator: NodeLocator, index: usize) -> Option<(NodeInput, InputMetadata)> {
let Some(node) = self.node_mut(locator) else {
log::error!("Could not get node {} in remove_input_slot", locator.node_id);
return None;
};
if index >= node.node.inputs.len() {
log::error!("Input index {index} out of bounds in remove_input_slot for node {}", locator.node_id);
return None;
}
let input = node.node.inputs.remove(index);
let metadata = (index < node.metadata.input_metadata.len()).then(|| node.metadata.input_metadata.remove(index));
Some((input, metadata.unwrap_or_default()))
}
/// Moves the input at `from` to `to`, carrying its metadata with it.
pub(crate) fn move_input_slot(&mut self, locator: NodeLocator, from: usize, to: usize) -> bool {
let Some((input, metadata)) = self.remove_input_slot(locator, from) else { return false };
self.insert_input_slot(locator, to, input, metadata)
}
/// Inserts an export at `index`, clamped to the end, along with the name the encapsulating node shows
/// for it. The document network has no encapsulating node, so it carries no name.
pub(crate) fn insert_export_slot(&mut self, network_path: &[NodeId], index: usize, export: NodeInput, output_name: String) -> bool {
let Some(network) = self.network_mut(network_path) else {
log::error!("Could not get nested network in insert_export_slot");
return false;
};
let index = index.min(network.exports.len());
network.exports.insert(index, export);
if let Some(encapsulating_node_metadata) = self.encapsulating_node_metadata_mut(network_path) {
let output_names = &mut encapsulating_node_metadata.persistent_metadata.output_names;
output_names.insert(index.min(output_names.len()), output_name);
}
true
}
/// Removes the export at `index` together with the encapsulating node's name for it.
pub(crate) fn remove_export_slot(&mut self, network_path: &[NodeId], index: usize) -> Option<NodeInput> {
let Some(network) = self.network_mut(network_path) else {
log::error!("Could not get nested network in remove_export_slot");
return None;
};
if index >= network.exports.len() {
log::error!("Export index {index} out of bounds in remove_export_slot");
return None;
}
let export = network.exports.remove(index);
if let Some(encapsulating_node_metadata) = self.encapsulating_node_metadata_mut(network_path) {
let output_names = &mut encapsulating_node_metadata.persistent_metadata.output_names;
if index < output_names.len() {
output_names.remove(index);
}
}
Some(export)
}
/// Moves the export at `from` to `to`, carrying the encapsulating node's name for it.
pub(crate) fn move_export_slot(&mut self, network_path: &[NodeId], from: usize, to: usize) -> bool {
let name = self
.encapsulating_node_metadata(network_path)
.and_then(|metadata| metadata.persistent_metadata.output_names.get(from).cloned())
.unwrap_or_default();
let Some(export) = self.remove_export_slot(network_path, from) else { return false };
self.insert_export_slot(network_path, to, export, name)
}
/// Drops the encapsulating node's link to the definition it was instantiated from, which no longer
/// describes it once its signature is edited.
pub(crate) fn clear_encapsulating_reference(&mut self, network_path: &[NodeId]) {
if let Some(encapsulating_node_metadata) = self.encapsulating_node_metadata_mut(network_path)
&& let Some(network_metadata) = encapsulating_node_metadata.persistent_metadata.network_metadata.as_mut()
{
network_metadata.persistent_metadata.reference = None;
}
}
}