graphfind_rs/petgraph/
print.rs

1#[cfg(feature = "svg")]
2use graphviz_rust::cmd::{CommandArg, Format};
3use petgraph::dot::Dot;
4use std::fmt::Debug;
5#[cfg(feature = "svg")]
6use std::vec;
7
8use crate::graph::VizDotGraph;
9
10/// Print implementation for petgraph-type graphs.
11///
12/// Requires NodeWeight, EdgeWeight to posess the Debug trait.
13impl<NodeWeight, EdgeWeight, IndexType, Direction> VizDotGraph<NodeWeight, EdgeWeight>
14    for petgraph::graph::Graph<NodeWeight, EdgeWeight, Direction, IndexType>
15where
16    NodeWeight: Debug,
17    EdgeWeight: Debug,
18    IndexType: petgraph::graph::IndexType,
19    Direction: petgraph::EdgeType,
20{
21    /// Use petgraph's Dot struct to output the graph.
22    fn print(&self) -> String {
23        format!("{:?}", Dot::new(self))
24    }
25
26    /// Use the graphviz-rust to print the given graph into a .svg file.
27    ///
28    /// Requires a graphviz engine to be installed on the machine that runs
29    /// this function.
30    ///
31    /// Requires the `svg` feature of this crate to be enabled.
32    #[cfg(feature = "svg")]
33    fn print_to_svg(&self, path: &str) -> Result<String, std::io::Error> {
34        graphviz_rust::exec_dot(
35            self.print(),
36            vec![
37                CommandArg::Format(Format::Svg),
38                CommandArg::Output(path.to_string()),
39            ],
40        )
41    }
42}