Skip to content

Commit a5e0624

Browse files
committed
libgraphviz: extend API with flags to indicate options like "do not include labels".
1 parent c500b63 commit a5e0624

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

src/libgraphviz/lib.rs

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -513,11 +513,29 @@ pub trait GraphWalk<'a, N, E> {
513513
fn target(&'a self, edge: &E) -> N;
514514
}
515515

516+
#[deriving(Copy, PartialEq, Eq, Show)]
517+
pub enum RenderOption {
518+
NoEdgeLabels,
519+
NoNodeLabels,
520+
}
521+
522+
/// Returns vec holding all the default render options.
523+
pub fn default_options() -> Vec<RenderOption> { vec![] }
524+
516525
/// Renders directed graph `g` into the writer `w` in DOT syntax.
517-
/// (Main entry point for the library.)
526+
/// (Simple wrapper around `render_opts` that passes a default set of options.)
518527
pub fn render<'a, N:Clone+'a, E:Clone+'a, G:Labeller<'a,N,E>+GraphWalk<'a,N,E>, W:Writer>(
519528
g: &'a G,
520-
w: &mut W) -> io::IoResult<()>
529+
w: &mut W) -> io::IoResult<()> {
530+
render_opts(g, w, &[])
531+
}
532+
533+
/// Renders directed graph `g` into the writer `w` in DOT syntax.
534+
/// (Main entry point for the library.)
535+
pub fn render_opts<'a, N:Clone+'a, E:Clone+'a, G:Labeller<'a,N,E>+GraphWalk<'a,N,E>, W:Writer>(
536+
g: &'a G,
537+
w: &mut W,
538+
options: &[RenderOption]) -> io::IoResult<()>
521539
{
522540
fn writeln<W:Writer>(w: &mut W, arg: &[&str]) -> io::IoResult<()> {
523541
for &s in arg.iter() { try!(w.write_str(s)); }
@@ -532,9 +550,13 @@ pub fn render<'a, N:Clone+'a, E:Clone+'a, G:Labeller<'a,N,E>+GraphWalk<'a,N,E>,
532550
for n in g.nodes().iter() {
533551
try!(indent(w));
534552
let id = g.node_id(n);
535-
let escaped = g.node_label(n).escape();
536-
try!(writeln(w, &[id.as_slice(),
537-
"[label=\"", escaped.as_slice(), "\"];"]));
553+
if options.contains(&RenderOption::NoNodeLabels) {
554+
try!(writeln(w, &[id.as_slice(), ";"]));
555+
} else {
556+
let escaped = g.node_label(n).escape();
557+
try!(writeln(w, &[id.as_slice(),
558+
"[label=\"", escaped.as_slice(), "\"];"]));
559+
}
538560
}
539561

540562
for e in g.edges().iter() {
@@ -544,8 +566,14 @@ pub fn render<'a, N:Clone+'a, E:Clone+'a, G:Labeller<'a,N,E>+GraphWalk<'a,N,E>,
544566
let target = g.target(e);
545567
let source_id = g.node_id(&source);
546568
let target_id = g.node_id(&target);
547-
try!(writeln(w, &[source_id.as_slice(), " -> ", target_id.as_slice(),
548-
"[label=\"", escaped_label.as_slice(), "\"];"]));
569+
if options.contains(&RenderOption::NoEdgeLabels) {
570+
try!(writeln(w, &[source_id.as_slice(),
571+
" -> ", target_id.as_slice(), ";"]));
572+
} else {
573+
try!(writeln(w, &[source_id.as_slice(),
574+
" -> ", target_id.as_slice(),
575+
"[label=\"", escaped_label.as_slice(), "\"];"]));
576+
}
549577
}
550578

551579
writeln(w, &["}"])

0 commit comments

Comments
 (0)