Skip to content

Commit

Permalink
fix/dotgraph: write dot graph even if conversion to svg fails
Browse files Browse the repository at this point in the history
Signed-off-by: Bernhard Schuster <[email protected]>
  • Loading branch information
drahnr committed Sep 22, 2023
1 parent 9cf852b commit 6d5d8eb
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 25 deletions.
66 changes: 41 additions & 25 deletions orchestra/proc-macro/src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ impl<'a> ConnectionGraph<'a> {
dest: impl AsRef<std::path::Path>,
) -> anyhow::Result<()> {
use self::graph_helpers::color_scheme;
use fs_err as fs;

let dot_content = format!(
r#"digraph {{
Expand All @@ -201,32 +200,10 @@ node [colorscheme={}]
let dest = dest.as_ref();
let dest = dest.to_path_buf();

let svg_content = {
let mut parser = dotlay::gv::DotParser::new(dot_content.as_str());
let graph = parser.process().map_err(|err_msg| {
anyhow::anyhow!(dbg!(err_msg)).context("Failed to parse dotfile")
})?;
let mut svg = dotlay::backends::svg::SVGWriter::new();
let mut builder = dotlay::gv::GraphBuilder::default();
builder.visit_graph(&graph);
let mut vg = builder.get();
vg.do_it(false, false, false, &mut svg);
svg.finalize()
};
write_to_disk(&dest, "dot", &dot_content)?;

fn write_to_disk(
dest: impl AsRef<std::path::Path>,
ext: &str,
content: impl AsRef<[u8]>,
) -> std::io::Result<()> {
let dest = dest.as_ref().with_extension(ext);
print!("Writing {} to {} ..", ext, dest.display());
fs::write(dest, content.as_ref())?;
println!(" OK");
Ok(())
}
let svg_content = convert_dot_to_svg(&dot_content)?;

write_to_disk(&dest, "dot", &dot_content)?;
write_to_disk(&dest, "svg", &svg_content)?;

Ok(())
Expand Down Expand Up @@ -388,6 +365,35 @@ node [colorscheme={}]
}
}

#[cfg(feature = "dotgraph")]
fn convert_dot_to_svg(dot_content: impl AsRef<str>) -> anyhow::Result<String> {
let mut parser = dotlay::gv::DotParser::new(dot_content.as_ref());
let graph = parser.process().map_err(|err_msg| {
parser.print_error();
anyhow::anyhow!(err_msg).context("Failed to parse dotfile content")
})?;
let mut svg = dotlay::backends::svg::SVGWriter::new();
let mut builder = dotlay::gv::GraphBuilder::default();
builder.visit_graph(&graph);
let mut vg = builder.get();
vg.do_it(true, false, false, &mut svg);
Ok(svg.finalize())
}

#[cfg(feature = "dotgraph")]
fn write_to_disk(
dest: impl AsRef<std::path::Path>,
ext: &str,
content: impl AsRef<[u8]>,
) -> std::io::Result<()> {
use fs_err as fs;
let dest = dest.as_ref().with_extension(ext);
print!("Writing {} to {} ..", ext, dest.display());
fs::write(dest, content.as_ref())?;
println!(" OK");
Ok(())
}

const GREEK_ALPHABET_SIZE: usize = 24;

fn greek_alphabet() -> [char; GREEK_ALPHABET_SIZE] {
Expand Down Expand Up @@ -490,4 +496,14 @@ mod tests {
assert_eq!(sccs.len(), 2); // `f` and everything else
assert_eq!(sccs[0].len(), 5); // every node but `f`
}

#[cfg(feature = "dotgraph")]
#[ignore]
#[test]
fn dot_to_svg_works() {
use super::convert_dot_to_svg;

let s = include_str!("../tests/assets/sample.dot");
convert_dot_to_svg(s).unwrap();
}
}
14 changes: 14 additions & 0 deletions orchestra/proc-macro/tests/assets/sample.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
digraph {
fontname="Cantarell"
bgcolor="white"
label = "orchestra message flow between subsystems"
node [colorscheme=rdylgn10]
0 [ label="AvailabilityStore"]
1 [ label="Provisioner"]
2 [ label="RuntimeApi"]
3 [ label="ChainApi"]
4 [ label="MlExecution"]
6 [ label="NetworkBridgeTx"]
7 [ color="/rdylgn10/1",fontcolor="/rdylgn10/1",xlabel=<<B><FONT COLOR="/rdylgn10/1">α</FONT></B>>,label="GossipSupport"]
1 -> 0 [ label="AvailabilityStoreMessage"]
}

0 comments on commit 6d5d8eb

Please sign in to comment.