From 400eb02efc6cbfe85f35f6320d043d1b7afb1f5b Mon Sep 17 00:00:00 2001 From: jutty Date: Wed, 10 Dec 2025 09:58:44 -0300 Subject: [PATCH] Implement automatic IDs --- README.md | 3 ++- src/formats.rs | 1 + src/main.rs | 12 ++++++------ src/types.rs | 33 +++++++++++++++++---------------- static/graph.toml | 9 --------- 5 files changed, 26 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index a5b1718..c707a6d 100644 --- a/README.md +++ b/README.md @@ -7,8 +7,9 @@ It works by ingesting a TOML file containing your node specification and serving ## Roadmap - [x] Array syntax for lightweight connections +- [x] Automatic IDs +- [ ] Automatic titles - [ ] Automatic anchors -- [ ] Automatic IDs - [ ] Mismatch between TOML ID and provided ID ## Motivation diff --git a/src/formats.rs b/src/formats.rs index 8bf6350..19b1132 100644 --- a/src/formats.rs +++ b/src/formats.rs @@ -55,6 +55,7 @@ fn modulate_nodes(old_nodes: HashMap) -> HashMap { } let new_node = Node { + id: key.clone(), connections: Some(new_edges), ..node.clone() }; diff --git a/src/main.rs b/src/main.rs index c71c185..99ee1cf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -95,11 +95,11 @@ async fn node_view(Path(id): Path) -> impl IntoResponse { let node: &Node = nodes.get(&id).unwrap_or(&empty_node); - context.insert("id", &node.id); + context.insert("id", &id); context.insert("title", &node.title); context.insert("body", &node.body); context.insert("connections", &node.connections.clone()); - context.insert("incoming", &graph.incoming.get(&node.id)); + context.insert("incoming", &graph.incoming.get(&id)); template_handler( "node.html", @@ -109,9 +109,9 @@ async fn node_view(Path(id): Path) -> impl IntoResponse { r#"Failed to generate page for node {} (ID {}) with {} outgoing, {} incoming connections and body "{}""#, node.title, - node.id, + id, node.connections.iter().len(), - graph.incoming.get(&node.id).iter().len(), + graph.incoming.get(&id).iter().len(), node.body, ), ) @@ -121,7 +121,7 @@ async fn index() -> Html { let mut context = tera::Context::new(); let graph = populate_graph(); - let root_node = graph.get_root(); + let root_node = graph.get_root().unwrap_or_default(); let nodes: Vec = graph.nodes.into_values().collect(); context.insert("nodes", &nodes); @@ -134,7 +134,7 @@ async fn tree() -> Html { let mut context = tera::Context::new(); let graph = populate_graph(); - let root_node = graph.get_root(); + let root_node = graph.get_root().unwrap_or_default(); let nodes: Vec = graph.nodes.into_values().collect(); context.insert("nodes", &nodes); diff --git a/src/types.rs b/src/types.rs index 09e9337..1f3916e 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,7 +1,7 @@ use serde::{Serialize, Deserialize}; use std::collections::HashMap; -#[derive(Serialize, Deserialize, Clone, Default)] +#[derive(Serialize, Deserialize, Clone, Default, Debug)] pub struct Graph { pub messages: Vec, pub root_node: String, @@ -10,7 +10,19 @@ pub struct Graph { pub incoming: HashMap>, } -#[derive(Serialize, Clone, Default, PartialEq, Deserialize)] +#[derive(Serialize, Deserialize, Clone, Default, Debug)] +pub struct Node { + pub title: String, + pub body: String, + #[serde(skip_serializing_if = "Option::is_none")] + pub connections: Option>, + #[serde(default)] + pub links: Vec, + #[serde(default)] + pub id: String, +} + +#[derive(Serialize, Clone, Default, PartialEq, Deserialize, Debug)] pub struct Edge { pub to: String, #[serde(default)] @@ -21,22 +33,11 @@ pub struct Edge { pub detached: bool, } -#[derive(Serialize, Deserialize, Clone, Default)] -pub struct Node { - pub title: String, - pub id: String, - pub body: String, - #[serde(skip_serializing_if = "Option::is_none")] - pub connections: Option>, - #[serde(default)] - pub links: Vec, -} - impl Graph { pub fn new(message: Option) -> Graph { Self { nodes: HashMap::new(), - root_node: "".to_string(), + root_node: "VoidNode".to_string(), incoming: HashMap::new(), messages: vec![message .unwrap_or("This graph is empty or in error".to_string())], @@ -54,8 +55,8 @@ impl Graph { impl Node { pub fn new(message: Option) -> Node { Self { - title: "Empty Node".to_string(), - id: "EmptyNode".to_string(), + id: "VoidNode".to_string(), + title: "Pure Void".to_string(), body: match message { Some(s) => s, None => "Node is empty, missing or wasn't found.".to_string() diff --git a/static/graph.toml b/static/graph.toml index ae6e285..262fdd8 100644 --- a/static/graph.toml +++ b/static/graph.toml @@ -3,7 +3,6 @@ root_node = "Interface" [nodes.Interface] title = "Interface" -id = "Interface" body = """ An interface is a point of contact between the inside and the outside of something. Contrast with intraface. """ @@ -12,7 +11,6 @@ links = ["Intraface"] [nodes.Intraface] title = "Intraface" -id = "Intraface" body = """ The intraface is the reflexive process of communicating, creating, thinking, that does not or cannot get shared with others. Contrast with interface. """ @@ -21,14 +19,12 @@ links = ["Thinking", "Interface"] [nodes.Thinking] title = "Thinking" -id = "Thinking" body = """ Thinking is a process by which some beings create and manipulate mental constructs. """ [nodes.Paradigm] title = "Paradigm" -id = "Paradigm" body = """ A paradigm is a cohesive set of beliefs, methods and principles that serve both as justification for a given position and as guidance for how to pursue its praxis. """ @@ -37,7 +33,6 @@ links = [ "Principle", "Belief", "Method", "Position", "Praxis" ] [nodes.Principle] title = "Principle" -id = "Principle" body = """ A principle is a belief that implies commitment and necessity. @@ -56,7 +51,6 @@ to = "Identity" [nodes.Religion] title = "Religion" -id = "Religion" body = """ A religion is a paradigm that involves unfalsifiable beliefs, particularly those in the domain of morality. @@ -86,7 +80,6 @@ links = [ [nodes.Identity] title = "Identity" -id = "Identity" body = """ Identity is how individuals construe their sameness and otherness from each other and from nothingness. """ @@ -99,7 +92,6 @@ to = "Emptiness" [nodes.Emptiness] title = "Emptiness" -id = "Emptiness" body = """ Emptiness is the vacuous base in which entities exist. """ @@ -108,7 +100,6 @@ links = [ "Entity" ] [nodes.Entity] title = "Entity" -id = "Entity" body = """ An entity is anything except for actual emptiness. It does not have to be sentient, or physical. It can be an idea, a concept, a memory. The concept of emptiness is an entity, but emptiness itself is not. """