2015-07-14 17:17:37 -05:00
|
|
|
// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
|
2014-04-25 12:10:03 -05:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-11-25 20:17:11 -06:00
|
|
|
//! Generate files suitable for use with [Graphviz](http://www.graphviz.org/)
|
|
|
|
//!
|
|
|
|
//! The `render` function generates output (e.g. an `output.dot` file) for
|
|
|
|
//! use with [Graphviz](http://www.graphviz.org/) by walking a labelled
|
|
|
|
//! graph. (Graphviz can then automatically lay out the nodes and edges
|
|
|
|
//! of the graph, and also optionally render the graph as an image or
|
|
|
|
//! other [output formats](
|
|
|
|
//! http://www.graphviz.org/content/output-formats), such as SVG.)
|
|
|
|
//!
|
|
|
|
//! Rather than impose some particular graph data structure on clients,
|
|
|
|
//! this library exposes two traits that clients can implement on their
|
|
|
|
//! own structs before handing them over to the rendering function.
|
|
|
|
//!
|
|
|
|
//! Note: This library does not yet provide access to the full
|
|
|
|
//! expressiveness of the [DOT language](
|
|
|
|
//! http://www.graphviz.org/doc/info/lang.html). For example, there are
|
|
|
|
//! many [attributes](http://www.graphviz.org/content/attrs) related to
|
|
|
|
//! providing layout hints (e.g. left-to-right versus top-down, which
|
|
|
|
//! algorithm to use, etc). The current intention of this library is to
|
|
|
|
//! emit a human-readable .dot file with very regular structure suitable
|
|
|
|
//! for easy post-processing.
|
|
|
|
//!
|
|
|
|
//! # Examples
|
|
|
|
//!
|
|
|
|
//! The first example uses a very simple graph representation: a list of
|
|
|
|
//! pairs of ints, representing the edges (the node set is implicit).
|
|
|
|
//! Each node label is derived directly from the int representing the node,
|
|
|
|
//! while the edge labels are all empty strings.
|
|
|
|
//!
|
2015-03-01 01:24:05 -06:00
|
|
|
//! This example also illustrates how to use `Cow<[T]>` to return
|
2014-11-25 20:17:11 -06:00
|
|
|
//! an owned vector or a borrowed slice as appropriate: we construct the
|
|
|
|
//! node vector from scratch, but borrow the edge list (rather than
|
|
|
|
//! constructing a copy of all the edges from scratch).
|
|
|
|
//!
|
|
|
|
//! The output from this example renders five nodes, with the first four
|
|
|
|
//! forming a diamond-shaped acyclic graph and then pointing to the fifth
|
|
|
|
//! which is cyclic.
|
|
|
|
//!
|
|
|
|
//! ```rust
|
2016-01-15 12:07:52 -06:00
|
|
|
//! #![feature(rustc_private)]
|
2015-07-27 09:50:19 -05:00
|
|
|
//!
|
2016-01-15 12:07:52 -06:00
|
|
|
//! use graphviz::IntoCow;
|
2015-03-11 17:24:14 -05:00
|
|
|
//! use std::io::Write;
|
2014-11-25 20:17:11 -06:00
|
|
|
//! use graphviz as dot;
|
|
|
|
//!
|
2015-03-23 17:54:39 -05:00
|
|
|
//! type Nd = isize;
|
|
|
|
//! type Ed = (isize,isize);
|
2014-11-25 20:17:11 -06:00
|
|
|
//! struct Edges(Vec<Ed>);
|
|
|
|
//!
|
2015-03-11 17:24:14 -05:00
|
|
|
//! pub fn render_to<W: Write>(output: &mut W) {
|
2016-10-29 16:54:04 -05:00
|
|
|
//! let edges = Edges(vec![(0,1), (0,2), (1,3), (2,3), (3,4), (4,4)]);
|
2014-11-25 20:17:11 -06:00
|
|
|
//! dot::render(&edges, output).unwrap()
|
|
|
|
//! }
|
|
|
|
//!
|
2016-01-26 15:23:05 -06:00
|
|
|
//! impl<'a> dot::Labeller<'a> for Edges {
|
|
|
|
//! type Node = Nd;
|
|
|
|
//! type Edge = Ed;
|
2014-11-25 20:17:11 -06:00
|
|
|
//! fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new("example1").unwrap() }
|
|
|
|
//!
|
|
|
|
//! fn node_id(&'a self, n: &Nd) -> dot::Id<'a> {
|
|
|
|
//! dot::Id::new(format!("N{}", *n)).unwrap()
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//!
|
2016-01-26 15:23:05 -06:00
|
|
|
//! impl<'a> dot::GraphWalk<'a> for Edges {
|
|
|
|
//! type Node = Nd;
|
|
|
|
//! type Edge = Ed;
|
2014-11-25 20:17:11 -06:00
|
|
|
//! fn nodes(&self) -> dot::Nodes<'a,Nd> {
|
|
|
|
//! // (assumes that |N| \approxeq |E|)
|
|
|
|
//! let &Edges(ref v) = self;
|
|
|
|
//! let mut nodes = Vec::with_capacity(v.len());
|
2015-06-10 11:22:20 -05:00
|
|
|
//! for &(s,t) in v {
|
2014-11-25 20:17:11 -06:00
|
|
|
//! nodes.push(s); nodes.push(t);
|
|
|
|
//! }
|
|
|
|
//! nodes.sort();
|
|
|
|
//! nodes.dedup();
|
|
|
|
//! nodes.into_cow()
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! fn edges(&'a self) -> dot::Edges<'a,Ed> {
|
|
|
|
//! let &Edges(ref edges) = self;
|
2015-03-30 11:22:46 -05:00
|
|
|
//! (&edges[..]).into_cow()
|
2014-11-25 20:17:11 -06:00
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! fn source(&self, e: &Ed) -> Nd { let &(s,_) = e; s }
|
|
|
|
//!
|
|
|
|
//! fn target(&self, e: &Ed) -> Nd { let &(_,t) = e; t }
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! # pub fn main() { render_to(&mut Vec::new()) }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! ```no_run
|
2015-03-11 17:24:14 -05:00
|
|
|
//! # pub fn render_to<W:std::io::Write>(output: &mut W) { unimplemented!() }
|
2014-11-25 20:17:11 -06:00
|
|
|
//! pub fn main() {
|
2015-03-11 17:24:14 -05:00
|
|
|
//! use std::fs::File;
|
|
|
|
//! let mut f = File::create("example1.dot").unwrap();
|
2014-11-25 20:17:11 -06:00
|
|
|
//! render_to(&mut f)
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! Output from first example (in `example1.dot`):
|
|
|
|
//!
|
|
|
|
//! ```ignore
|
|
|
|
//! digraph example1 {
|
|
|
|
//! N0[label="N0"];
|
|
|
|
//! N1[label="N1"];
|
|
|
|
//! N2[label="N2"];
|
|
|
|
//! N3[label="N3"];
|
|
|
|
//! N4[label="N4"];
|
|
|
|
//! N0 -> N1[label=""];
|
|
|
|
//! N0 -> N2[label=""];
|
|
|
|
//! N1 -> N3[label=""];
|
|
|
|
//! N2 -> N3[label=""];
|
|
|
|
//! N3 -> N4[label=""];
|
|
|
|
//! N4 -> N4[label=""];
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! The second example illustrates using `node_label` and `edge_label` to
|
|
|
|
//! add labels to the nodes and edges in the rendered graph. The graph
|
|
|
|
//! here carries both `nodes` (the label text to use for rendering a
|
|
|
|
//! particular node), and `edges` (again a list of `(source,target)`
|
|
|
|
//! indices).
|
|
|
|
//!
|
|
|
|
//! This example also illustrates how to use a type (in this case the edge
|
|
|
|
//! type) that shares substructure with the graph: the edge type here is a
|
|
|
|
//! direct reference to the `(source,target)` pair stored in the graph's
|
|
|
|
//! internal vector (rather than passing around a copy of the pair
|
|
|
|
//! itself). Note that this implies that `fn edges(&'a self)` must
|
2015-03-23 17:54:39 -05:00
|
|
|
//! construct a fresh `Vec<&'a (usize,usize)>` from the `Vec<(usize,usize)>`
|
2014-11-25 20:17:11 -06:00
|
|
|
//! edges stored in `self`.
|
|
|
|
//!
|
|
|
|
//! Since both the set of nodes and the set of edges are always
|
|
|
|
//! constructed from scratch via iterators, we use the `collect()` method
|
|
|
|
//! from the `Iterator` trait to collect the nodes and edges into freshly
|
|
|
|
//! constructed growable `Vec` values (rather use the `into_cow`
|
|
|
|
//! from the `IntoCow` trait as was used in the first example
|
|
|
|
//! above).
|
|
|
|
//!
|
|
|
|
//! The output from this example renders four nodes that make up the
|
|
|
|
//! Hasse-diagram for the subsets of the set `{x, y}`. Each edge is
|
|
|
|
//! labelled with the ⊆ character (specified using the HTML character
|
|
|
|
//! entity `&sube`).
|
|
|
|
//!
|
|
|
|
//! ```rust
|
2015-11-03 09:33:58 -06:00
|
|
|
//! #![feature(rustc_private)]
|
2015-07-27 09:50:19 -05:00
|
|
|
//!
|
2015-03-11 17:24:14 -05:00
|
|
|
//! use std::io::Write;
|
2014-11-25 20:17:11 -06:00
|
|
|
//! use graphviz as dot;
|
|
|
|
//!
|
2015-03-23 17:54:39 -05:00
|
|
|
//! type Nd = usize;
|
|
|
|
//! type Ed<'a> = &'a (usize, usize);
|
|
|
|
//! struct Graph { nodes: Vec<&'static str>, edges: Vec<(usize,usize)> }
|
2014-11-25 20:17:11 -06:00
|
|
|
//!
|
2015-03-11 17:24:14 -05:00
|
|
|
//! pub fn render_to<W: Write>(output: &mut W) {
|
2016-10-29 16:54:04 -05:00
|
|
|
//! let nodes = vec!["{x,y}","{x}","{y}","{}"];
|
|
|
|
//! let edges = vec![(0,1), (0,2), (1,3), (2,3)];
|
2014-11-25 20:17:11 -06:00
|
|
|
//! let graph = Graph { nodes: nodes, edges: edges };
|
|
|
|
//!
|
|
|
|
//! dot::render(&graph, output).unwrap()
|
|
|
|
//! }
|
|
|
|
//!
|
2016-01-26 15:23:05 -06:00
|
|
|
//! impl<'a> dot::Labeller<'a> for Graph {
|
|
|
|
//! type Node = Nd;
|
|
|
|
//! type Edge = Ed<'a>;
|
2014-11-25 20:17:11 -06:00
|
|
|
//! fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new("example2").unwrap() }
|
|
|
|
//! fn node_id(&'a self, n: &Nd) -> dot::Id<'a> {
|
|
|
|
//! dot::Id::new(format!("N{}", n)).unwrap()
|
|
|
|
//! }
|
2014-12-12 10:09:32 -06:00
|
|
|
//! fn node_label<'b>(&'b self, n: &Nd) -> dot::LabelText<'b> {
|
2015-11-03 09:33:58 -06:00
|
|
|
//! dot::LabelText::LabelStr(self.nodes[*n].into())
|
2014-11-25 20:17:11 -06:00
|
|
|
//! }
|
2014-12-12 10:09:32 -06:00
|
|
|
//! fn edge_label<'b>(&'b self, _: &Ed) -> dot::LabelText<'b> {
|
2015-11-03 09:33:58 -06:00
|
|
|
//! dot::LabelText::LabelStr("⊆".into())
|
2014-11-25 20:17:11 -06:00
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//!
|
2016-01-26 15:23:05 -06:00
|
|
|
//! impl<'a> dot::GraphWalk<'a> for Graph {
|
|
|
|
//! type Node = Nd;
|
|
|
|
//! type Edge = Ed<'a>;
|
2015-01-26 15:05:07 -06:00
|
|
|
//! fn nodes(&self) -> dot::Nodes<'a,Nd> { (0..self.nodes.len()).collect() }
|
2014-11-25 20:17:11 -06:00
|
|
|
//! fn edges(&'a self) -> dot::Edges<'a,Ed<'a>> { self.edges.iter().collect() }
|
|
|
|
//! fn source(&self, e: &Ed) -> Nd { let & &(s,_) = e; s }
|
|
|
|
//! fn target(&self, e: &Ed) -> Nd { let & &(_,t) = e; t }
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! # pub fn main() { render_to(&mut Vec::new()) }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! ```no_run
|
2015-03-11 17:24:14 -05:00
|
|
|
//! # pub fn render_to<W:std::io::Write>(output: &mut W) { unimplemented!() }
|
2014-11-25 20:17:11 -06:00
|
|
|
//! pub fn main() {
|
2015-03-11 17:24:14 -05:00
|
|
|
//! use std::fs::File;
|
|
|
|
//! let mut f = File::create("example2.dot").unwrap();
|
2014-11-25 20:17:11 -06:00
|
|
|
//! render_to(&mut f)
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! The third example is similar to the second, except now each node and
|
|
|
|
//! edge now carries a reference to the string label for each node as well
|
|
|
|
//! as that node's index. (This is another illustration of how to share
|
|
|
|
//! structure with the graph itself, and why one might want to do so.)
|
|
|
|
//!
|
|
|
|
//! The output from this example is the same as the second example: the
|
|
|
|
//! Hasse-diagram for the subsets of the set `{x, y}`.
|
|
|
|
//!
|
|
|
|
//! ```rust
|
2015-11-03 09:33:58 -06:00
|
|
|
//! #![feature(rustc_private)]
|
2015-07-27 09:50:19 -05:00
|
|
|
//!
|
2015-03-11 17:24:14 -05:00
|
|
|
//! use std::io::Write;
|
2014-11-25 20:17:11 -06:00
|
|
|
//! use graphviz as dot;
|
|
|
|
//!
|
2015-03-23 17:54:39 -05:00
|
|
|
//! type Nd<'a> = (usize, &'a str);
|
2014-11-25 20:17:11 -06:00
|
|
|
//! type Ed<'a> = (Nd<'a>, Nd<'a>);
|
2015-03-23 17:54:39 -05:00
|
|
|
//! struct Graph { nodes: Vec<&'static str>, edges: Vec<(usize,usize)> }
|
2014-11-25 20:17:11 -06:00
|
|
|
//!
|
2015-03-11 17:24:14 -05:00
|
|
|
//! pub fn render_to<W: Write>(output: &mut W) {
|
2016-10-29 16:54:04 -05:00
|
|
|
//! let nodes = vec!["{x,y}","{x}","{y}","{}"];
|
|
|
|
//! let edges = vec![(0,1), (0,2), (1,3), (2,3)];
|
2014-11-25 20:17:11 -06:00
|
|
|
//! let graph = Graph { nodes: nodes, edges: edges };
|
|
|
|
//!
|
|
|
|
//! dot::render(&graph, output).unwrap()
|
|
|
|
//! }
|
|
|
|
//!
|
2016-01-26 15:23:05 -06:00
|
|
|
//! impl<'a> dot::Labeller<'a> for Graph {
|
|
|
|
//! type Node = Nd<'a>;
|
|
|
|
//! type Edge = Ed<'a>;
|
2014-11-25 20:17:11 -06:00
|
|
|
//! fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new("example3").unwrap() }
|
|
|
|
//! fn node_id(&'a self, n: &Nd<'a>) -> dot::Id<'a> {
|
2014-12-09 14:15:10 -06:00
|
|
|
//! dot::Id::new(format!("N{}", n.0)).unwrap()
|
2014-11-25 20:17:11 -06:00
|
|
|
//! }
|
2014-12-12 10:09:32 -06:00
|
|
|
//! fn node_label<'b>(&'b self, n: &Nd<'b>) -> dot::LabelText<'b> {
|
2014-11-25 20:17:11 -06:00
|
|
|
//! let &(i, _) = n;
|
2015-11-03 09:33:58 -06:00
|
|
|
//! dot::LabelText::LabelStr(self.nodes[i].into())
|
2014-11-25 20:17:11 -06:00
|
|
|
//! }
|
2014-12-12 10:09:32 -06:00
|
|
|
//! fn edge_label<'b>(&'b self, _: &Ed<'b>) -> dot::LabelText<'b> {
|
2015-11-03 09:33:58 -06:00
|
|
|
//! dot::LabelText::LabelStr("⊆".into())
|
2014-11-25 20:17:11 -06:00
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//!
|
2016-01-26 15:23:05 -06:00
|
|
|
//! impl<'a> dot::GraphWalk<'a> for Graph {
|
|
|
|
//! type Node = Nd<'a>;
|
|
|
|
//! type Edge = Ed<'a>;
|
2014-11-25 20:17:11 -06:00
|
|
|
//! fn nodes(&'a self) -> dot::Nodes<'a,Nd<'a>> {
|
2015-03-23 17:54:39 -05:00
|
|
|
//! self.nodes.iter().map(|s| &s[..]).enumerate().collect()
|
2014-11-25 20:17:11 -06:00
|
|
|
//! }
|
|
|
|
//! fn edges(&'a self) -> dot::Edges<'a,Ed<'a>> {
|
|
|
|
//! self.edges.iter()
|
2015-03-23 17:54:39 -05:00
|
|
|
//! .map(|&(i,j)|((i, &self.nodes[i][..]),
|
|
|
|
//! (j, &self.nodes[j][..])))
|
2014-11-25 20:17:11 -06:00
|
|
|
//! .collect()
|
|
|
|
//! }
|
|
|
|
//! fn source(&self, e: &Ed<'a>) -> Nd<'a> { let &(s,_) = e; s }
|
|
|
|
//! fn target(&self, e: &Ed<'a>) -> Nd<'a> { let &(_,t) = e; t }
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! # pub fn main() { render_to(&mut Vec::new()) }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! ```no_run
|
2015-03-11 17:24:14 -05:00
|
|
|
//! # pub fn render_to<W:std::io::Write>(output: &mut W) { unimplemented!() }
|
2014-11-25 20:17:11 -06:00
|
|
|
//! pub fn main() {
|
2015-03-11 17:24:14 -05:00
|
|
|
//! use std::fs::File;
|
|
|
|
//! let mut f = File::create("example3.dot").unwrap();
|
2014-11-25 20:17:11 -06:00
|
|
|
//! render_to(&mut f)
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! # References
|
|
|
|
//!
|
|
|
|
//! * [Graphviz](http://www.graphviz.org/)
|
|
|
|
//!
|
|
|
|
//! * [DOT language](http://www.graphviz.org/doc/info/lang.html)
|
2014-04-25 12:10:03 -05:00
|
|
|
|
2014-07-01 09:12:04 -05:00
|
|
|
#![crate_name = "graphviz"]
|
2015-08-13 12:21:36 -05:00
|
|
|
#![unstable(feature = "rustc_private", issue = "27812")]
|
2015-01-21 20:21:14 -06:00
|
|
|
#![feature(staged_api)]
|
2014-04-25 12:10:03 -05:00
|
|
|
#![crate_type = "rlib"]
|
|
|
|
#![crate_type = "dylib"]
|
2015-08-09 16:15:05 -05:00
|
|
|
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
|
2015-05-15 18:04:01 -05:00
|
|
|
html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
|
2015-11-03 09:33:58 -06:00
|
|
|
html_root_url = "https://doc.rust-lang.org/nightly/",
|
|
|
|
test(attr(allow(unused_variables), deny(warnings))))]
|
2016-01-21 17:26:19 -06:00
|
|
|
#![cfg_attr(not(stage0), deny(warnings))]
|
2015-06-09 16:39:23 -05:00
|
|
|
|
|
|
|
#![feature(str_escape)]
|
2014-11-06 02:05:53 -06:00
|
|
|
|
2015-01-02 21:31:50 -06:00
|
|
|
use self::LabelText::*;
|
2014-04-25 12:10:03 -05:00
|
|
|
|
2016-01-15 12:07:52 -06:00
|
|
|
use std::borrow::{Cow, ToOwned};
|
2015-03-11 17:24:14 -05:00
|
|
|
use std::io::prelude::*;
|
|
|
|
use std::io;
|
2014-04-25 12:10:03 -05:00
|
|
|
|
|
|
|
/// The text for a graphviz label on a node or edge.
|
|
|
|
pub enum LabelText<'a> {
|
|
|
|
/// This kind of label preserves the text directly as is.
|
|
|
|
///
|
|
|
|
/// Occurrences of backslashes (`\`) are escaped, and thus appear
|
|
|
|
/// as backslashes in the rendered label.
|
2015-02-18 17:58:07 -06:00
|
|
|
LabelStr(Cow<'a, str>),
|
2014-04-25 12:10:03 -05:00
|
|
|
|
|
|
|
/// This kind of label uses the graphviz label escString type:
|
|
|
|
/// http://www.graphviz.org/content/attrs#kescString
|
|
|
|
///
|
|
|
|
/// Occurrences of backslashes (`\`) are not escaped; instead they
|
|
|
|
/// are interpreted as initiating an escString escape sequence.
|
|
|
|
///
|
|
|
|
/// Escape sequences of particular interest: in addition to `\n`
|
|
|
|
/// to break a line (centering the line preceding the `\n`), there
|
|
|
|
/// are also the escape sequences `\l` which left-justifies the
|
|
|
|
/// preceding line and `\r` which right-justifies it.
|
2015-02-18 17:58:07 -06:00
|
|
|
EscStr(Cow<'a, str>),
|
2015-08-18 16:50:56 -05:00
|
|
|
|
|
|
|
/// This uses a graphviz [HTML string label][html]. The string is
|
|
|
|
/// printed exactly as given, but between `<` and `>`. **No
|
|
|
|
/// escaping is performed.**
|
|
|
|
///
|
|
|
|
/// [html]: http://www.graphviz.org/content/node-shapes#html
|
|
|
|
HtmlStr(Cow<'a, str>),
|
2014-04-25 12:10:03 -05:00
|
|
|
}
|
|
|
|
|
2015-07-14 17:17:37 -05:00
|
|
|
/// The style for a node or edge.
|
|
|
|
/// See http://www.graphviz.org/doc/info/attrs.html#k:style for descriptions.
|
|
|
|
/// Note that some of these are not valid for edges.
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
|
|
|
pub enum Style {
|
|
|
|
None,
|
|
|
|
Solid,
|
|
|
|
Dashed,
|
|
|
|
Dotted,
|
|
|
|
Bold,
|
|
|
|
Rounded,
|
|
|
|
Diagonals,
|
|
|
|
Filled,
|
|
|
|
Striped,
|
|
|
|
Wedged,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Style {
|
|
|
|
pub fn as_slice(self) -> &'static str {
|
|
|
|
match self {
|
|
|
|
Style::None => "",
|
|
|
|
Style::Solid => "solid",
|
|
|
|
Style::Dashed => "dashed",
|
|
|
|
Style::Dotted => "dotted",
|
|
|
|
Style::Bold => "bold",
|
|
|
|
Style::Rounded => "rounded",
|
|
|
|
Style::Diagonals => "diagonals",
|
|
|
|
Style::Filled => "filled",
|
|
|
|
Style::Striped => "striped",
|
|
|
|
Style::Wedged => "wedged",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-25 12:10:03 -05:00
|
|
|
// There is a tension in the design of the labelling API.
|
|
|
|
//
|
|
|
|
// For example, I considered making a `Labeller<T>` trait that
|
|
|
|
// provides labels for `T`, and then making the graph type `G`
|
|
|
|
// implement `Labeller<Node>` and `Labeller<Edge>`. However, this is
|
|
|
|
// not possible without functional dependencies. (One could work
|
|
|
|
// around that, but I did not explore that avenue heavily.)
|
|
|
|
//
|
|
|
|
// Another approach that I actually used for a while was to make a
|
|
|
|
// `Label<Context>` trait that is implemented by the client-specific
|
|
|
|
// Node and Edge types (as well as an implementation on Graph itself
|
|
|
|
// for the overall name for the graph). The main disadvantage of this
|
|
|
|
// second approach (compared to having the `G` type parameter
|
|
|
|
// implement a Labelling service) that I have encountered is that it
|
|
|
|
// makes it impossible to use types outside of the current crate
|
|
|
|
// directly as Nodes/Edges; you need to wrap them in newtype'd
|
|
|
|
// structs. See e.g. the `No` and `Ed` structs in the examples. (In
|
|
|
|
// practice clients using a graph in some other crate would need to
|
|
|
|
// provide some sort of adapter shim over the graph anyway to
|
|
|
|
// interface with this library).
|
|
|
|
//
|
|
|
|
// Another approach would be to make a single `Labeller<N,E>` trait
|
|
|
|
// that provides three methods (graph_label, node_label, edge_label),
|
|
|
|
// and then make `G` implement `Labeller<N,E>`. At first this did not
|
|
|
|
// appeal to me, since I had thought I would need separate methods on
|
|
|
|
// each data variant for dot-internal identifiers versus user-visible
|
|
|
|
// labels. However, the identifier/label distinction only arises for
|
|
|
|
// nodes; graphs themselves only have identifiers, and edges only have
|
|
|
|
// labels.
|
|
|
|
//
|
|
|
|
// So in the end I decided to use the third approach described above.
|
|
|
|
|
|
|
|
/// `Id` is a Graphviz `ID`.
|
|
|
|
pub struct Id<'a> {
|
2015-02-18 17:58:07 -06:00
|
|
|
name: Cow<'a, str>,
|
2014-04-25 12:10:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Id<'a> {
|
|
|
|
/// Creates an `Id` named `name`.
|
|
|
|
///
|
|
|
|
/// The caller must ensure that the input conforms to an
|
|
|
|
/// identifier format: it must be a non-empty string made up of
|
|
|
|
/// alphanumeric or underscore characters, not beginning with a
|
|
|
|
/// digit (i.e. the regular expression `[a-zA-Z_][a-zA-Z_0-9]*`).
|
|
|
|
///
|
|
|
|
/// (Note: this format is a strict subset of the `ID` format
|
|
|
|
/// defined by the DOT language. This function may change in the
|
|
|
|
/// future to accept a broader subset, or the entirety, of DOT's
|
|
|
|
/// `ID` format.)
|
2014-11-12 09:21:03 -06:00
|
|
|
///
|
|
|
|
/// Passing an invalid string (containing spaces, brackets,
|
|
|
|
/// quotes, ...) will return an empty `Err` value.
|
2015-02-12 01:16:32 -06:00
|
|
|
pub fn new<Name: IntoCow<'a, str>>(name: Name) -> Result<Id<'a>, ()> {
|
2014-11-21 16:10:42 -06:00
|
|
|
let name = name.into_cow();
|
2014-04-25 12:10:03 -05:00
|
|
|
{
|
2014-11-21 16:10:42 -06:00
|
|
|
let mut chars = name.chars();
|
2014-11-12 09:21:03 -06:00
|
|
|
match chars.next() {
|
2015-09-04 22:46:45 -05:00
|
|
|
Some(c) if is_letter_or_underscore(c) => {}
|
2015-09-04 22:44:26 -05:00
|
|
|
_ => return Err(()),
|
2014-11-12 09:21:03 -06:00
|
|
|
}
|
|
|
|
if !chars.all(is_constituent) {
|
2015-11-23 17:11:20 -06:00
|
|
|
return Err(());
|
2014-11-12 09:21:03 -06:00
|
|
|
}
|
2014-04-25 12:10:03 -05:00
|
|
|
}
|
2015-11-23 17:11:20 -06:00
|
|
|
return Ok(Id { name: name });
|
2014-04-25 12:10:03 -05:00
|
|
|
|
|
|
|
fn is_letter_or_underscore(c: char) -> bool {
|
|
|
|
in_range('a', c, 'z') || in_range('A', c, 'Z') || c == '_'
|
|
|
|
}
|
|
|
|
fn is_constituent(c: char) -> bool {
|
|
|
|
is_letter_or_underscore(c) || in_range('0', c, '9')
|
|
|
|
}
|
|
|
|
fn in_range(low: char, c: char, high: char) -> bool {
|
2015-03-23 17:54:39 -05:00
|
|
|
low as usize <= c as usize && c as usize <= high as usize
|
2014-04-25 12:10:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_slice(&'a self) -> &'a str {
|
2014-11-21 16:10:42 -06:00
|
|
|
&*self.name
|
2014-04-25 12:10:03 -05:00
|
|
|
}
|
|
|
|
|
2015-02-18 17:58:07 -06:00
|
|
|
pub fn name(self) -> Cow<'a, str> {
|
2014-04-25 12:10:03 -05:00
|
|
|
self.name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Each instance of a type that implements `Label<C>` maps to a
|
|
|
|
/// unique identifier with respect to `C`, which is used to identify
|
|
|
|
/// it in the generated .dot file. They can also provide more
|
|
|
|
/// elaborate (and non-unique) label text that is used in the graphviz
|
|
|
|
/// rendered output.
|
|
|
|
|
|
|
|
/// The graph instance is responsible for providing the DOT compatible
|
|
|
|
/// identifiers for the nodes and (optionally) rendered labels for the nodes and
|
|
|
|
/// edges, as well as an identifier for the graph itself.
|
2016-01-26 15:23:05 -06:00
|
|
|
pub trait Labeller<'a> {
|
|
|
|
type Node;
|
|
|
|
type Edge;
|
|
|
|
|
2014-04-25 12:10:03 -05:00
|
|
|
/// Must return a DOT compatible identifier naming the graph.
|
|
|
|
fn graph_id(&'a self) -> Id<'a>;
|
|
|
|
|
|
|
|
/// Maps `n` to a unique identifier with respect to `self`. The
|
2015-10-13 08:44:11 -05:00
|
|
|
/// implementor is responsible for ensuring that the returned name
|
2014-04-25 12:10:03 -05:00
|
|
|
/// is a valid DOT identifier.
|
2016-01-26 15:23:05 -06:00
|
|
|
fn node_id(&'a self, n: &Self::Node) -> Id<'a>;
|
2014-04-25 12:10:03 -05:00
|
|
|
|
2015-08-18 16:50:56 -05:00
|
|
|
/// Maps `n` to one of the [graphviz `shape` names][1]. If `None`
|
|
|
|
/// is returned, no `shape` attribute is specified.
|
|
|
|
///
|
|
|
|
/// [1]: http://www.graphviz.org/content/node-shapes
|
2016-01-26 15:23:05 -06:00
|
|
|
fn node_shape(&'a self, _node: &Self::Node) -> Option<LabelText<'a>> {
|
2015-08-18 16:50:56 -05:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2014-04-25 12:10:03 -05:00
|
|
|
/// Maps `n` to a label that will be used in the rendered output.
|
|
|
|
/// The label need not be unique, and may be the empty string; the
|
|
|
|
/// default is just the output from `node_id`.
|
2016-01-26 15:23:05 -06:00
|
|
|
fn node_label(&'a self, n: &Self::Node) -> LabelText<'a> {
|
2014-04-25 12:10:03 -05:00
|
|
|
LabelStr(self.node_id(n).name)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Maps `e` to a label that will be used in the rendered output.
|
|
|
|
/// The label need not be unique, and may be the empty string; the
|
|
|
|
/// default is in fact the empty string.
|
2016-01-26 15:23:05 -06:00
|
|
|
fn edge_label(&'a self, e: &Self::Edge) -> LabelText<'a> {
|
2014-04-25 12:10:03 -05:00
|
|
|
let _ignored = e;
|
2014-11-21 16:10:42 -06:00
|
|
|
LabelStr("".into_cow())
|
2014-04-25 12:10:03 -05:00
|
|
|
}
|
2015-07-14 17:17:37 -05:00
|
|
|
|
|
|
|
/// Maps `n` to a style that will be used in the rendered output.
|
2016-01-26 15:23:05 -06:00
|
|
|
fn node_style(&'a self, _n: &Self::Node) -> Style {
|
2015-07-14 17:17:37 -05:00
|
|
|
Style::None
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Maps `e` to a style that will be used in the rendered output.
|
2016-01-26 15:23:05 -06:00
|
|
|
fn edge_style(&'a self, _e: &Self::Edge) -> Style {
|
2015-07-14 17:17:37 -05:00
|
|
|
Style::None
|
|
|
|
}
|
2014-04-25 12:10:03 -05:00
|
|
|
}
|
|
|
|
|
2015-08-18 16:50:56 -05:00
|
|
|
/// Escape tags in such a way that it is suitable for inclusion in a
|
|
|
|
/// Graphviz HTML label.
|
|
|
|
pub fn escape_html(s: &str) -> String {
|
2015-11-23 17:11:20 -06:00
|
|
|
s.replace("&", "&")
|
|
|
|
.replace("\"", """)
|
|
|
|
.replace("<", "<")
|
|
|
|
.replace(">", ">")
|
2015-08-18 16:50:56 -05:00
|
|
|
}
|
|
|
|
|
2014-04-25 12:10:03 -05:00
|
|
|
impl<'a> LabelText<'a> {
|
2015-09-04 22:44:26 -05:00
|
|
|
pub fn label<S: IntoCow<'a, str>>(s: S) -> LabelText<'a> {
|
2014-12-15 05:32:54 -06:00
|
|
|
LabelStr(s.into_cow())
|
|
|
|
}
|
|
|
|
|
2015-09-04 22:44:26 -05:00
|
|
|
pub fn escaped<S: IntoCow<'a, str>>(s: S) -> LabelText<'a> {
|
2014-12-15 05:32:54 -06:00
|
|
|
EscStr(s.into_cow())
|
|
|
|
}
|
|
|
|
|
2015-09-04 22:44:26 -05:00
|
|
|
pub fn html<S: IntoCow<'a, str>>(s: S) -> LabelText<'a> {
|
2015-08-18 16:50:56 -05:00
|
|
|
HtmlStr(s.into_cow())
|
|
|
|
}
|
|
|
|
|
2015-09-04 22:44:26 -05:00
|
|
|
fn escape_char<F>(c: char, mut f: F)
|
|
|
|
where F: FnMut(char)
|
|
|
|
{
|
2014-04-25 12:10:03 -05:00
|
|
|
match c {
|
|
|
|
// not escaping \\, since Graphviz escString needs to
|
|
|
|
// interpret backslashes; see EscStr above.
|
|
|
|
'\\' => f(c),
|
2015-11-23 17:11:20 -06:00
|
|
|
_ => {
|
|
|
|
for c in c.escape_default() {
|
|
|
|
f(c)
|
|
|
|
}
|
|
|
|
}
|
2014-04-25 12:10:03 -05:00
|
|
|
}
|
|
|
|
}
|
2014-05-22 18:57:53 -05:00
|
|
|
fn escape_str(s: &str) -> String {
|
|
|
|
let mut out = String::with_capacity(s.len());
|
2014-04-25 12:10:03 -05:00
|
|
|
for c in s.chars() {
|
2014-09-22 10:28:35 -05:00
|
|
|
LabelText::escape_char(c, |c| out.push(c));
|
2014-04-25 12:10:03 -05:00
|
|
|
}
|
|
|
|
out
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Renders text as string suitable for a label in a .dot file.
|
2015-08-18 16:50:56 -05:00
|
|
|
/// This includes quotes or suitable delimeters.
|
|
|
|
pub fn to_dot_string(&self) -> String {
|
2014-04-25 12:10:03 -05:00
|
|
|
match self {
|
2015-08-18 16:50:56 -05:00
|
|
|
&LabelStr(ref s) => format!("\"{}\"", s.escape_default()),
|
|
|
|
&EscStr(ref s) => format!("\"{}\"", LabelText::escape_str(&s[..])),
|
|
|
|
&HtmlStr(ref s) => format!("<{}>", s),
|
2014-04-25 12:10:03 -05:00
|
|
|
}
|
|
|
|
}
|
2014-07-02 10:50:18 -05:00
|
|
|
|
|
|
|
/// Decomposes content into string suitable for making EscStr that
|
|
|
|
/// yields same content as self. The result obeys the law
|
|
|
|
/// render(`lt`) == render(`EscStr(lt.pre_escaped_content())`) for
|
|
|
|
/// all `lt: LabelText`.
|
2015-02-18 17:58:07 -06:00
|
|
|
fn pre_escaped_content(self) -> Cow<'a, str> {
|
2014-07-02 10:50:18 -05:00
|
|
|
match self {
|
|
|
|
EscStr(s) => s,
|
2015-11-23 17:11:20 -06:00
|
|
|
LabelStr(s) => {
|
|
|
|
if s.contains('\\') {
|
|
|
|
(&*s).escape_default().into_cow()
|
|
|
|
} else {
|
|
|
|
s
|
|
|
|
}
|
|
|
|
}
|
2015-08-18 16:50:56 -05:00
|
|
|
HtmlStr(s) => s,
|
2014-07-02 10:50:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Puts `prefix` on a line above this label, with a blank line separator.
|
2014-07-17 23:44:59 -05:00
|
|
|
pub fn prefix_line(self, prefix: LabelText) -> LabelText<'static> {
|
2014-07-02 10:50:18 -05:00
|
|
|
prefix.suffix_line(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Puts `suffix` on a line below this label, with a blank line separator.
|
2014-07-17 23:44:59 -05:00
|
|
|
pub fn suffix_line(self, suffix: LabelText) -> LabelText<'static> {
|
2014-12-10 21:46:38 -06:00
|
|
|
let mut prefix = self.pre_escaped_content().into_owned();
|
2014-07-02 10:50:18 -05:00
|
|
|
let suffix = suffix.pre_escaped_content();
|
2014-09-22 10:28:35 -05:00
|
|
|
prefix.push_str(r"\n\n");
|
2015-02-18 13:48:57 -06:00
|
|
|
prefix.push_str(&suffix[..]);
|
2014-11-21 16:10:42 -06:00
|
|
|
EscStr(prefix.into_cow())
|
2014-07-02 10:50:18 -05:00
|
|
|
}
|
2014-04-25 12:10:03 -05:00
|
|
|
}
|
|
|
|
|
2015-02-18 17:58:07 -06:00
|
|
|
pub type Nodes<'a,N> = Cow<'a,[N]>;
|
|
|
|
pub type Edges<'a,E> = Cow<'a,[E]>;
|
2014-04-25 12:10:03 -05:00
|
|
|
|
|
|
|
// (The type parameters in GraphWalk should be associated items,
|
|
|
|
// when/if Rust supports such.)
|
|
|
|
|
|
|
|
/// GraphWalk is an abstraction over a directed graph = (nodes,edges)
|
|
|
|
/// made up of node handles `N` and edge handles `E`, where each `E`
|
|
|
|
/// can be mapped to its source and target nodes.
|
|
|
|
///
|
|
|
|
/// The lifetime parameter `'a` is exposed in this trait (rather than
|
|
|
|
/// introduced as a generic parameter on each method declaration) so
|
|
|
|
/// that a client impl can choose `N` and `E` that have substructure
|
|
|
|
/// that is bound by the self lifetime `'a`.
|
|
|
|
///
|
|
|
|
/// The `nodes` and `edges` method each return instantiations of
|
2015-10-13 08:44:11 -05:00
|
|
|
/// `Cow<[T]>` to leave implementors the freedom to create
|
2014-04-25 12:10:03 -05:00
|
|
|
/// entirely new vectors or to pass back slices into internally owned
|
|
|
|
/// vectors.
|
2016-01-26 15:23:05 -06:00
|
|
|
pub trait GraphWalk<'a> {
|
|
|
|
type Node: Clone;
|
|
|
|
type Edge: Clone;
|
|
|
|
|
2014-04-25 12:10:03 -05:00
|
|
|
/// Returns all the nodes in this graph.
|
2016-01-26 15:23:05 -06:00
|
|
|
fn nodes(&'a self) -> Nodes<'a, Self::Node>;
|
2014-04-25 12:10:03 -05:00
|
|
|
/// Returns all of the edges in this graph.
|
2016-01-26 15:23:05 -06:00
|
|
|
fn edges(&'a self) -> Edges<'a, Self::Edge>;
|
2014-04-25 12:10:03 -05:00
|
|
|
/// The source node for `edge`.
|
2016-01-26 15:23:05 -06:00
|
|
|
fn source(&'a self, edge: &Self::Edge) -> Self::Node;
|
2014-04-25 12:10:03 -05:00
|
|
|
/// The target node for `edge`.
|
2016-01-26 15:23:05 -06:00
|
|
|
fn target(&'a self, edge: &Self::Edge) -> Self::Node;
|
2014-04-25 12:10:03 -05:00
|
|
|
}
|
|
|
|
|
2015-03-30 08:40:52 -05:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
2014-12-15 05:37:42 -06:00
|
|
|
pub enum RenderOption {
|
|
|
|
NoEdgeLabels,
|
|
|
|
NoNodeLabels,
|
2015-07-14 17:17:37 -05:00
|
|
|
NoEdgeStyles,
|
|
|
|
NoNodeStyles,
|
2014-12-15 05:37:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns vec holding all the default render options.
|
2015-09-04 22:44:26 -05:00
|
|
|
pub fn default_options() -> Vec<RenderOption> {
|
|
|
|
vec![]
|
|
|
|
}
|
2014-12-15 05:37:42 -06:00
|
|
|
|
2014-04-25 12:10:03 -05:00
|
|
|
/// Renders directed graph `g` into the writer `w` in DOT syntax.
|
2014-12-15 05:37:42 -06:00
|
|
|
/// (Simple wrapper around `render_opts` that passes a default set of options.)
|
2016-01-26 15:23:05 -06:00
|
|
|
pub fn render<'a,N,E,G,W>(g: &'a G, w: &mut W) -> io::Result<()>
|
|
|
|
where N: Clone + 'a,
|
|
|
|
E: Clone + 'a,
|
|
|
|
G: Labeller<'a, Node=N, Edge=E> + GraphWalk<'a, Node=N, Edge=E>,
|
|
|
|
W: Write
|
|
|
|
{
|
2014-12-15 05:37:42 -06:00
|
|
|
render_opts(g, w, &[])
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Renders directed graph `g` into the writer `w` in DOT syntax.
|
|
|
|
/// (Main entry point for the library.)
|
2016-01-26 15:23:05 -06:00
|
|
|
pub fn render_opts<'a, N, E, G, W>(g: &'a G,
|
|
|
|
w: &mut W,
|
|
|
|
options: &[RenderOption])
|
|
|
|
-> io::Result<()>
|
|
|
|
where N: Clone + 'a,
|
|
|
|
E: Clone + 'a,
|
|
|
|
G: Labeller<'a, Node=N, Edge=E> + GraphWalk<'a, Node=N, Edge=E>,
|
|
|
|
W: Write
|
|
|
|
{
|
2015-09-04 22:44:26 -05:00
|
|
|
fn writeln<W: Write>(w: &mut W, arg: &[&str]) -> io::Result<()> {
|
|
|
|
for &s in arg {
|
2016-03-22 22:01:37 -05:00
|
|
|
w.write_all(s.as_bytes())?;
|
2015-09-04 22:44:26 -05:00
|
|
|
}
|
2015-03-11 17:24:14 -05:00
|
|
|
write!(w, "\n")
|
2014-04-25 12:10:03 -05:00
|
|
|
}
|
|
|
|
|
2015-09-04 22:44:26 -05:00
|
|
|
fn indent<W: Write>(w: &mut W) -> io::Result<()> {
|
2015-03-11 17:24:14 -05:00
|
|
|
w.write_all(b" ")
|
2014-04-25 12:10:03 -05:00
|
|
|
}
|
|
|
|
|
2016-03-22 22:01:37 -05:00
|
|
|
writeln(w, &["digraph ", g.graph_id().as_slice(), " {"])?;
|
2015-06-11 07:56:07 -05:00
|
|
|
for n in g.nodes().iter() {
|
2016-03-22 22:01:37 -05:00
|
|
|
indent(w)?;
|
2014-04-25 12:10:03 -05:00
|
|
|
let id = g.node_id(n);
|
2015-07-14 17:17:37 -05:00
|
|
|
|
2015-08-18 16:50:56 -05:00
|
|
|
let escaped = &g.node_label(n).to_dot_string();
|
|
|
|
let shape;
|
2015-07-14 17:17:37 -05:00
|
|
|
|
|
|
|
let mut text = vec![id.as_slice()];
|
|
|
|
|
|
|
|
if !options.contains(&RenderOption::NoNodeLabels) {
|
2015-08-18 16:50:56 -05:00
|
|
|
text.push("[label=");
|
2015-07-14 17:17:37 -05:00
|
|
|
text.push(escaped);
|
2015-08-18 16:50:56 -05:00
|
|
|
text.push("]");
|
2015-07-14 17:17:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let style = g.node_style(n);
|
|
|
|
if !options.contains(&RenderOption::NoNodeStyles) && style != Style::None {
|
|
|
|
text.push("[style=\"");
|
|
|
|
text.push(style.as_slice());
|
|
|
|
text.push("\"]");
|
2014-12-15 05:37:42 -06:00
|
|
|
}
|
2015-07-14 17:17:37 -05:00
|
|
|
|
2015-08-18 16:50:56 -05:00
|
|
|
if let Some(s) = g.node_shape(n) {
|
|
|
|
shape = s.to_dot_string();
|
|
|
|
text.push("[shape=");
|
|
|
|
text.push(&shape);
|
|
|
|
text.push("]");
|
|
|
|
}
|
|
|
|
|
2015-07-14 17:17:37 -05:00
|
|
|
text.push(";");
|
2016-03-22 22:01:37 -05:00
|
|
|
writeln(w, &text)?;
|
2014-04-25 12:10:03 -05:00
|
|
|
}
|
|
|
|
|
2015-06-11 07:56:07 -05:00
|
|
|
for e in g.edges().iter() {
|
2015-08-18 16:50:56 -05:00
|
|
|
let escaped_label = &g.edge_label(e).to_dot_string();
|
2016-03-22 22:01:37 -05:00
|
|
|
indent(w)?;
|
2014-04-25 12:10:03 -05:00
|
|
|
let source = g.source(e);
|
|
|
|
let target = g.target(e);
|
|
|
|
let source_id = g.node_id(&source);
|
|
|
|
let target_id = g.node_id(&target);
|
2015-07-14 17:17:37 -05:00
|
|
|
|
|
|
|
let mut text = vec![source_id.as_slice(), " -> ", target_id.as_slice()];
|
|
|
|
|
|
|
|
if !options.contains(&RenderOption::NoEdgeLabels) {
|
2015-08-18 16:50:56 -05:00
|
|
|
text.push("[label=");
|
2015-07-14 17:17:37 -05:00
|
|
|
text.push(escaped_label);
|
2015-08-18 16:50:56 -05:00
|
|
|
text.push("]");
|
2015-07-14 17:17:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let style = g.edge_style(e);
|
|
|
|
if !options.contains(&RenderOption::NoEdgeStyles) && style != Style::None {
|
|
|
|
text.push("[style=\"");
|
|
|
|
text.push(style.as_slice());
|
|
|
|
text.push("\"]");
|
2014-12-15 05:37:42 -06:00
|
|
|
}
|
2015-07-14 17:17:37 -05:00
|
|
|
|
|
|
|
text.push(";");
|
2016-03-22 22:01:37 -05:00
|
|
|
writeln(w, &text)?;
|
2014-04-25 12:10:03 -05:00
|
|
|
}
|
|
|
|
|
2014-11-17 02:39:01 -06:00
|
|
|
writeln(w, &["}"])
|
2014-04-25 12:10:03 -05:00
|
|
|
}
|
|
|
|
|
2016-01-15 12:07:52 -06:00
|
|
|
pub trait IntoCow<'a, B: ?Sized> where B: ToOwned {
|
|
|
|
fn into_cow(self) -> Cow<'a, B>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> IntoCow<'a, str> for String {
|
|
|
|
fn into_cow(self) -> Cow<'a, str> {
|
|
|
|
Cow::Owned(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> IntoCow<'a, str> for &'a str {
|
|
|
|
fn into_cow(self) -> Cow<'a, str> {
|
|
|
|
Cow::Borrowed(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T: Clone> IntoCow<'a, [T]> for Vec<T> {
|
|
|
|
fn into_cow(self) -> Cow<'a, [T]> {
|
|
|
|
Cow::Owned(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T: Clone> IntoCow<'a, [T]> for &'a [T] {
|
|
|
|
fn into_cow(self) -> Cow<'a, [T]> {
|
|
|
|
Cow::Borrowed(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-25 12:10:03 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2014-11-06 02:05:53 -06:00
|
|
|
use self::NodeLabels::*;
|
2015-07-14 17:17:37 -05:00
|
|
|
use super::{Id, Labeller, Nodes, Edges, GraphWalk, render, Style};
|
2015-08-18 16:50:56 -05:00
|
|
|
use super::LabelText::{self, LabelStr, EscStr, HtmlStr};
|
2015-03-11 17:24:14 -05:00
|
|
|
use std::io;
|
|
|
|
use std::io::prelude::*;
|
2016-01-15 12:07:52 -06:00
|
|
|
use IntoCow;
|
2014-04-25 12:10:03 -05:00
|
|
|
|
|
|
|
/// each node is an index in a vector in the graph.
|
2015-03-23 17:54:39 -05:00
|
|
|
type Node = usize;
|
2014-04-25 12:10:03 -05:00
|
|
|
struct Edge {
|
2015-07-14 17:17:37 -05:00
|
|
|
from: usize,
|
|
|
|
to: usize,
|
|
|
|
label: &'static str,
|
|
|
|
style: Style,
|
2014-04-25 12:10:03 -05:00
|
|
|
}
|
|
|
|
|
2015-07-14 17:17:37 -05:00
|
|
|
fn edge(from: usize, to: usize, label: &'static str, style: Style) -> Edge {
|
2015-11-23 17:11:20 -06:00
|
|
|
Edge {
|
|
|
|
from: from,
|
|
|
|
to: to,
|
|
|
|
label: label,
|
|
|
|
style: style,
|
|
|
|
}
|
2014-04-25 12:10:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct LabelledGraph {
|
|
|
|
/// The name for this graph. Used for labelling generated `digraph`.
|
|
|
|
name: &'static str,
|
|
|
|
|
|
|
|
/// Each node is an index into `node_labels`; these labels are
|
|
|
|
/// used as the label text for each node. (The node *names*,
|
|
|
|
/// which are unique identifiers, are derived from their index
|
|
|
|
/// in this array.)
|
|
|
|
///
|
|
|
|
/// If a node maps to None here, then just use its name as its
|
|
|
|
/// text.
|
|
|
|
node_labels: Vec<Option<&'static str>>,
|
|
|
|
|
2015-07-14 17:17:37 -05:00
|
|
|
node_styles: Vec<Style>,
|
|
|
|
|
2014-04-25 12:10:03 -05:00
|
|
|
/// Each edge relates a from-index to a to-index along with a
|
|
|
|
/// label; `edges` collects them.
|
|
|
|
edges: Vec<Edge>,
|
|
|
|
}
|
|
|
|
|
|
|
|
// A simple wrapper around LabelledGraph that forces the labels to
|
|
|
|
// be emitted as EscStr.
|
|
|
|
struct LabelledGraphWithEscStrs {
|
2015-09-04 22:44:26 -05:00
|
|
|
graph: LabelledGraph,
|
2014-04-25 12:10:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
enum NodeLabels<L> {
|
|
|
|
AllNodesLabelled(Vec<L>),
|
2015-03-23 17:54:39 -05:00
|
|
|
UnlabelledNodes(usize),
|
2014-04-25 12:10:03 -05:00
|
|
|
SomeNodesLabelled(Vec<Option<L>>),
|
|
|
|
}
|
|
|
|
|
|
|
|
type Trivial = NodeLabels<&'static str>;
|
|
|
|
|
|
|
|
impl NodeLabels<&'static str> {
|
|
|
|
fn to_opt_strs(self) -> Vec<Option<&'static str>> {
|
|
|
|
match self {
|
2015-09-04 22:44:26 -05:00
|
|
|
UnlabelledNodes(len) => vec![None; len],
|
2015-09-04 22:46:45 -05:00
|
|
|
AllNodesLabelled(lbls) => lbls.into_iter().map(|l| Some(l)).collect(),
|
2015-09-04 22:44:26 -05:00
|
|
|
SomeNodesLabelled(lbls) => lbls.into_iter().collect(),
|
2014-04-25 12:10:03 -05:00
|
|
|
}
|
|
|
|
}
|
2015-07-14 17:17:37 -05:00
|
|
|
|
|
|
|
fn len(&self) -> usize {
|
|
|
|
match self {
|
|
|
|
&UnlabelledNodes(len) => len,
|
|
|
|
&AllNodesLabelled(ref lbls) => lbls.len(),
|
|
|
|
&SomeNodesLabelled(ref lbls) => lbls.len(),
|
|
|
|
}
|
|
|
|
}
|
2014-04-25 12:10:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LabelledGraph {
|
|
|
|
fn new(name: &'static str,
|
|
|
|
node_labels: Trivial,
|
2015-07-14 17:17:37 -05:00
|
|
|
edges: Vec<Edge>,
|
2015-09-04 22:44:26 -05:00
|
|
|
node_styles: Option<Vec<Style>>)
|
|
|
|
-> LabelledGraph {
|
2015-07-14 17:17:37 -05:00
|
|
|
let count = node_labels.len();
|
2014-04-25 12:10:03 -05:00
|
|
|
LabelledGraph {
|
|
|
|
name: name,
|
|
|
|
node_labels: node_labels.to_opt_strs(),
|
2015-07-14 17:17:37 -05:00
|
|
|
edges: edges,
|
|
|
|
node_styles: match node_styles {
|
|
|
|
Some(nodes) => nodes,
|
|
|
|
None => vec![Style::None; count],
|
2015-09-04 22:44:26 -05:00
|
|
|
},
|
2014-04-25 12:10:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LabelledGraphWithEscStrs {
|
|
|
|
fn new(name: &'static str,
|
|
|
|
node_labels: Trivial,
|
2015-09-04 22:44:26 -05:00
|
|
|
edges: Vec<Edge>)
|
|
|
|
-> LabelledGraphWithEscStrs {
|
|
|
|
LabelledGraphWithEscStrs { graph: LabelledGraph::new(name, node_labels, edges, None) }
|
2014-04-25 12:10:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn id_name<'a>(n: &Node) -> Id<'a> {
|
2014-11-17 13:29:38 -06:00
|
|
|
Id::new(format!("N{}", *n)).unwrap()
|
2014-04-25 12:10:03 -05:00
|
|
|
}
|
|
|
|
|
2016-01-26 15:23:05 -06:00
|
|
|
impl<'a> Labeller<'a> for LabelledGraph {
|
|
|
|
type Node = Node;
|
|
|
|
type Edge = &'a Edge;
|
2014-04-25 12:10:03 -05:00
|
|
|
fn graph_id(&'a self) -> Id<'a> {
|
2015-02-18 13:48:57 -06:00
|
|
|
Id::new(&self.name[..]).unwrap()
|
2014-04-25 12:10:03 -05:00
|
|
|
}
|
|
|
|
fn node_id(&'a self, n: &Node) -> Id<'a> {
|
|
|
|
id_name(n)
|
|
|
|
}
|
|
|
|
fn node_label(&'a self, n: &Node) -> LabelText<'a> {
|
2014-09-09 04:32:58 -05:00
|
|
|
match self.node_labels[*n] {
|
2014-11-21 16:10:42 -06:00
|
|
|
Some(ref l) => LabelStr(l.into_cow()),
|
2015-09-04 22:44:26 -05:00
|
|
|
None => LabelStr(id_name(n).name()),
|
2014-04-25 12:10:03 -05:00
|
|
|
}
|
|
|
|
}
|
2015-09-04 22:44:26 -05:00
|
|
|
fn edge_label(&'a self, e: &&'a Edge) -> LabelText<'a> {
|
2014-11-21 16:10:42 -06:00
|
|
|
LabelStr(e.label.into_cow())
|
2014-04-25 12:10:03 -05:00
|
|
|
}
|
2015-07-14 17:17:37 -05:00
|
|
|
fn node_style(&'a self, n: &Node) -> Style {
|
|
|
|
self.node_styles[*n]
|
|
|
|
}
|
2015-09-04 22:44:26 -05:00
|
|
|
fn edge_style(&'a self, e: &&'a Edge) -> Style {
|
2015-07-14 17:17:37 -05:00
|
|
|
e.style
|
|
|
|
}
|
2014-04-25 12:10:03 -05:00
|
|
|
}
|
|
|
|
|
2016-01-26 15:23:05 -06:00
|
|
|
impl<'a> Labeller<'a> for LabelledGraphWithEscStrs {
|
|
|
|
type Node = Node;
|
|
|
|
type Edge = &'a Edge;
|
2015-09-04 22:44:26 -05:00
|
|
|
fn graph_id(&'a self) -> Id<'a> {
|
|
|
|
self.graph.graph_id()
|
|
|
|
}
|
|
|
|
fn node_id(&'a self, n: &Node) -> Id<'a> {
|
|
|
|
self.graph.node_id(n)
|
|
|
|
}
|
2014-04-25 12:10:03 -05:00
|
|
|
fn node_label(&'a self, n: &Node) -> LabelText<'a> {
|
|
|
|
match self.graph.node_label(n) {
|
2015-08-18 16:50:56 -05:00
|
|
|
LabelStr(s) | EscStr(s) | HtmlStr(s) => EscStr(s),
|
2014-04-25 12:10:03 -05:00
|
|
|
}
|
|
|
|
}
|
2015-09-04 22:44:26 -05:00
|
|
|
fn edge_label(&'a self, e: &&'a Edge) -> LabelText<'a> {
|
2014-04-25 12:10:03 -05:00
|
|
|
match self.graph.edge_label(e) {
|
2015-08-18 16:50:56 -05:00
|
|
|
LabelStr(s) | EscStr(s) | HtmlStr(s) => EscStr(s),
|
2014-04-25 12:10:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-26 15:23:05 -06:00
|
|
|
impl<'a> GraphWalk<'a> for LabelledGraph {
|
|
|
|
type Node = Node;
|
|
|
|
type Edge = &'a Edge;
|
2015-09-04 22:44:26 -05:00
|
|
|
fn nodes(&'a self) -> Nodes<'a, Node> {
|
2015-01-24 08:39:32 -06:00
|
|
|
(0..self.node_labels.len()).collect()
|
2014-04-25 12:10:03 -05:00
|
|
|
}
|
2015-09-04 22:44:26 -05:00
|
|
|
fn edges(&'a self) -> Edges<'a, &'a Edge> {
|
2014-04-25 12:10:03 -05:00
|
|
|
self.edges.iter().collect()
|
|
|
|
}
|
2015-09-04 22:44:26 -05:00
|
|
|
fn source(&'a self, edge: &&'a Edge) -> Node {
|
2014-04-25 12:10:03 -05:00
|
|
|
edge.from
|
|
|
|
}
|
2015-09-04 22:44:26 -05:00
|
|
|
fn target(&'a self, edge: &&'a Edge) -> Node {
|
2014-04-25 12:10:03 -05:00
|
|
|
edge.to
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-26 15:23:05 -06:00
|
|
|
impl<'a> GraphWalk<'a> for LabelledGraphWithEscStrs {
|
|
|
|
type Node = Node;
|
|
|
|
type Edge = &'a Edge;
|
2015-09-04 22:44:26 -05:00
|
|
|
fn nodes(&'a self) -> Nodes<'a, Node> {
|
2014-04-25 12:10:03 -05:00
|
|
|
self.graph.nodes()
|
|
|
|
}
|
2015-09-04 22:44:26 -05:00
|
|
|
fn edges(&'a self) -> Edges<'a, &'a Edge> {
|
2014-04-25 12:10:03 -05:00
|
|
|
self.graph.edges()
|
|
|
|
}
|
2015-09-04 22:44:26 -05:00
|
|
|
fn source(&'a self, edge: &&'a Edge) -> Node {
|
2014-04-25 12:10:03 -05:00
|
|
|
edge.from
|
|
|
|
}
|
2015-09-04 22:44:26 -05:00
|
|
|
fn target(&'a self, edge: &&'a Edge) -> Node {
|
2014-04-25 12:10:03 -05:00
|
|
|
edge.to
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-11 17:24:14 -05:00
|
|
|
fn test_input(g: LabelledGraph) -> io::Result<String> {
|
2014-11-11 15:01:29 -06:00
|
|
|
let mut writer = Vec::new();
|
2014-04-25 12:10:03 -05:00
|
|
|
render(&g, &mut writer).unwrap();
|
2015-03-11 17:24:14 -05:00
|
|
|
let mut s = String::new();
|
2016-03-22 22:01:37 -05:00
|
|
|
Read::read_to_string(&mut &*writer, &mut s)?;
|
2015-03-11 17:24:14 -05:00
|
|
|
Ok(s)
|
2014-04-25 12:10:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// All of the tests use raw-strings as the format for the expected outputs,
|
|
|
|
// so that you can cut-and-paste the content into a .dot file yourself to
|
|
|
|
// see what the graphviz visualizer would produce.
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty_graph() {
|
2015-09-04 22:44:26 -05:00
|
|
|
let labels: Trivial = UnlabelledNodes(0);
|
2015-07-14 17:17:37 -05:00
|
|
|
let r = test_input(LabelledGraph::new("empty_graph", labels, vec![], None));
|
2014-11-27 12:19:57 -06:00
|
|
|
assert_eq!(r.unwrap(),
|
2014-04-25 12:10:03 -05:00
|
|
|
r#"digraph empty_graph {
|
|
|
|
}
|
|
|
|
"#);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn single_node() {
|
2015-09-04 22:44:26 -05:00
|
|
|
let labels: Trivial = UnlabelledNodes(1);
|
2015-07-14 17:17:37 -05:00
|
|
|
let r = test_input(LabelledGraph::new("single_node", labels, vec![], None));
|
2014-11-27 12:19:57 -06:00
|
|
|
assert_eq!(r.unwrap(),
|
2014-04-25 12:10:03 -05:00
|
|
|
r#"digraph single_node {
|
|
|
|
N0[label="N0"];
|
|
|
|
}
|
|
|
|
"#);
|
|
|
|
}
|
|
|
|
|
2015-07-14 17:17:37 -05:00
|
|
|
#[test]
|
|
|
|
fn single_node_with_style() {
|
2015-09-04 22:44:26 -05:00
|
|
|
let labels: Trivial = UnlabelledNodes(1);
|
2015-07-14 17:17:37 -05:00
|
|
|
let styles = Some(vec![Style::Dashed]);
|
|
|
|
let r = test_input(LabelledGraph::new("single_node", labels, vec![], styles));
|
|
|
|
assert_eq!(r.unwrap(),
|
|
|
|
r#"digraph single_node {
|
|
|
|
N0[label="N0"][style="dashed"];
|
|
|
|
}
|
|
|
|
"#);
|
|
|
|
}
|
|
|
|
|
2014-04-25 12:10:03 -05:00
|
|
|
#[test]
|
|
|
|
fn single_edge() {
|
2015-09-04 22:44:26 -05:00
|
|
|
let labels: Trivial = UnlabelledNodes(2);
|
|
|
|
let result = test_input(LabelledGraph::new("single_edge",
|
|
|
|
labels,
|
|
|
|
vec![edge(0, 1, "E", Style::None)],
|
|
|
|
None));
|
2014-11-27 12:19:57 -06:00
|
|
|
assert_eq!(result.unwrap(),
|
2014-04-25 12:10:03 -05:00
|
|
|
r#"digraph single_edge {
|
|
|
|
N0[label="N0"];
|
|
|
|
N1[label="N1"];
|
|
|
|
N0 -> N1[label="E"];
|
|
|
|
}
|
|
|
|
"#);
|
|
|
|
}
|
|
|
|
|
2015-07-14 17:17:37 -05:00
|
|
|
#[test]
|
|
|
|
fn single_edge_with_style() {
|
2015-09-04 22:44:26 -05:00
|
|
|
let labels: Trivial = UnlabelledNodes(2);
|
|
|
|
let result = test_input(LabelledGraph::new("single_edge",
|
|
|
|
labels,
|
|
|
|
vec![edge(0, 1, "E", Style::Bold)],
|
|
|
|
None));
|
2015-07-14 17:17:37 -05:00
|
|
|
assert_eq!(result.unwrap(),
|
|
|
|
r#"digraph single_edge {
|
|
|
|
N0[label="N0"];
|
|
|
|
N1[label="N1"];
|
|
|
|
N0 -> N1[label="E"][style="bold"];
|
|
|
|
}
|
|
|
|
"#);
|
|
|
|
}
|
|
|
|
|
2014-10-05 05:11:17 -05:00
|
|
|
#[test]
|
|
|
|
fn test_some_labelled() {
|
2015-09-04 22:44:26 -05:00
|
|
|
let labels: Trivial = SomeNodesLabelled(vec![Some("A"), None]);
|
2015-07-14 17:17:37 -05:00
|
|
|
let styles = Some(vec![Style::None, Style::Dotted]);
|
2015-09-04 22:44:26 -05:00
|
|
|
let result = test_input(LabelledGraph::new("test_some_labelled",
|
|
|
|
labels,
|
|
|
|
vec![edge(0, 1, "A-1", Style::None)],
|
|
|
|
styles));
|
2014-11-27 12:19:57 -06:00
|
|
|
assert_eq!(result.unwrap(),
|
2014-10-05 05:11:17 -05:00
|
|
|
r#"digraph test_some_labelled {
|
|
|
|
N0[label="A"];
|
2015-07-14 17:17:37 -05:00
|
|
|
N1[label="N1"][style="dotted"];
|
2014-10-05 05:11:17 -05:00
|
|
|
N0 -> N1[label="A-1"];
|
|
|
|
}
|
|
|
|
"#);
|
|
|
|
}
|
|
|
|
|
2014-04-25 12:10:03 -05:00
|
|
|
#[test]
|
|
|
|
fn single_cyclic_node() {
|
2015-09-04 22:44:26 -05:00
|
|
|
let labels: Trivial = UnlabelledNodes(1);
|
|
|
|
let r = test_input(LabelledGraph::new("single_cyclic_node",
|
|
|
|
labels,
|
|
|
|
vec![edge(0, 0, "E", Style::None)],
|
|
|
|
None));
|
2014-11-27 12:19:57 -06:00
|
|
|
assert_eq!(r.unwrap(),
|
2014-04-25 12:10:03 -05:00
|
|
|
r#"digraph single_cyclic_node {
|
|
|
|
N0[label="N0"];
|
|
|
|
N0 -> N0[label="E"];
|
|
|
|
}
|
|
|
|
"#);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn hasse_diagram() {
|
2015-11-23 17:11:20 -06:00
|
|
|
let labels = AllNodesLabelled(vec!["{x,y}", "{x}", "{y}", "{}"]);
|
2015-09-04 22:44:26 -05:00
|
|
|
let r = test_input(LabelledGraph::new("hasse_diagram",
|
|
|
|
labels,
|
2015-09-04 22:46:45 -05:00
|
|
|
vec![edge(0, 1, "", Style::None),
|
|
|
|
edge(0, 2, "", Style::None),
|
|
|
|
edge(1, 3, "", Style::None),
|
|
|
|
edge(2, 3, "", Style::None)],
|
2015-09-04 22:44:26 -05:00
|
|
|
None));
|
2014-11-27 12:19:57 -06:00
|
|
|
assert_eq!(r.unwrap(),
|
2014-04-25 12:10:03 -05:00
|
|
|
r#"digraph hasse_diagram {
|
|
|
|
N0[label="{x,y}"];
|
|
|
|
N1[label="{x}"];
|
|
|
|
N2[label="{y}"];
|
|
|
|
N3[label="{}"];
|
|
|
|
N0 -> N1[label=""];
|
|
|
|
N0 -> N2[label=""];
|
|
|
|
N1 -> N3[label=""];
|
|
|
|
N2 -> N3[label=""];
|
|
|
|
}
|
|
|
|
"#);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn left_aligned_text() {
|
2015-11-23 17:11:20 -06:00
|
|
|
let labels = AllNodesLabelled(vec![
|
2014-04-25 12:10:03 -05:00
|
|
|
"if test {\
|
|
|
|
\\l branch1\
|
|
|
|
\\l} else {\
|
|
|
|
\\l branch2\
|
|
|
|
\\l}\
|
|
|
|
\\lafterward\
|
|
|
|
\\l",
|
|
|
|
"branch1",
|
|
|
|
"branch2",
|
2015-11-23 17:11:20 -06:00
|
|
|
"afterward"]);
|
2014-04-25 12:10:03 -05:00
|
|
|
|
2014-11-11 15:01:29 -06:00
|
|
|
let mut writer = Vec::new();
|
2014-04-25 12:10:03 -05:00
|
|
|
|
2015-09-04 22:44:26 -05:00
|
|
|
let g = LabelledGraphWithEscStrs::new("syntax_tree",
|
|
|
|
labels,
|
2015-09-04 22:46:45 -05:00
|
|
|
vec![edge(0, 1, "then", Style::None),
|
|
|
|
edge(0, 2, "else", Style::None),
|
|
|
|
edge(1, 3, ";", Style::None),
|
|
|
|
edge(2, 3, ";", Style::None)]);
|
2014-04-25 12:10:03 -05:00
|
|
|
|
|
|
|
render(&g, &mut writer).unwrap();
|
2015-03-11 17:24:14 -05:00
|
|
|
let mut r = String::new();
|
|
|
|
Read::read_to_string(&mut &*writer, &mut r).unwrap();
|
2014-04-25 12:10:03 -05:00
|
|
|
|
2015-03-11 17:24:14 -05:00
|
|
|
assert_eq!(r,
|
2014-04-25 12:10:03 -05:00
|
|
|
r#"digraph syntax_tree {
|
|
|
|
N0[label="if test {\l branch1\l} else {\l branch2\l}\lafterward\l"];
|
|
|
|
N1[label="branch1"];
|
|
|
|
N2[label="branch2"];
|
|
|
|
N3[label="afterward"];
|
|
|
|
N0 -> N1[label="then"];
|
|
|
|
N0 -> N2[label="else"];
|
|
|
|
N1 -> N3[label=";"];
|
|
|
|
N2 -> N3[label=";"];
|
|
|
|
}
|
|
|
|
"#);
|
|
|
|
}
|
2014-11-12 09:21:03 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple_id_construction() {
|
2014-11-17 17:24:27 -06:00
|
|
|
let id1 = Id::new("hello");
|
2014-11-12 09:21:03 -06:00
|
|
|
match id1 {
|
2015-09-04 22:46:45 -05:00
|
|
|
Ok(_) => {}
|
2015-09-04 22:44:26 -05:00
|
|
|
Err(..) => panic!("'hello' is not a valid value for id anymore"),
|
2014-11-12 09:21:03 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn badly_formatted_id() {
|
2014-11-17 17:24:27 -06:00
|
|
|
let id2 = Id::new("Weird { struct : ure } !!!");
|
2014-11-12 09:21:03 -06:00
|
|
|
match id2 {
|
std: Stabilize the std::fmt module
This commit performs a final stabilization pass over the std::fmt module,
marking all necessary APIs as stable. One of the more interesting aspects of
this module is that it exposes a good deal of its runtime representation to the
outside world in order for `format_args!` to be able to construct the format
strings. Instead of hacking the compiler to assume that these items are stable,
this commit instead lays out a story for the stabilization and evolution of
these APIs.
There are three primary details used by the `format_args!` macro:
1. `Arguments` - an opaque package of a "compiled format string". This structure
is passed around and the `write` function is the source of truth for
transforming a compiled format string into a string at runtime. This must be
able to be constructed in stable code.
2. `Argument` - an opaque structure representing an argument to a format string.
This is *almost* a trait object as it's just a pointer/function pair, but due
to the function originating from one of many traits, it's not actually a
trait object. Like `Arguments`, this must be constructed from stable code.
3. `fmt::rt` - this module contains the runtime type definitions primarily for
the `rt::Argument` structure. Whenever an argument is formatted with
nonstandard flags, a corresponding `rt::Argument` is generated describing how
the argument is being formatted. This can be used to construct an
`Arguments`.
The primary interface to `std::fmt` is the `Arguments` structure, and as such
this type name is stabilize as-is today. It is expected for libraries to pass
around an `Arguments` structure to represent a pending formatted computation.
The remaining portions are largely "cruft" which would rather not be stabilized,
but due to the stability checks they must be. As a result, almost all pieces
have been renamed to represent that they are "version 1" of the formatting
representation. The theory is that at a later date if we change the
representation of these types we can add new definitions called "version 2" and
corresponding constructors for `Arguments`.
One of the other remaining large questions about the fmt module were how the
pending I/O reform would affect the signatures of methods in the module. Due to
[RFC 526][rfc], however, the writers of fmt are now incompatible with the
writers of io, so this question has largely been solved. As a result the
interfaces are largely stabilized as-is today.
[rfc]: https://github.com/rust-lang/rfcs/blob/master/text/0526-fmt-text-writer.md
Specifically, the following changes were made:
* The contents of `fmt::rt` were all moved under `fmt::rt::v1`
* `fmt::rt` is stable
* `fmt::rt::v1` is stable
* `Error` is stable
* `Writer` is stable
* `Writer::write_str` is stable
* `Writer::write_fmt` is stable
* `Formatter` is stable
* `Argument` has been renamed to `ArgumentV1` and is stable
* `ArgumentV1::new` is stable
* `ArgumentV1::from_uint` is stable
* `Arguments::new_v1` is stable (renamed from `new`)
* `Arguments::new_v1_formatted` is stable (renamed from `with_placeholders`)
* All formatting traits are now stable, as well as the `fmt` method.
* `fmt::write` is stable
* `fmt::format` is stable
* `Formatter::pad_integral` is stable
* `Formatter::pad` is stable
* `Formatter::write_str` is stable
* `Formatter::write_fmt` is stable
* Some assorted top level items which were only used by `format_args!` were
removed in favor of static functions on `ArgumentV1` as well.
* The formatting-flag-accessing methods remain unstable
Within the contents of the `fmt::rt::v1` module, the following actions were
taken:
* Reexports of all enum variants were removed
* All prefixes on enum variants were removed
* A few miscellaneous enum variants were renamed
* Otherwise all structs, fields, and variants were marked stable.
In addition to these actions in the `std::fmt` module, many implementations of
`Show` and `String` were stabilized as well.
In some other modules:
* `ToString` is now stable
* `ToString::to_string` is now stable
* `Vec` no longer implements `fmt::Writer` (this has moved to `String`)
This is a breaking change due to all of the changes to the `fmt::rt` module, but
this likely will not have much impact on existing programs.
Closes #20661
[breaking-change]
2015-01-13 17:42:53 -06:00
|
|
|
Ok(_) => panic!("graphviz id suddenly allows spaces, brackets and stuff"),
|
2015-09-04 22:46:45 -05:00
|
|
|
Err(..) => {}
|
2014-11-12 09:21:03 -06:00
|
|
|
}
|
|
|
|
}
|
2014-04-25 12:10:03 -05:00
|
|
|
}
|