Skip to main content

DotWriter

Struct DotWriter 

Source
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 &lt; in the attributes label, xlabel, headlabel and taillabel. In these, the writer also turns & into &amp;, 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>

Source

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).

Source

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.

Source

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).

Source

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).

Source

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).

Source

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).

Source

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.

Source

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).

Source

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).

Source

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.

Source

pub fn write_to_string(&self) -> String

Returns the DOT text of the graph, as write writes it.

Time: as for write. Memory: the text for the result, whose buffer may hold up to as much again in spare capacity; while it runs, node_count bytes more.

§Panics

As for write.

Trait Implementations§

Source§

impl Debug for DotWriter<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for DotWriter<'a>

§

impl<'a> !Send for DotWriter<'a>

§

impl<'a> !Sync for DotWriter<'a>

§

impl<'a> !UnwindSafe for DotWriter<'a>

§

impl<'a> Freeze for DotWriter<'a>

§

impl<'a> Unpin for DotWriter<'a>

§

impl<'a> UnsafeUnpin for DotWriter<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.