use std::cmp::Ordering;
use std::fmt;
use std::fmt::{Debug, Display};
use std::hash::{Hash, Hasher};
#[derive(Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Node<T: Send, A> {
pub name: T,
pub attributes: Option<A>,
}
impl<T, A> Node<T, A>
where
T: Eq + Clone + PartialOrd + Ord + Hash + Send + Sync + Display,
A: Clone,
{
pub fn from_name(name: T) -> Node<T, A> {
Node {
name,
attributes: None,
}
}
pub fn from_name_and_attributes(name: T, attributes: A) -> Node<T, A>
where
T: Hash + Eq + Clone,
{
Node {
name,
attributes: Some(attributes),
}
}
}
impl<T: Eq + Ord + Send + Sync, A> Ord for Node<T, A> {
fn cmp(&self, other: &Self) -> Ordering {
self.name.cmp(&other.name)
}
}
impl<T: Eq + PartialOrd + Ord + Send + Sync, A> PartialOrd for Node<T, A> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl<T: std::cmp::PartialEq + Send + Sync, A> PartialEq for Node<T, A> {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
}
}
impl<T: Eq + Send + Sync, A> Eq for Node<T, A> {}
impl<T: Debug + Send + Sync, A> fmt::Debug for Node<T, A> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Node").field("name", &self.name).finish()
}
}
impl<T: Display + Send + Sync, A> fmt::Display for Node<T, A> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.name)
}
}
impl<T: Hash + Send + Sync, A> Hash for Node<T, A> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.name.hash(state);
}
}