Skip to main content

graphity_dot/
lib.rs

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