From a5e0624a3216a9cf155370a71c9901e56638fa0d Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Mon, 15 Dec 2014 12:37:42 +0100 Subject: [PATCH] libgraphviz: extend API with flags to indicate options like "do not include labels". --- src/libgraphviz/lib.rs | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/src/libgraphviz/lib.rs b/src/libgraphviz/lib.rs index 9e4b7a6a4cc..0576c46d3bd 100644 --- a/src/libgraphviz/lib.rs +++ b/src/libgraphviz/lib.rs @@ -513,11 +513,29 @@ pub trait GraphWalk<'a, N, E> { fn target(&'a self, edge: &E) -> N; } +#[deriving(Copy, PartialEq, Eq, Show)] +pub enum RenderOption { + NoEdgeLabels, + NoNodeLabels, +} + +/// Returns vec holding all the default render options. +pub fn default_options() -> Vec { vec![] } + /// Renders directed graph `g` into the writer `w` in DOT syntax. -/// (Main entry point for the library.) +/// (Simple wrapper around `render_opts` that passes a default set of options.) pub fn render<'a, N:Clone+'a, E:Clone+'a, G:Labeller<'a,N,E>+GraphWalk<'a,N,E>, W:Writer>( g: &'a G, - w: &mut W) -> io::IoResult<()> + w: &mut W) -> io::IoResult<()> { + render_opts(g, w, &[]) +} + +/// Renders directed graph `g` into the writer `w` in DOT syntax. +/// (Main entry point for the library.) +pub fn render_opts<'a, N:Clone+'a, E:Clone+'a, G:Labeller<'a,N,E>+GraphWalk<'a,N,E>, W:Writer>( + g: &'a G, + w: &mut W, + options: &[RenderOption]) -> io::IoResult<()> { fn writeln(w: &mut W, arg: &[&str]) -> io::IoResult<()> { for &s in arg.iter() { try!(w.write_str(s)); } @@ -532,9 +550,13 @@ fn indent(w: &mut W) -> io::IoResult<()> { for n in g.nodes().iter() { try!(indent(w)); let id = g.node_id(n); - let escaped = g.node_label(n).escape(); - try!(writeln(w, &[id.as_slice(), - "[label=\"", escaped.as_slice(), "\"];"])); + if options.contains(&RenderOption::NoNodeLabels) { + try!(writeln(w, &[id.as_slice(), ";"])); + } else { + let escaped = g.node_label(n).escape(); + try!(writeln(w, &[id.as_slice(), + "[label=\"", escaped.as_slice(), "\"];"])); + } } for e in g.edges().iter() { @@ -544,8 +566,14 @@ fn indent(w: &mut W) -> io::IoResult<()> { let target = g.target(e); let source_id = g.node_id(&source); let target_id = g.node_id(&target); - try!(writeln(w, &[source_id.as_slice(), " -> ", target_id.as_slice(), - "[label=\"", escaped_label.as_slice(), "\"];"])); + if options.contains(&RenderOption::NoEdgeLabels) { + try!(writeln(w, &[source_id.as_slice(), + " -> ", target_id.as_slice(), ";"])); + } else { + try!(writeln(w, &[source_id.as_slice(), + " -> ", target_id.as_slice(), + "[label=\"", escaped_label.as_slice(), "\"];"])); + } } writeln(w, &["}"]) -- 2.44.0