Implement automatic IDs

This commit is contained in:
Juno Takano 2025-12-10 09:58:44 -03:00
commit 400eb02efc
5 changed files with 26 additions and 32 deletions

View file

@ -7,8 +7,9 @@ It works by ingesting a TOML file containing your node specification and serving
## Roadmap ## Roadmap
- [x] Array syntax for lightweight connections - [x] Array syntax for lightweight connections
- [x] Automatic IDs
- [ ] Automatic titles
- [ ] Automatic anchors - [ ] Automatic anchors
- [ ] Automatic IDs
- [ ] Mismatch between TOML ID and provided ID - [ ] Mismatch between TOML ID and provided ID
## Motivation ## Motivation

View file

@ -55,6 +55,7 @@ fn modulate_nodes(old_nodes: HashMap<String, Node>) -> HashMap<String, Node> {
} }
let new_node = Node { let new_node = Node {
id: key.clone(),
connections: Some(new_edges), connections: Some(new_edges),
..node.clone() ..node.clone()
}; };

View file

@ -95,11 +95,11 @@ async fn node_view(Path(id): Path<String>) -> impl IntoResponse {
let node: &Node = nodes.get(&id).unwrap_or(&empty_node); 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("title", &node.title);
context.insert("body", &node.body); context.insert("body", &node.body);
context.insert("connections", &node.connections.clone()); context.insert("connections", &node.connections.clone());
context.insert("incoming", &graph.incoming.get(&node.id)); context.insert("incoming", &graph.incoming.get(&id));
template_handler( template_handler(
"node.html", "node.html",
@ -109,9 +109,9 @@ async fn node_view(Path(id): Path<String>) -> impl IntoResponse {
r#"Failed to generate page for node {} (ID {}) with {} outgoing, r#"Failed to generate page for node {} (ID {}) with {} outgoing,
{} incoming connections and body "{}""#, {} incoming connections and body "{}""#,
node.title, node.title,
node.id, id,
node.connections.iter().len(), node.connections.iter().len(),
graph.incoming.get(&node.id).iter().len(), graph.incoming.get(&id).iter().len(),
node.body, node.body,
), ),
) )
@ -121,7 +121,7 @@ async fn index() -> Html<String> {
let mut context = tera::Context::new(); let mut context = tera::Context::new();
let graph = populate_graph(); let graph = populate_graph();
let root_node = graph.get_root(); let root_node = graph.get_root().unwrap_or_default();
let nodes: Vec<Node> = graph.nodes.into_values().collect(); let nodes: Vec<Node> = graph.nodes.into_values().collect();
context.insert("nodes", &nodes); context.insert("nodes", &nodes);
@ -134,7 +134,7 @@ async fn tree() -> Html<String> {
let mut context = tera::Context::new(); let mut context = tera::Context::new();
let graph = populate_graph(); let graph = populate_graph();
let root_node = graph.get_root(); let root_node = graph.get_root().unwrap_or_default();
let nodes: Vec<Node> = graph.nodes.into_values().collect(); let nodes: Vec<Node> = graph.nodes.into_values().collect();
context.insert("nodes", &nodes); context.insert("nodes", &nodes);

View file

@ -1,7 +1,7 @@
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
use std::collections::HashMap; use std::collections::HashMap;
#[derive(Serialize, Deserialize, Clone, Default)] #[derive(Serialize, Deserialize, Clone, Default, Debug)]
pub struct Graph { pub struct Graph {
pub messages: Vec<String>, pub messages: Vec<String>,
pub root_node: String, pub root_node: String,
@ -10,7 +10,19 @@ pub struct Graph {
pub incoming: HashMap<String, Vec<Edge>>, pub incoming: HashMap<String, Vec<Edge>>,
} }
#[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<Vec<Edge>>,
#[serde(default)]
pub links: Vec<String>,
#[serde(default)]
pub id: String,
}
#[derive(Serialize, Clone, Default, PartialEq, Deserialize, Debug)]
pub struct Edge { pub struct Edge {
pub to: String, pub to: String,
#[serde(default)] #[serde(default)]
@ -21,22 +33,11 @@ pub struct Edge {
pub detached: bool, 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<Vec<Edge>>,
#[serde(default)]
pub links: Vec<String>,
}
impl Graph { impl Graph {
pub fn new(message: Option<String>) -> Graph { pub fn new(message: Option<String>) -> Graph {
Self { Self {
nodes: HashMap::new(), nodes: HashMap::new(),
root_node: "".to_string(), root_node: "VoidNode".to_string(),
incoming: HashMap::new(), incoming: HashMap::new(),
messages: vec![message messages: vec![message
.unwrap_or("This graph is empty or in error".to_string())], .unwrap_or("This graph is empty or in error".to_string())],
@ -54,8 +55,8 @@ impl Graph {
impl Node { impl Node {
pub fn new(message: Option<String>) -> Node { pub fn new(message: Option<String>) -> Node {
Self { Self {
title: "Empty Node".to_string(), id: "VoidNode".to_string(),
id: "EmptyNode".to_string(), title: "Pure Void".to_string(),
body: match message { body: match message {
Some(s) => s, Some(s) => s,
None => "Node is empty, missing or wasn't found.".to_string() None => "Node is empty, missing or wasn't found.".to_string()

View file

@ -3,7 +3,6 @@ root_node = "Interface"
[nodes.Interface] [nodes.Interface]
title = "Interface" title = "Interface"
id = "Interface"
body = """ body = """
An interface is a point of contact between the inside and the outside of something. Contrast with intraface. 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] [nodes.Intraface]
title = "Intraface" title = "Intraface"
id = "Intraface"
body = """ body = """
The intraface is the reflexive process of communicating, creating, thinking, that does not or cannot get shared with others. Contrast with interface. 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] [nodes.Thinking]
title = "Thinking" title = "Thinking"
id = "Thinking"
body = """ body = """
Thinking is a process by which some beings create and manipulate mental constructs. Thinking is a process by which some beings create and manipulate mental constructs.
""" """
[nodes.Paradigm] [nodes.Paradigm]
title = "Paradigm" title = "Paradigm"
id = "Paradigm"
body = """ 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. 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] [nodes.Principle]
title = "Principle" title = "Principle"
id = "Principle"
body = """ body = """
A principle is a belief that implies commitment and necessity. A principle is a belief that implies commitment and necessity.
@ -56,7 +51,6 @@ to = "Identity"
[nodes.Religion] [nodes.Religion]
title = "Religion" title = "Religion"
id = "Religion"
body = """ body = """
A religion is a paradigm that involves unfalsifiable beliefs, particularly those in the domain of morality. A religion is a paradigm that involves unfalsifiable beliefs, particularly those in the domain of morality.
@ -86,7 +80,6 @@ links = [
[nodes.Identity] [nodes.Identity]
title = "Identity" title = "Identity"
id = "Identity"
body = """ body = """
Identity is how individuals construe their sameness and otherness from each other and from nothingness. Identity is how individuals construe their sameness and otherness from each other and from nothingness.
""" """
@ -99,7 +92,6 @@ to = "Emptiness"
[nodes.Emptiness] [nodes.Emptiness]
title = "Emptiness" title = "Emptiness"
id = "Emptiness"
body = """ body = """
Emptiness is the vacuous base in which entities exist. Emptiness is the vacuous base in which entities exist.
""" """
@ -108,7 +100,6 @@ links = [ "Entity" ]
[nodes.Entity] [nodes.Entity]
title = "Entity" title = "Entity"
id = "Entity"
body = """ 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. 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.
""" """