Skip to main content

kernel/graph/
chart.rs

1// Copyright 2026 Mark AB (markik)
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4//! chartulary capability-trait impls for the web [`Node`] (graph re-base, G5).
5//!
6//! mere's `Node` is the canonical *foreign* implementor of the chartulary
7//! container capabilities: its `id` is the stable identity, its address claims map
8//! to scheme-qualified addresses, and its title and tags are the curated labels.
9//! This is the first step of the graph re-base: the real web node satisfies the
10//! generic substrate's node traits, so the substrate can hold and project it. The
11//! browser-runtime facets (favicon, viewer routing, session restore, lifecycle)
12//! stay on `Node` and simply do not participate in the generic capabilities.
13//!
14//! `ContentBearing` reports the node's inline authored body (`Node::body`, a knot
15//! note's djot) as content: `content()` is the blake3 `muniment::Hash` of the body
16//! bytes, the address that body would have in a muniment `BlobStore`. So the node
17//! names its content's identity even though the bytes still live inline. Moving the
18//! bytes out-of-line into an actual blob store (and content-addressing fetched web
19//! pages, which live in mere's own cache, not on the node) is the follow-on step.
20
21use chartulary::{
22    Address, Addressed, Classified, ContentBearing, GraphBearing, Identified, Labeled, Predicated,
23    RelationClass,
24};
25use uuid::Uuid;
26
27use super::edge_data::predicate_iri;
28use super::edge_payload::EdgePayload;
29use super::node::Node;
30
31impl Identified for Node {
32    type Id = Uuid;
33
34    fn id(&self) -> &Uuid {
35        &self.id
36    }
37}
38
39impl Addressed for Node {
40    fn addresses(&self) -> Vec<Address> {
41        Addressed::addresses(&self.container)
42    }
43}
44
45impl Labeled for Node {
46    fn title(&self) -> Option<&str> {
47        Labeled::title(&self.container)
48    }
49
50    fn tags(&self) -> Vec<String> {
51        self.tags.iter().cloned().collect()
52    }
53}
54
55impl ContentBearing for Node {
56    fn content(&self) -> Option<muniment::Hash> {
57        ContentBearing::content(&self.container)
58    }
59
60    fn media_type(&self) -> Option<&str> {
61        ContentBearing::media_type(&self.container)
62    }
63}
64
65impl GraphBearing for Node {
66    fn nested(&self) -> Option<&codicil::LogId> {
67        // Structural containment (the one-node ruling): the node BEARS the
68        // graph named by this log identity. A denizen's inner world hangs
69        // here; agency (subject + kind) stays a facet.
70        GraphBearing::nested(&self.container)
71    }
72}
73
74/// The single predicate IRI a semantic edge projects, or `None` if the edge
75/// carries no semantic relation (an experience-layer edge: Traversal, Containment,
76/// Arrangement, Imported). Precedence matches linked-data: an explicit statement or
77/// open predicate wins, else the first recognized sub-kind's canonical IRI.
78fn edge_predicate(payload: &EdgePayload) -> Option<&str> {
79    let semantic = payload.semantic.as_ref()?;
80    if let Some(statement) = semantic.statements.first() {
81        Some(statement.predicate.as_str())
82    } else if let Some(predicate) = &semantic.predicate {
83        Some(predicate.as_str())
84    } else if let Some(&sub_kind) = semantic.sub_kinds.iter().next() {
85        Some(predicate_iri(sub_kind))
86    } else {
87        None
88    }
89}
90
91impl Predicated for EdgePayload {
92    fn predicate(&self) -> Option<&str> {
93        edge_predicate(self)
94    }
95}
96
97impl Classified for EdgePayload {
98    fn class(&self) -> RelationClass {
99        match edge_predicate(self) {
100            // A semantic edge joins the shared ring as an open predicate (its IRI),
101            // so it projects to RDF.
102            Some(iri) => RelationClass::open(iri.to_string()),
103            // An experience-layer edge is mere's private family: it does not project.
104            None => RelationClass::app("mere", 0),
105        }
106    }
107}
108
109#[cfg(test)]
110mod tests {
111    use super::super::edge_taxonomy::{EdgeAssertion, SemanticSubKind};
112    use super::*;
113
114    #[test]
115    fn web_node_satisfies_the_container_capabilities() {
116        // A compile-time proof that mere's Node implements the substrate's node
117        // capability traits.
118        fn assert_caps<N: Identified<Id = Uuid> + Addressed + Labeled + ContentBearing>() {}
119        assert_caps::<Node>();
120    }
121
122    #[cfg(not(target_arch = "wasm32"))]
123    #[test]
124    fn a_web_node_projects_its_identity_address_and_label() {
125        let mut node = Node::test_stub("https://example.test/paper");
126        node.title = "A Paper".to_string();
127        node.tags.insert("research".to_string());
128
129        assert_eq!(Identified::id(&node), &node.id);
130        assert_eq!(
131            Addressed::primary_address(&node).unwrap().as_str(),
132            "https://example.test/paper"
133        );
134        assert_eq!(Labeled::title(&node), Some("A Paper"));
135        assert_eq!(Labeled::tags(&node), vec!["research".to_string()]);
136    }
137
138    #[cfg(not(target_arch = "wasm32"))]
139    #[test]
140    fn a_web_node_reports_its_body_as_content() {
141        let mut node = Node::test_stub("mere://note/x");
142        assert_eq!(
143            ContentBearing::content(&node),
144            None,
145            "no body: no graph-owned content (a bare web tab's content lives in the cache)"
146        );
147        node.body = Some("# A note".to_string());
148        node.media_type = Some("text/markdown".to_string());
149        assert_eq!(
150            ContentBearing::content(&node),
151            Some(muniment::Hash::of(b"# A note")),
152            "content is the body's blake3 hash, the muniment blob address"
153        );
154        assert_eq!(ContentBearing::media_type(&node), Some("text/markdown"));
155    }
156
157    #[cfg(not(target_arch = "wasm32"))]
158    #[test]
159    fn a_bearing_node_reports_its_nested_graph() {
160        let mut node = Node::test_stub("mere://denizen/ab12");
161        assert_eq!(
162            GraphBearing::nested(&node),
163            None,
164            "an ordinary node bears no graph"
165        );
166        node.nested = Some(codicil::LogId::new("denizens/trail-keeper"));
167        assert_eq!(
168            GraphBearing::nested(&node).map(codicil::LogId::as_str),
169            Some("denizens/trail-keeper"),
170            "containment is structural: the borne world reads off the node itself"
171        );
172    }
173
174    #[test]
175    fn semantic_edges_project_experience_edges_do_not() {
176        // A semantic edge carries a predicate IRI and joins the RDF-projecting ring.
177        let mut cites = EdgePayload::new();
178        cites.assert_relation(EdgeAssertion::Semantic {
179            sub_kind: SemanticSubKind::Cites,
180            label: None,
181            decay_progress: None,
182        });
183        assert_eq!(
184            Predicated::predicate(&cites),
185            Some(predicate_iri(SemanticSubKind::Cites))
186        );
187        assert!(
188            Classified::class(&cites).predicate().is_some(),
189            "a semantic edge projects"
190        );
191
192        // An edge with no semantic relation is experience-layer: it does not project.
193        let experience = EdgePayload::new();
194        assert_eq!(Predicated::predicate(&experience), None);
195        assert!(
196            Classified::class(&experience).predicate().is_none(),
197            "an experience edge stays private"
198        );
199    }
200}