Add syntax for simpler connections between nodes

This commit is contained in:
Juno Takano 2025-12-10 00:58:46 -03:00
commit 26d6b3240a
4 changed files with 65 additions and 74 deletions

View file

@ -26,7 +26,16 @@ fn modulate_nodes(old_nodes: HashMap<String, Node>) -> HashMap<String, Node> {
for (key, node) in old_nodes.iter() {
let connections = node.connections.clone().unwrap_or_default();
let mut vec = connections.clone();
let mut new_edges = connections.clone();
for link in node.links.iter() {
new_edges.push(Edge {
from: key.to_string(),
to: link.to_string(),
anchor: String::new(),
detached: !old_nodes.contains_key(link),
})
}
for (i, edge) in connections.iter().enumerate() {
@ -42,14 +51,14 @@ fn modulate_nodes(old_nodes: HashMap<String, Node>) -> HashMap<String, Node> {
new_edge.detached = true;
}
vec[i] = new_edge;
new_edges[i] = new_edge;
}
let new_node = Node {
connections: Some(vec),
connections: Some(new_edges),
..node.clone()
};
nodes.insert(key.to_string(), new_node);
}
@ -64,14 +73,9 @@ fn make_incoming(nodes: HashMap<String, Node>) -> HashMap<String, Vec<Edge>> {
let empty_vec: Vec<Edge> = vec![];
for edge in node.connections.clone().unwrap_or_default().iter() {
let vec = incoming.get(&edge.to.clone()).unwrap_or(&empty_vec);
if vec.contains(edge) {
vec.clone().extend_from_slice(&[edge.clone()]);
incoming.insert(edge.to.clone(), vec.clone());
} else {
incoming.insert(edge.to.clone(), vec![edge.clone()]);
}
let mut edges = incoming.get(&edge.to.clone()).unwrap_or(&empty_vec).clone();
edges.extend_from_slice(&[edge.clone()]);
incoming.insert(edge.to.clone(), edges.clone());
}
}

View file

@ -28,14 +28,16 @@ pub struct Node {
pub body: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub connections: Option<Vec<Edge>>,
#[serde(default)]
pub links: Vec<String>,
}
impl Graph {
pub fn new(message: Option<String>) -> Graph {
Self {
nodes: HashMap::new(),
incoming: HashMap::new(),
root_node: "".to_string(),
incoming: HashMap::new(),
messages: vec![message
.unwrap_or("This graph is empty or in error".to_string())],
}
@ -59,6 +61,7 @@ impl Node {
None => "Node is empty, missing or wasn't found.".to_string()
},
connections: None,
links: vec![],
}
}
}