use chartulary::{
Address, Addressed, Classified, ContentBearing, GraphBearing, Identified, Labeled, Predicated,
RelationClass,
};
use uuid::Uuid;
use super::edge_data::predicate_iri;
use super::edge_payload::EdgePayload;
use super::node::Node;
impl Identified for Node {
type Id = Uuid;
fn id(&self) -> &Uuid {
&self.id
}
}
impl Addressed for Node {
fn addresses(&self) -> Vec<Address> {
Addressed::addresses(&self.container)
}
}
impl Labeled for Node {
fn title(&self) -> Option<&str> {
Labeled::title(&self.container)
}
fn tags(&self) -> Vec<String> {
self.tags.iter().cloned().collect()
}
}
impl ContentBearing for Node {
fn content(&self) -> Option<muniment::Hash> {
ContentBearing::content(&self.container)
}
fn media_type(&self) -> Option<&str> {
ContentBearing::media_type(&self.container)
}
}
impl GraphBearing for Node {
fn nested(&self) -> Option<&codicil::LogId> {
GraphBearing::nested(&self.container)
}
}
fn edge_predicate(payload: &EdgePayload) -> Option<&str> {
let semantic = payload.semantic.as_ref()?;
if let Some(statement) = semantic.statements.first() {
Some(statement.predicate.as_str())
} else if let Some(predicate) = &semantic.predicate {
Some(predicate.as_str())
} else if let Some(&sub_kind) = semantic.sub_kinds.iter().next() {
Some(predicate_iri(sub_kind))
} else {
None
}
}
impl Predicated for EdgePayload {
fn predicate(&self) -> Option<&str> {
edge_predicate(self)
}
}
impl Classified for EdgePayload {
fn class(&self) -> RelationClass {
match edge_predicate(self) {
Some(iri) => RelationClass::open(iri.to_string()),
None => RelationClass::app("mere", 0),
}
}
}
#[cfg(test)]
mod tests {
use super::super::edge_taxonomy::{EdgeAssertion, SemanticSubKind};
use super::*;
#[test]
fn web_node_satisfies_the_container_capabilities() {
fn assert_caps<N: Identified<Id = Uuid> + Addressed + Labeled + ContentBearing>() {}
assert_caps::<Node>();
}
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn a_web_node_projects_its_identity_address_and_label() {
let mut node = Node::test_stub("https://example.test/paper");
node.title = "A Paper".to_string();
node.tags.insert("research".to_string());
assert_eq!(Identified::id(&node), &node.id);
assert_eq!(
Addressed::primary_address(&node).unwrap().as_str(),
"https://example.test/paper"
);
assert_eq!(Labeled::title(&node), Some("A Paper"));
assert_eq!(Labeled::tags(&node), vec!["research".to_string()]);
}
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn a_web_node_reports_its_body_as_content() {
let mut node = Node::test_stub("mere://note/x");
assert_eq!(
ContentBearing::content(&node),
None,
"no body: no graph-owned content (a bare web tab's content lives in the cache)"
);
node.body = Some("# A note".to_string());
node.media_type = Some("text/markdown".to_string());
assert_eq!(
ContentBearing::content(&node),
Some(muniment::Hash::of(b"# A note")),
"content is the body's blake3 hash, the muniment blob address"
);
assert_eq!(ContentBearing::media_type(&node), Some("text/markdown"));
}
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn a_bearing_node_reports_its_nested_graph() {
let mut node = Node::test_stub("mere://denizen/ab12");
assert_eq!(
GraphBearing::nested(&node),
None,
"an ordinary node bears no graph"
);
node.nested = Some(codicil::LogId::new("denizens/trail-keeper"));
assert_eq!(
GraphBearing::nested(&node).map(codicil::LogId::as_str),
Some("denizens/trail-keeper"),
"containment is structural: the borne world reads off the node itself"
);
}
#[test]
fn semantic_edges_project_experience_edges_do_not() {
let mut cites = EdgePayload::new();
cites.assert_relation(EdgeAssertion::Semantic {
sub_kind: SemanticSubKind::Cites,
label: None,
decay_progress: None,
});
assert_eq!(
Predicated::predicate(&cites),
Some(predicate_iri(SemanticSubKind::Cites))
);
assert!(
Classified::class(&cites).predicate().is_some(),
"a semantic edge projects"
);
let experience = EdgePayload::new();
assert_eq!(Predicated::predicate(&experience), None);
assert!(
Classified::class(&experience).predicate().is_none(),
"an experience edge stays private"
);
}
}