use crate::graph::node::Node;
use crate::graph::GraphDefinition;
use crate::identify::{Identifiable, Identity};
use std::cmp::Ordering;
use std::fmt::{Display, Formatter};
use std::marker::PhantomData;
use std::rc::Rc;
pub struct Edge<T: GraphDefinition> {
pub origin: Node<T>,
pub target: Node<T>,
pub meta: Rc<T::EdgeMeta>,
pub graph_type: PhantomData<T>,
}
impl<T: GraphDefinition> Edge<T> {
pub fn new(from: Node<T>, to: Node<T>, meta: Rc<T::EdgeMeta>) -> Self {
Self {
origin: from,
target: to,
meta,
graph_type: Default::default(),
}
}
}
impl<T: GraphDefinition> Clone for Edge<T> {
fn clone(&self) -> Self {
Self {
origin: self.origin.clone(),
target: self.target.clone(),
meta: Rc::clone(&self.meta),
graph_type: Default::default(),
}
}
}
impl<T: GraphDefinition> PartialEq for Edge<T> {
fn eq(&self, other: &Self) -> bool {
self.get_id() == other.get_id()
}
}
impl<T: GraphDefinition> PartialOrd for Edge<T> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.get_id().partial_cmp(&other.get_id())
}
}
impl<T: GraphDefinition> Eq for Edge<T> {}
impl<T: GraphDefinition> Ord for Edge<T> {
fn cmp(&self, other: &Self) -> Ordering {
self.get_id().cmp(&other.get_id())
}
}
#[derive(Clone, PartialOrd, PartialEq, Eq, Ord, Hash, Debug)]
pub struct EdgeIdentity<I: Identity>(I, I);
impl<I: Identity> Display for EdgeIdentity<I> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "[{} -> {} ]", &self.0, &self.1)
}
}
impl<I: Identity> Identity for EdgeIdentity<I> {}
impl<T> Identifiable<EdgeIdentity<T::Id>> for Edge<T>
where
T: GraphDefinition,
{
fn get_id(&self) -> EdgeIdentity<T::Id> {
EdgeIdentity(self.origin.get_id(), self.target.get_id())
}
}