pub struct DotWriter<'a> { /* private fields */ }Expand description
A writer of DOT (the language of Graphviz) that labels and styles a graph with the caller’s data by node ID and edge index.
DotWriter::of returns a writer for a graph, the methods that configure it return the writer
again, and write or write_to_string write the text. The
graph holds no data, so labels and attributes come from hooks that receive a node ID or an edge
index and read the caller’s arrays. Here, the packages of a small program get their names from
names. The strongly connected components sccs tell which packages are in a cycle, and these are
drawn in red:
let dot = DotWriter::of(&dependencies)
.node_label(|node| Some(names[node as usize].into()))
.node_attributes(|node, attributes| {
if sccs.contains_cycle(sccs.scc_of(node)) {
attributes.insert("color", "red");
}
})
.write_to_string();
// model and storage, the cycle, in red:
// 4 [label="model", color="red"];
// 5 [label="storage", color="red"];Nodes are named by their node IDs. The text lists the graph attributes, the clusters in group order with their nodes, then the nodes in no cluster, then the edges in edge index order, so the same graph and hooks always give the same text. A node outside the clusters is listed only if it has a label or attributes, or no edge in the text; Graphviz creates the other nodes from their edges.
Labels and attribute values are written as quoted strings: the writer escapes quotes and
backslashes and turns line breaks into DOT’s \n. Graphviz decodes HTML entities such as < in
the attributes label, xlabel, headlabel and taillabel. In these, the writer also turns &
into &, so that a label shows exactly its text. Other values, such as a URL, keep their
&. NUL, which DOT cannot hold, is written as U+FFFD.
Attribute names must be identifiers - an ASCII letter or underscore, then ASCII letters, digits or
underscores - other than the DOT keywords node, edge, graph, digraph, subgraph and
strict, in any letter case.
The label hooks return a Cow: a &str into the caller’s data, which lives for the writer’s
lifetime 'a, is written without a copy (Some(names[node as usize].as_str().into())), and a
computed text is a String (Some(format!("v{node}").into())). None is Java’s null.
A writer keeps its configuration and can write any number of times. The graph, the partition and
the data the hooks read are borrowed for its lifetime 'a. The hooks are Fn closures, called
from write through &self; a hook that counts its calls needs a
Cell. For Markdown and documentation, MermaidWriter in the crate
graphity-mermaid writes a Mermaid flowchart.
Since the hooks are arbitrary closures, the writer is neither Send nor Sync - Java’s instances
are not safe to share between threads either.
Implementations§
Source§impl<'a> DotWriter<'a>
impl<'a> DotWriter<'a>
Sourcepub fn of(graph: &'a Graph) -> Self
pub fn of(graph: &'a Graph) -> Self
Returns a writer for graph, with no labels, attributes, clusters or filter yet. The graph of a
BiAdjacentGraph is accepted as well, as a &Graph.
Time: O(1).
Sourcepub fn graph_attribute(
self,
name: impl Into<Cow<'a, str>>,
value: impl Into<Cow<'a, str>>,
) -> Self
pub fn graph_attribute( self, name: impl Into<Cow<'a, str>>, value: impl Into<Cow<'a, str>>, ) -> Self
Sets an attribute of the graph, such as rankdir to LR.
Time: O(number of graph attributes), as the attributes are a vector of pairs, which for the handful of attributes of a graph beats hashing.
§Panics
With an invalid argument message if name is not an identifier or is a DOT keyword.
Sourcepub fn node_label(
self,
label: impl Fn(u32) -> Option<Cow<'a, str>> + 'a,
) -> Self
pub fn node_label( self, label: impl Fn(u32) -> Option<Cow<'a, str>> + 'a, ) -> Self
Sets the label of each node, by node ID; None leaves a node unlabeled, and Graphviz then shows
its node ID. label is called with a node ID and returns the label of the node or None.
Time: O(1).
Sourcepub fn edge_label(
self,
label: impl Fn(u32) -> Option<Cow<'a, str>> + 'a,
) -> Self
pub fn edge_label( self, label: impl Fn(u32) -> Option<Cow<'a, str>> + 'a, ) -> Self
Sets the label of each edge, by edge index; None leaves an edge unlabeled. label is called with
an edge index and returns the label of the edge or None.
Time: O(1).
Sourcepub fn node_attributes(
self,
hook: impl Fn(u32, &mut Attributes<'a>) + 'a,
) -> Self
pub fn node_attributes( self, hook: impl Fn(u32, &mut Attributes<'a>) + 'a, ) -> Self
Sets the hook that adds DOT attributes, such as shape, color or style, to each node, by node
ID - Java’s AttributeHook is a closure here, and its map an Attributes.
The hook receives the label from node_label as the attribute label, which
it can replace or remove. hook is called with each node that is written and puts its attributes.
Time: O(1).
Sourcepub fn edge_attributes(
self,
hook: impl Fn(u32, &mut Attributes<'a>) + 'a,
) -> Self
pub fn edge_attributes( self, hook: impl Fn(u32, &mut Attributes<'a>) + 'a, ) -> Self
Sets the hook that adds DOT attributes, such as color or style, to each edge, by edge index.
The hook receives the label from edge_label as the attribute label, which
it can replace or remove. hook is called with each edge that is written and puts its attributes.
Time: O(1).
Sourcepub fn clusters(
self,
partition: &'a NodePartition,
label: impl Fn(u32) -> Option<Cow<'a, str>> + 'a,
) -> Self
pub fn clusters( self, partition: &'a NodePartition, label: impl Fn(u32) -> Option<Cow<'a, str>> + 'a, ) -> Self
Draws each group of partition as a cluster, with a label by group ID. label is called with a
group ID and returns the label of its cluster or None.
Nodes in no group stay outside every cluster, and a group without a node to write has no cluster.
A label of None leaves a cluster unlabeled, also when the graph has a label, which Graphviz
would otherwise repeat on every cluster. An Sccs is accepted as well, as
a &NodePartition.
Time: O(1).
§Panics
With an invalid argument message if partition has a different node count than the graph.
Sourcepub fn node_filter(self, include: impl Fn(u32) -> bool + 'a) -> Self
pub fn node_filter(self, include: impl Fn(u32) -> bool + 'a) -> Self
Restricts the text to the nodes that include accepts and the edges between them.
Node IDs stay those of the graph, so the hooks keep reading the same arrays.
Time: O(1).
Sourcepub fn undirected(self) -> Self
pub fn undirected(self) -> Self
Switches to the undirected reading of the graph: a graph with -- edges instead of a digraph.
The edges a -> b and b -> a become one, written as the edge from the lower node ID; the edge
hooks receive its edge index.
Time: O(1).
Sourcepub fn write<W: Write>(&self, out: W) -> Result<()>
pub fn write<W: Write>(&self, out: W) -> Result<()>
Writes the graph to out, as DOT text.
out is any io::Write, such as a file, standard output, a socket or a Vec<u8>, and receives
the text in UTF-8; Java takes an Appendable. It is taken by value: lend it with &mut out to
keep using it, and flush it yourself; this call neither flushes nor closes it. Passed by value, it
is dropped when the call returns, which closes a File and flushes a
BufWriter without reporting an error. The writer issues many small writes,
so an unbuffered out such as a File is best wrapped in a BufWriter, lent with &mut and
flushed afterwards.
Time: O(node_count + edge_count + group_count), plus the hooks, with group_count the number of groups of the partition drawn as clusters; in the undirected reading, O(log out_degree(target)) more per edge. Memory: node_count bytes while it runs.
§Errors
The first error of out - Java’s IOException.
§Panics
With an invalid argument message if an attribute hook puts a name that is not an identifier or
is a DOT keyword; what was written before stays in out.