Skip to main content

Crate rich_diagram

Crate rich_diagram 

Source
Expand description

Graph diagrams for the rich Rust port: a graph model, a layered layout, and a renderable that draws it with box-drawing characters.

Not a port of anything upstream: an rs-rich addition. It depends on core rich only, so any crate (Mermaid flowcharts among them) can draw graphs without pulling in rs-rich-ext.

  • Graph: nodes with labels and Shapes, edges with labels, Strokes and Heads, flowing in a Direction. Build one in code with its chaining builder, or from parts as a parser does.
  • draw: the layered (Sugiyama-style) layout, giving a Drawing: plain lines of text. See layout for how it works.
  • Diagram: the drawing as a renderable that measures itself and crops to the width it is given, in Unicode box drawing or ASCII.
use rich::Console;
use rich_diagram::{Diagram, Direction, Graph, Shape};

let graph = Graph::new(Direction::TopDown)
    .node("start", "Start").shape(Shape::Round)
    .node("ok", "Tests pass?").shape(Shape::Rhombus)
    .edge("start", "ok")
    .edge("ok", "ship").label("yes");
let console = Console::builder().width(40).color_system(None).build();
let out = console.render_export(&Diagram::new(graph));
assert!(out.contains("Tests pass?"), "{out}");
assert!(out.lines().all(|line| rich::cells::cell_len(line) <= 40));

Sources:

  • dot: DOT (Graphviz) sources, parsed natively (dot::parse) and drawn by the Dot renderable. With the plugin feature, plugin::DotPlugin registers a dot fence renderer and source renderer; with the graphviz feature, graphviz::render_svg runs Graphviz’s own dot for SVG.
  • er: entity-relationship diagrams. An ErModel of entities, columns and keys, relationships and groups, built in code (a schema reader fills it), drawn by the ErDiagram renderable.

Groupings: a Cluster is drawn as a dashed frame around its nodes, its label in the top border, nested clusters inside it; a same-rank group (Graph::same_rank) puts its nodes side by side where the layered layout can. What a drawing could not show is in Drawing::notes.

Re-exports§

pub use diagram::Diagram;
pub use dot::Dot;
pub use dot::DotError;
pub use dot::DotGraph;
pub use er::ErDiagram;
pub use er::ErModel;
pub use graph::Cluster;
pub use graph::Direction;
pub use graph::Edge;
pub use graph::Graph;
pub use graph::Head;
pub use graph::MAX_EDGE_LENGTH;
pub use graph::MAX_EDGES;
pub use graph::MAX_NODES;
pub use graph::MAX_SOURCE;
pub use graph::Node;
pub use graph::Shape;
pub use graph::Stroke;
pub use layout::DrawError;
pub use layout::Drawing;
pub use layout::draw;

Modules§

diagram
Diagram: a Graph as a renderable.
dot
DOT (Graphviz) sources, parsed natively into a Graph (#240).
er
Entity-relationship diagrams (#247): tables, their columns and keys, and the relationships between them, drawn through the layered layout.
graph
The graph model: nodes with labels and shapes, edges with labels, strokes and heads, and the direction the graph flows.
graphviz
Render a DOT source with Graphviz’s own dot binary, for SVG.
layout
Lay out a Graph in ranks and draw it with box-drawing characters.
plugin
DotPlugin: DOT through the plugin API, like rs-rich-mermaid’s MermaidPlugin. A fence renderer draws ```dot (and ```graphviz) blocks in Markdown, and a dot source renderer draws a source. Uses only public items of rich and rich_plugin_api.