Skip to main content

Crate graphity_mermaid

Crate graphity_mermaid 

Source
Expand description

Export of graphs to Mermaid flowcharts, which render directly in Markdown, in issues and in documentation.

This crate is the counterpart of the Java module graphity-mermaid, package com.kobayami.graphity.mermaid, released with graphity-core at the same version. MermaidWriter writes a graph as the text of a Mermaid flowchart, in the Direction of choice and with node Shapes. A graph holds no data, so the writer takes labels, shapes, classes and styles from hooks that receive a node ID or an edge index and read the caller’s arrays. 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 as a Mermaid flowchart, each labeled with
// its name.
let mermaid = MermaidWriter::of(&dependencies)
    .node_label(|node| Some(names[node as usize].into()))
    .node_filter(|node| sccs.contains_cycle(sccs.scc_of(node)))
    .write_to_string();
// flowchart TD
//   n4["model"]
//   n5["storage"]
//   n4 --> n5
//   n5 --> n4

The node filter keeps the node IDs of the graph, so the hooks read the same arrays as for the whole graph. Mermaid renders graphs of a few hundred nodes well; for larger ones, the crate graphity-dot writes DOT (the language of Graphviz).

§Hooks, output and errors

  • A hook is a closure: Fn(u32) -> Option<Cow<'a, str>> for labels, classes and styles, Fn(u32) -> Option<Shape> for shapes and Fn(u32) -> bool for the node filter. 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())).
  • MermaidWriter::write takes any std::io::Write and writes UTF-8. That is the trait of files, standard output and sockets. MermaidWriter::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 class name or style 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 returned 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§

MermaidWriter
A writer of Mermaid flowcharts that labels and styles a graph with the caller’s data by node ID and edge index. Mermaid flowcharts render in Markdown.

Enums§

Direction
The direction of a flowchart - Java’s nested enum MermaidWriter.Direction, a type of the crate here, as Rust has no nested types.
Shape
The shape of a node, with the brackets Mermaid draws it with - Java’s nested enum MermaidWriter.Shape, a type of the crate here, as Rust has no nested types.