]> git.lizzy.rs Git - rust.git/commitdiff
libgraphviz: extend API with flags to indicate options like "do not include labels".
authorFelix S. Klock II <pnkfelix@pnkfx.org>
Mon, 15 Dec 2014 11:37:42 +0000 (12:37 +0100)
committerFelix S. Klock II <pnkfelix@pnkfx.org>
Mon, 15 Dec 2014 21:34:09 +0000 (22:34 +0100)
src/libgraphviz/lib.rs

index 9e4b7a6a4cc909a479de069dbcd16f3bc0d07657..0576c46d3bd8fc3543ac47db302e8ade378e97e4 100644 (file)
@@ -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<RenderOption> { 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:Writer>(w: &mut W, arg: &[&str]) -> io::IoResult<()> {
         for &s in arg.iter() { try!(w.write_str(s)); }
@@ -532,9 +550,13 @@ fn indent<W:Writer>(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:Writer>(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, &["}"])