use super::{Edge, Graph, Relation};
use petgraph::{dot::Dot, visit::EdgeRef as _};
use core::fmt::{Debug, Display, Formatter};
impl Display for Graph {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
let config = [
petgraph::dot::Config::EdgeNoLabel,
petgraph::dot::Config::NodeNoLabel,
];
Dot::with_attr_getters(
self.dag.graph(),
&config,
&|_g, i| {
let passed = self.passed(i.id());
let trigger = self.is_trigger(i.id());
format!(
"tooltip={:?},comment={source:?},{arrows},color={color}",
i.weight().to_string(),
source = i.weight().source,
color = match passed {
Some(true) => "green",
Some(false) => "red",
None => "black",
},
arrows = match trigger {
true => "dir=back,arrowtail=inv",
false => "arrowhead=odot",
}
)
},
&|_g, (_i, job)| {
let status = job.get_status();
format!(
"label={:?},comment={source:?},shape=record,style=rounded,color={color}",
format!(
"{{ {} | {} }}",
job.job.id.to_string(),
match status {
Some(code) => format!("{code}"),
None => "-".to_string(),
}
),
source = job.job.source,
color = match status {
None if job.is_running() => "cyan",
None => "gray",
Some(_) => "black",
}
)
},
)
.fmt(f)
}
}
impl core::fmt::Display for Relation {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
match self {
Relation::Trigger => f.write_str("=>"),
Relation::Dependency => f.write_str("K="),
}
}
}
impl Display for Edge {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
write!(f, "{} {} {}", self.origin, self.relation, self.reference)?;
for code in &self.codes {
write!(f, "\n{}", code)?;
}
Ok(())
}
}