use std::collections::HashMap;
pub use crate::core::view::NodeId;
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Edge<W, E> {
pub from: NodeId,
pub to: NodeId,
pub weight: W,
pub data: E,
pub attributes: HashMap<String, String>,
}
impl<W: Default, E: Default> Default for Edge<W, E> {
fn default() -> Self {
Edge {
from: NodeId::from_index(0),
to: NodeId::from_index(0),
weight: W::default(),
data: E::default(),
attributes: HashMap::new(),
}
}
}
impl<W, E> Edge<W, E> {
pub fn new(from: NodeId, to: NodeId, weight: W, data: E) -> Self {
Edge {
from,
to,
weight,
data,
attributes: HashMap::new(),
}
}
pub fn get_attribute(&self, key: &str) -> Option<&String> {
self.attributes.get(key)
}
pub fn set_attribute(&mut self, key: String, value: String) -> Option<String> {
self.attributes.insert(key, value)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::core::view::NodeId;
#[test]
fn test_edge_creation() {
let edge = Edge::<i32, &str>::new(
NodeId::from_index(0),
NodeId::from_index(1),
10,
"connection",
);
assert_eq!(edge.from, NodeId::from_index(0));
assert_eq!(edge.to, NodeId::from_index(1));
assert_eq!(edge.weight, 10);
assert_eq!(edge.data, "connection");
assert!(edge.attributes.is_empty());
}
#[test]
fn test_default_edge() {
let edge: Edge<i32, String> = Edge::default();
assert_eq!(edge.from, NodeId::from_index(0));
assert_eq!(edge.to, NodeId::from_index(0));
assert_eq!(edge.weight, 0);
assert_eq!(edge.data, String::new());
assert!(edge.attributes.is_empty());
}
#[test]
fn test_attributes() {
let mut edge =
Edge::<f32, &str>::new(NodeId::from_index(1), NodeId::from_index(2), 3.5, "path");
edge.set_attribute("color".to_string(), "blue".to_string());
assert_eq!(edge.get_attribute("color"), Some(&"blue".to_string()));
assert_eq!(edge.get_attribute("size"), None);
}
#[test]
fn test_edge_equality() {
let edge1 = Edge::<u32, &str>::new(NodeId::from_index(0), NodeId::from_index(1), 5, "link");
let edge2 = Edge::<u32, &str>::new(NodeId::from_index(0), NodeId::from_index(1), 5, "link");
assert_eq!(edge1, edge2);
}
}