Skip to main content

rudof_rdf/rdf_core/visualizer/
visual_rdf_edge.rs

1use crate::rdf_core::vocabs::RdfVocab;
2use crate::rdf_core::{Rdf, term::Iri};
3use std::fmt::Display;
4
5/// Represents an edge in a visual RDF graph.
6///
7/// Edges connect nodes and can represent RDF predicates or special relationships
8/// like reification.
9#[derive(Debug, Clone, PartialEq, Eq, Hash)]
10pub enum VisualRDFEdge {
11    /// An edge representing an IRI predicate with a label and URL.
12    Iri {
13        /// The display label for the IRI
14        label: String,
15        /// The full IRI URL
16        url: String,
17    },
18    /// A special edge representing reification (rdf:reifies).
19    Reifies,
20}
21
22impl VisualRDFEdge {
23    /// Creates a `VisualRDFEdge` from an RDF IRI.
24    ///
25    /// If the IRI is the special `rdf:reifies` predicate, returns `Reifies`.
26    /// Otherwise, creates an `Iri` variant with qualified label and URL.
27    ///
28    /// # Arguments
29    /// * `rdf` - The RDF context for IRI qualification
30    /// * `iri` - The IRI to convert
31    ///
32    /// # Returns
33    /// * `VisualRDFEdge` - The corresponding visual edge
34    pub fn from_iri<R: Rdf>(rdf: &R, iri: &R::IRI) -> Self {
35        if iri.as_str() == RdfVocab::RDF_REIFIES {
36            return VisualRDFEdge::Reifies;
37        }
38        let iri_label = R::qualify_iri(rdf, iri);
39        let iri_str = (*iri).as_str().to_string();
40        VisualRDFEdge::Iri {
41            label: iri_label,
42            url: iri_str,
43        }
44    }
45
46    /// Converts the edge to a PlantUML link format.
47    ///
48    /// # Returns
49    /// * `String` - The PlantUML link representation
50    pub fn as_plantuml_link(&self) -> String {
51        match self {
52            VisualRDFEdge::Iri { label, url } => format!("[[{url} {label}]]"),
53            VisualRDFEdge::Reifies => format!("[[{} {}]]", RdfVocab::RDF_REIFIES, "reifies"),
54        }
55    }
56
57    /// Gets the display label for the edge.
58    ///
59    /// # Returns
60    /// * `String` - The label string
61    pub fn label(&self) -> String {
62        match self {
63            VisualRDFEdge::Iri { label, .. } => label.clone(),
64            VisualRDFEdge::Reifies => "reifies".to_string(),
65        }
66    }
67}
68
69impl Display for VisualRDFEdge {
70    /// Formats the edge for display purposes.
71    ///
72    /// Shows the label and URL for IRI edges, or just "reifies" for reification edges.
73    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
74        match self {
75            VisualRDFEdge::Iri { label, url } => write!(f, "{label} ({url})"),
76            VisualRDFEdge::Reifies => write!(f, "reifies"),
77        }
78    }
79}