1use 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 GraphBearing::nested(&self.container)
71 }
72}
73
74fn 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 Some(iri) => RelationClass::open(iri.to_string()),
103 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 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 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 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}