1use petgraph::dot::{Config, Dot};
2use petgraph::prelude::*;
3use petgraph::EdgeType;
4use std::fmt::Display;
5use std::process::Command;
6
7pub fn to_dot<N, E, Ty>(name: &str, graph: &Graph<N, E, Ty>)
8where
9 N: Display,
10 E: Display,
11 Ty: EdgeType,
12{
13 let path = format!("out/{}.dot", name);
14 let dot_graph = Dot::with_attr_getters(
15 graph,
16 &[Config::EdgeNoLabel, Config::NodeNoLabel],
17 &|_g, e| format!("label = \"{}\"", e.weight()),
18 &|_g, (i, u)| format!("label = \"{}: {}\"", i.index(), u),
19 );
20 std::fs::write(&path, format!("{}", dot_graph)).expect("Unable to write file");
21 Command::new("dot")
22 .args(["-Tpng", &path, "-O"])
23 .output()
24 .expect("Unable to execute dot command");
25}