use std::collections::HashMap;
pub type NodeId = usize;
#[derive(Debug)]
pub enum EdgeError {
GenericError(String),
}
impl std::fmt::Display for EdgeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
EdgeError::GenericError(msg) => write!(f, "Edge error: {}", msg),
}
}
}
impl std::error::Error for EdgeError {}
#[derive(Debug, Clone, PartialEq, Eq)]
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: 0,
to: 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) -> Result<(), EdgeError> {
self.attributes.insert(key, value);
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_edge_creation() {
let edge = Edge::<i32, &str>::new(0, 1, 10, "connection");
assert_eq!(edge.from, 0);
assert_eq!(edge.to, 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, 0);
assert_eq!(edge.to, 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(1, 2, 3.5, "path");
edge.set_attribute("color".to_string(), "blue".to_string())
.unwrap();
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(0, 1, 5, "link");
let edge2 = Edge::<u32, &str>::new(0, 1, 5, "link");
assert_eq!(edge1, edge2);
}
}