#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HexEdge {
pub source: crate::graph::node_id::NodeId,
pub target: crate::graph::node_id::NodeId,
pub relationship: crate::graph::relationship::Relationship,
pub metadata: std::collections::HashMap<String, String>,
}
impl HexEdge {
pub fn new(
source: crate::graph::node_id::NodeId,
target: crate::graph::node_id::NodeId,
relationship: crate::graph::relationship::Relationship,
) -> Self {
Self {
source,
target,
relationship,
metadata: std::collections::HashMap::new(),
}
}
pub fn with_metadata(
source: crate::graph::node_id::NodeId,
target: crate::graph::node_id::NodeId,
relationship: crate::graph::relationship::Relationship,
metadata: std::collections::HashMap<String, String>,
) -> Self {
Self {
source,
target,
relationship,
metadata,
}
}
pub fn source(&self) -> &crate::graph::node_id::NodeId {
&self.source
}
pub fn target(&self) -> &crate::graph::node_id::NodeId {
&self.target
}
pub fn relationship(&self) -> crate::graph::relationship::Relationship {
self.relationship
}
pub fn get_metadata(&self, key: &str) -> Option<&String> {
self.metadata.get(key)
}
pub fn has_relationship(&self, rel: crate::graph::relationship::Relationship) -> bool {
self.relationship == rel
}
pub fn connects(
&self,
from: &crate::graph::node_id::NodeId,
to: &crate::graph::node_id::NodeId,
) -> bool {
&self.source == from && &self.target == to
}
}
impl std::fmt::Display for HexEdge {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{} --[{}]--> {}",
self.source, self.relationship, self.target
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_hex_edge_creation() {
let source = crate::graph::node_id::NodeId::from_name("Source");
let target = crate::graph::node_id::NodeId::from_name("Target");
let edge = HexEdge::new(
source.clone(),
target.clone(),
crate::graph::relationship::Relationship::Depends,
);
assert_eq!(edge.source(), &source);
assert_eq!(edge.target(), &target);
assert_eq!(
edge.relationship(),
crate::graph::relationship::Relationship::Depends
);
}
#[test]
fn test_hex_edge_with_metadata() {
let mut metadata = std::collections::HashMap::new();
metadata.insert(String::from("strength"), String::from("strong"));
let edge = HexEdge::with_metadata(
crate::graph::node_id::NodeId::from_name("A"),
crate::graph::node_id::NodeId::from_name("B"),
crate::graph::relationship::Relationship::Depends,
metadata,
);
assert_eq!(edge.get_metadata("strength"), Some(&String::from("strong")));
}
#[test]
fn test_edge_predicates() {
let source = crate::graph::node_id::NodeId::from_name("Source");
let target = crate::graph::node_id::NodeId::from_name("Target");
let edge = HexEdge::new(
source.clone(),
target.clone(),
crate::graph::relationship::Relationship::Implements,
);
assert!(edge.has_relationship(crate::graph::relationship::Relationship::Implements));
assert!(!edge.has_relationship(crate::graph::relationship::Relationship::Depends));
assert!(edge.connects(&source, &target));
assert!(!edge.connects(&target, &source));
}
#[test]
fn test_edge_display() {
let edge = HexEdge::new(
crate::graph::node_id::NodeId::from_name("A"),
crate::graph::node_id::NodeId::from_name("B"),
crate::graph::relationship::Relationship::Implements,
);
let display = format!("{}", edge);
assert!(display.contains("Implements"));
assert!(display.contains("-->"));
}
}