Skip to main content

Crate graphity_dot

Crate graphity_dot 

Source
Expand description

Export of graphs to DOT (the language of Graphviz) to look at them with its layout tools.

This crate is the counterpart of the Java module graphity-dot, package com.kobayami.graphity.dot, released with graphity-core at the same version. DotWriter writes a graph as DOT text. A graph holds no data, so the writer takes labels and attributes from hooks that receive a node ID or an edge index and read the caller’s arrays. Attributes is the map that the hook for the attributes of one node or edge fills. The clusters come from a partition of the nodes, with a hook for their labels. Here, the packages of a small program that depend on each other in a cycle:

// The packages are the nodes of dependencies, names[node] is the name of a package,
// and sccs holds the strongly connected components of dependencies.

// Write the packages that lie on a cycle in DOT, each labeled with its name.
let dot = DotWriter::of(&dependencies)
    .node_label(|node| Some(names[node as usize].into()))
    .node_filter(|node| sccs.contains_cycle(sccs.scc_of(node)))
    .write_to_string();
// digraph {
//   4 [label="model"];
//   5 [label="storage"];
//   4 -> 5;
//   5 -> 4;
// }

The node filter keeps the node IDs of the graph, so the hooks read the same arrays as for the whole graph. For Markdown and documentation, the crate graphity-mermaid writes Mermaid flowcharts.

§Hooks, output and errors

  • A hook is a closure: Fn(u32) -> Option<Cow<'a, str>> for labels, Fn(u32) -> bool for the node filter, and Fn(u32, &mut Attributes<'a>) for the attributes of a node or an edge (Java’s AttributeHook). None means none, as Java’s null. The writer borrows the graph, the partition of the clusters and the hooks for its lifetime 'a, so a hook reads the caller’s arrays without copying them: a text that is already there is returned as a borrowed Cow (Some(names[node as usize].as_str().into())), a computed one as an owned String (Some(format!("v{node}").into())).
  • DotWriter::write takes any std::io::Write and writes UTF-8. That is the trait of files, standard output and sockets, which is where a DOT text goes. DotWriter::write_to_string returns a String. These are Java’s write(Appendable) and writeToString().
  • The configuration methods consume and return the writer, so the calls chain as in Java; a configured writer writes any number of times through &self.
  • An invalid attribute name panics with an invalid argument: message, as the core does for an argument out of its range ([graphity_core::error]). This holds whether it is passed to a configuration method or put by a hook. Java throws IllegalArgumentException. A cluster partition over a different number of nodes panics the same way. write returns the errors of its output.

Structs§

Attributes
The attributes of one node or edge, name to value, in the order they were put: the map that the hooks of DotWriter::node_attributes and DotWriter::edge_attributes fill.
DotWriter
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.
Iter
The entries of an Attributes map, in the order they were put, as Attributes::iter returns them.