Skip to main content

graphity_mermaid/
lib.rs

1//! Export of graphs to Mermaid flowcharts, which render directly in Markdown, in issues and in
2//! documentation.
3//!
4//! This crate is the counterpart of the Java module `graphity-mermaid`, package
5//! `com.kobayami.graphity.mermaid`, released with `graphity-core` at the same version.
6//! [`MermaidWriter`] writes a graph as the text of a Mermaid flowchart, in the [`Direction`] of choice
7//! and with node [`Shape`]s. A graph holds no data, so the writer takes labels, shapes, classes and
8//! styles from hooks that receive a node ID or an edge index and read the caller's arrays. The
9//! clusters come from a partition of the nodes, with a hook for their labels. Here, the packages of a
10//! small program that depend on each other in a cycle:
11//!
12//! <!-- example: writing_mermaid.rs#cycle -->
13//! ```
14//! # use graphity_mermaid::MermaidWriter;
15//! # let dependencies = graphity_core::Graph::of(
16//! #     &[0, 3, 4, 7, 9, 10, 13, 14, 14],
17//! #     &[1, 2, 3, 7, 3, 4, 7, 4, 5, 5, 4, 6, 7, 7],
18//! # ).unwrap();
19//! # let names: &[&str] = &["app", "cli", "ui", "service", "model", "storage", "json", "util"];
20//! # let sccs = graphity_core::components::sccs_of(&dependencies);
21//! // The packages are the nodes of dependencies, names[node] is the name of a package,
22//! // and sccs holds the strongly connected components of dependencies.
23//!
24//! // Write the packages that lie on a cycle as a Mermaid flowchart, each labeled with
25//! // its name.
26//! let mermaid = MermaidWriter::of(&dependencies)
27//!     .node_label(|node| Some(names[node as usize].into()))
28//!     .node_filter(|node| sccs.contains_cycle(sccs.scc_of(node)))
29//!     .write_to_string();
30//! // flowchart TD
31//! //   n4["model"]
32//! //   n5["storage"]
33//! //   n4 --> n5
34//! //   n5 --> n4
35//! # assert_eq!(mermaid, "flowchart TD\n  n4[\"model\"]\n  n5[\"storage\"]\n  n4 --> n5\n  n5 --> n4\n");
36//! ```
37//!
38//! The node filter keeps the node IDs of the graph, so the hooks read the same arrays as for the whole
39//! graph. Mermaid renders graphs of a few hundred nodes well; for larger ones, the crate
40//! `graphity-dot` writes DOT (the language of Graphviz).
41//!
42//! # Hooks, output and errors
43//!
44//! - A hook is a closure: `Fn(u32) -> Option<Cow<'a, str>>` for labels, classes and styles,
45//!   `Fn(u32) -> Option<Shape>` for shapes and `Fn(u32) -> bool` for the node filter. `None`
46//!   means none, as Java's `null`. The writer borrows the graph, the partition of the clusters and
47//!   the hooks for its lifetime `'a`, so a hook reads the caller's arrays without copying them: a
48//!   text that is already there is returned as a borrowed [`Cow`](std::borrow::Cow)
49//!   (`Some(names[node as usize].as_str().into())`), a computed one as an owned `String`
50//!   (`Some(format!("v{node}").into())`).
51//! - [`MermaidWriter::write`] takes any [`std::io::Write`] and writes UTF-8. That is the trait of
52//!   files, standard output and sockets. [`MermaidWriter::write_to_string`] returns a `String`. These
53//!   are Java's `write(Appendable)` and `writeToString()`.
54//! - The configuration methods consume and return the writer, so the calls chain as in Java; a
55//!   configured writer writes any number of times through `&self`.
56//! - An invalid class name or style panics with an `invalid argument: ` message, as the core does for
57//!   an argument out of its range ([`graphity_core::error`]). This holds whether it is passed to a
58//!   configuration method or returned by a hook. Java throws `IllegalArgumentException`. A cluster
59//!   partition over a different number of nodes panics the same way. `write` returns the errors of its
60//!   output.
61
62mod class_defs;
63mod mermaid_writer;
64mod selection;
65
66pub use mermaid_writer::{Direction, MermaidWriter, Shape};