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 andShapes, edges with labels,Strokes andHeads, flowing in aDirection. Build one in code with its chaining builder, or from parts as a parser does.draw: the layered (Sugiyama-style) layout, giving aDrawing: plain lines of text. Seelayoutfor 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 theDotrenderable. With thepluginfeature,plugin::DotPluginregisters adotfence renderer and source renderer; with thegraphvizfeature,graphviz::render_svgruns Graphviz’s owndotfor SVG.er: entity-relationship diagrams. AnErModelof entities, columns and keys, relationships and groups, built in code (a schema reader fills it), drawn by theErDiagramrenderable.
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: aGraphas 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
dotbinary, for SVG. - layout
- Lay out a
Graphin ranks and draw it with box-drawing characters. - plugin
DotPlugin: DOT through the plugin API, like rs-rich-mermaid’sMermaidPlugin. A fence renderer draws```dot(and```graphviz) blocks in Markdown, and adotsource renderer draws a source. Uses only public items ofrichandrich_plugin_api.