#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HexNode {
pub id: crate::graph::node_id::NodeId,
pub layer: crate::graph::layer::Layer,
pub role: crate::graph::role::Role,
pub type_name: String,
pub module_path: String,
pub metadata: std::collections::HashMap<String, String>,
}
impl HexNode {
pub fn new(
id: crate::graph::node_id::NodeId,
layer: crate::graph::layer::Layer,
role: crate::graph::role::Role,
type_name: &str,
module_path: &str,
) -> Self {
Self {
id,
layer,
role,
type_name: String::from(type_name),
module_path: String::from(module_path),
metadata: std::collections::HashMap::new(),
}
}
pub fn with_metadata(
id: crate::graph::node_id::NodeId,
layer: crate::graph::layer::Layer,
role: crate::graph::role::Role,
type_name: &str,
module_path: &str,
metadata: std::collections::HashMap<String, String>,
) -> Self {
Self {
id,
layer,
role,
type_name: String::from(type_name),
module_path: String::from(module_path),
metadata,
}
}
pub fn id(&self) -> &crate::graph::node_id::NodeId {
&self.id
}
pub fn layer(&self) -> crate::graph::layer::Layer {
self.layer
}
pub fn role(&self) -> crate::graph::role::Role {
self.role
}
pub fn type_name(&self) -> &str {
&self.type_name
}
pub fn module_path(&self) -> &str {
&self.module_path
}
pub fn get_metadata(&self, key: &str) -> Option<&String> {
self.metadata.get(key)
}
pub fn is_in_layer(&self, layer: crate::graph::layer::Layer) -> bool {
self.layer == layer
}
pub fn has_role(&self, role: crate::graph::role::Role) -> bool {
self.role == role
}
}
impl std::fmt::Display for HexNode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}::{} ({} in {})",
self.type_name, self.role, self.layer, self.module_path
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_hex_node_creation() {
let node = HexNode::new(
crate::graph::node_id::NodeId::from_name("TestEntity"),
crate::graph::layer::Layer::Domain,
crate::graph::role::Role::Entity,
"TestEntity",
"test::domain",
);
assert_eq!(node.type_name(), "TestEntity");
assert_eq!(node.module_path(), "test::domain");
assert_eq!(node.layer(), crate::graph::layer::Layer::Domain);
assert_eq!(node.role(), crate::graph::role::Role::Entity);
}
#[test]
fn test_hex_node_with_metadata() {
let mut metadata = std::collections::HashMap::new();
metadata.insert(String::from("version"), String::from("1.0"));
let node = HexNode::with_metadata(
crate::graph::node_id::NodeId::from_name("TestEntity"),
crate::graph::layer::Layer::Domain,
crate::graph::role::Role::Entity,
"TestEntity",
"test::domain",
metadata,
);
assert_eq!(node.get_metadata("version"), Some(&String::from("1.0")));
assert_eq!(node.get_metadata("missing"), None);
}
#[test]
fn test_node_predicates() {
let node = HexNode::new(
crate::graph::node_id::NodeId::from_name("TestEntity"),
crate::graph::layer::Layer::Domain,
crate::graph::role::Role::Entity,
"TestEntity",
"test::domain",
);
assert!(node.is_in_layer(crate::graph::layer::Layer::Domain));
assert!(!node.is_in_layer(crate::graph::layer::Layer::Port));
assert!(node.has_role(crate::graph::role::Role::Entity));
assert!(!node.has_role(crate::graph::role::Role::Repository));
}
#[test]
fn test_node_display() {
let node = HexNode::new(
crate::graph::node_id::NodeId::from_name("TestEntity"),
crate::graph::layer::Layer::Domain,
crate::graph::role::Role::Entity,
"TestEntity",
"test::domain",
);
let display = format!("{}", node);
assert!(display.contains("TestEntity"));
assert!(display.contains("Entity"));
assert!(display.contains("Domain"));
}
}