122 lines
3.4 KiB
Rust
122 lines
3.4 KiB
Rust
use std::collections::HashMap;
|
|
|
|
use crate::types::{Graph, Node, Edge};
|
|
|
|
pub fn populate_graph() -> Graph {
|
|
let toml_source = match std::fs::read_to_string("./static/graph.toml") {
|
|
Ok(s) => s,
|
|
Err(e) => format!("Error: {e}"),
|
|
};
|
|
let graph = deserialize_graph(&Format::Toml, &toml_source);
|
|
|
|
let nodes = modulate_nodes(&graph.nodes);
|
|
|
|
Graph {
|
|
nodes: nodes.clone(),
|
|
incoming: make_incoming(&nodes),
|
|
..graph
|
|
}
|
|
}
|
|
|
|
fn modulate_nodes(old_nodes: &HashMap<String, Node>) -> HashMap<String, Node> {
|
|
let mut nodes: HashMap<String, Node> = HashMap::new();
|
|
|
|
for (key, node) in old_nodes {
|
|
let connections = node.connections.clone().unwrap_or_default();
|
|
let mut new_edges = connections.clone();
|
|
|
|
for (i, edge) in connections.iter().enumerate() {
|
|
let mut new_edge = edge.clone();
|
|
|
|
// Populate empty "from" IDs in edges with node's ID
|
|
if edge.from.is_empty() {
|
|
new_edge.from.clone_from(key);
|
|
}
|
|
|
|
// Flag detached edges
|
|
if !old_nodes.contains_key(&edge.to) {
|
|
new_edge.detached = true;
|
|
}
|
|
|
|
if let Some(e) = new_edges.get_mut(i) {
|
|
*e = new_edge;
|
|
}
|
|
}
|
|
|
|
// Create connections for each link
|
|
for link in &node.links {
|
|
new_edges.push(Edge {
|
|
from: key.clone(),
|
|
to: link.clone(),
|
|
anchor: String::new(),
|
|
detached: !old_nodes.contains_key(link),
|
|
});
|
|
}
|
|
|
|
// Populate empty titles with IDs
|
|
let new_title = if node.title.is_empty() {
|
|
key.clone()
|
|
} else {
|
|
node.title.clone()
|
|
};
|
|
|
|
let new_node = Node {
|
|
id: key.clone(),
|
|
title: new_title,
|
|
connections: Some(new_edges),
|
|
..node.clone()
|
|
};
|
|
|
|
nodes.insert(key.clone(), new_node);
|
|
}
|
|
|
|
nodes
|
|
}
|
|
|
|
// Construct a HashMap with incoming connections (reversed edges)
|
|
fn make_incoming(nodes: &HashMap<String, Node>) -> HashMap<String, Vec<Edge>> {
|
|
let mut incoming: HashMap<String, Vec<Edge>> = HashMap::new();
|
|
|
|
for node in nodes.clone().into_values() {
|
|
let empty_vec: Vec<Edge> = vec![];
|
|
for edge in &node.connections.clone().unwrap_or_default() {
|
|
let mut edges =
|
|
incoming.get(&edge.to.clone()).unwrap_or(&empty_vec).clone();
|
|
edges.extend_from_slice(std::slice::from_ref(edge));
|
|
incoming.insert(edge.to.clone(), edges.clone());
|
|
}
|
|
}
|
|
|
|
incoming
|
|
}
|
|
|
|
pub enum Format {
|
|
Toml,
|
|
Json,
|
|
}
|
|
|
|
pub fn serialize_graph(out_format: &Format, graph: &Graph) -> String {
|
|
match *out_format {
|
|
Format::Toml => match toml::to_string(graph) {
|
|
Ok(s) => s,
|
|
Err(e) => e.to_string(),
|
|
},
|
|
Format::Json => match serde_json::to_string(graph) {
|
|
Ok(s) => s,
|
|
Err(e) => e.to_string(),
|
|
},
|
|
}
|
|
}
|
|
|
|
pub fn deserialize_graph(in_format: &Format, serial: &str) -> Graph {
|
|
match *in_format {
|
|
Format::Toml => match toml::from_str(serial) {
|
|
Ok(g) => g,
|
|
Err(error) => Graph::new(Some(error.to_string())),
|
|
},
|
|
Format::Json => match serde_json::from_str(serial) {
|
|
Ok(g) => g,
|
|
Err(error) => Graph::new(Some(error.to_string())),
|
|
},
|
|
}
|
|
}
|