use std::{cmp::Ordering, fmt::Display};
use serde::{Deserialize, Serialize};
use crate::{GraphStyle, NodeId};
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct Subgraph {
title: Option<String>,
pub(crate) nodes: Vec<NodeId>,
style: Option<GraphStyle>,
}
impl Subgraph {
pub fn new() -> Self {
Self { ..Default::default() }
}
pub fn from<T: Display>(title: T) -> Self {
Self {
title: Some(title.to_string()),
..Default::default()
}
}
pub fn from_nodes<I: IntoIterator<Item = NodeId>>(nodes: I) -> Self {
Self {
nodes: nodes.into_iter().collect(),
..Default::default()
}
}
pub fn title<T: Display>(mut self, title: T) -> Self {
self.title = Some(title.to_string());
self
}
pub fn nodes<I: IntoIterator<Item = NodeId>>(mut self, nodes: I) -> Self {
let mut nodes: Vec<_> = nodes.into_iter().collect();
nodes.reverse();
self.nodes = nodes;
self
}
pub fn style(mut self, graphstyle: GraphStyle) -> Self {
self.style = Some(graphstyle);
self
}
pub fn add<I: IntoIterator<Item = NodeId>>(&mut self, nodes: I) {
self.nodes.extend(nodes);
}
pub fn get_title(&self) -> Option<&str> {
self.title.as_deref()
}
pub fn get_nodes(&self) -> &Vec<NodeId> {
&self.nodes
}
pub fn get_style(&self) -> Option<&GraphStyle> {
self.style.as_ref()
}
}
impl Eq for Subgraph {}
impl PartialEq for Subgraph {
fn eq(&self, other: &Self) -> bool {
self.title == other.title && self.nodes == other.nodes
}
}
impl Ord for Subgraph {
fn cmp(&self, other: &Self) -> Ordering {
self.title.cmp(&other.title).then_with(|| self.nodes.cmp(&other.nodes))
}
}
impl PartialOrd for Subgraph {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}