paiagram_core/export/
graphviz.rs1use bevy::{ecs::system::RunSystemOnce, prelude::*};
2use petgraph::dot;
3
4use crate::graph::Graph;
5
6pub struct Graphviz<'a> {
7 world: &'a mut World,
8}
9
10impl<'a> super::ExportObject for Graphviz<'a> {
11 fn export_to_buffer(&mut self, buffer: &mut Vec<u8>) {
12 self.world
13 .run_system_once_with(make_dot_string, buffer)
14 .unwrap();
15 }
16 fn extension(&self) -> impl AsRef<str> {
17 ".dot"
18 }
19 fn filename(&self) -> impl AsRef<str> {
20 "diagram"
21 }
22}
23
24fn make_dot_string(InMut(buffer): InMut<Vec<u8>>, graph: Res<Graph>, names: Query<&Name>) {
25 let get_node_attr = |_, (_, entity): (_, &Entity)| {
26 format!(
27 r#"label = "{}""#,
28 names
29 .get(entity.entity())
30 .map_or("<Unknown>".to_string(), |name| name.to_string())
31 )
32 };
33 let get_edge_attr = |_, _| String::new();
34 let dot_string = dot::Dot::with_attr_getters(
35 &graph.map,
36 &[dot::Config::EdgeNoLabel, dot::Config::NodeNoLabel],
37 &get_edge_attr,
38 &get_node_attr,
39 );
40 buffer.clear();
41 buffer.extend_from_slice(&format!("{:?}", dot_string).into_bytes());
42}