A lightweight MoonBit library for building directed graphs and exporting to Graphviz DOT or Mermaid flowcharts.
moon add bobzhang/dot///|
test "dot: quickstart" {
let builder = @flowgraph.DotBuilder::new()
builder
..add_node(id="a", label="Start")
..add_node(id="b", label="End")
.add_edge(src="a", dst="b", label="→")
inspect(
builder.to_dot(),
content=(
#|digraph Marshal {
#| rankdir=LR;
#| node [shape=box, style=rounded];
#|
#| a [label="Start"];
#| b [label="End"];
#|
#| a -> b [label="→"];
#|}
#|
),
)
}///|
test "quickstart-mermaid" (it : @test.Test) {
let builder = @flowgraph.DotBuilder::new()
builder
..add_node(id="a", label="Start")
..add_node(id="b", label="End")
.add_edge(src="a", dst="b", label="→")
let got = builder.to_mermaid()
inspect(
got,
content=(
#|flowchart LR
#|%% graph: Marshal
#|
#| a["Start"]
#| b["End"]
#|
#| a -->|→| b
#|
),
)
it.write(got)
it.snapshot(filename="\{it.name()}.mmd")
}///|
test "styled graph example" {
let builder = @flowgraph.DotBuilder::with_config(
graph_name="Workflow",
rankdir="TB", // Top to Bottom
)
builder
..add_node(id="start", label="Start", shape="circle", color="green")
..add_node(id="process", label="Process", shape="box", color="lightblue")
..add_node(id="end", label="End", shape="doublecircle", color="red")
..add_edge(
src="start",
dst="process",
label="begin",
style="solid",
color="black",
)
.add_edge(
src="process",
dst="end",
label="complete",
style="dashed",
color="blue",
)
}///|
test "bidirectional example" {
let builder = @flowgraph.DotBuilder::new()
builder
..add_node(id="client", label="Client")
..add_node(id="server", label="Server")
.add_bidirectional_edge(node1="client", node2="server", label="HTTP")
}///|
test "subgraph example" {
let builder = @flowgraph.DotBuilder::new()
builder
..add_node(id="web1", label="Web Server 1")
..add_node(id="web2", label="Web Server 2")
..add_node(id="app1", label="App Server 1")
..add_node(id="app2", label="App Server 2")
..add_subgraph(name="frontend", label="Frontend Tier", nodes=["web1", "web2"])
..add_subgraph(name="backend", label="Backend Tier", nodes=["app1", "app2"])
..add_edge(src="web1", dst="app1", label="")
.add_edge(src="web2", dst="app2", label="")
}///|
test "auto-generated ids" {
let builder = @flowgraph.DotBuilder::new()
let id1 = builder.fresh_id() // "n0"
let id2 = builder.fresh_id() // "n1"
let id3 = builder.fresh_id() // "n2"
builder
..add_node(id=id1, label="First")
..add_node(id=id2, label="Second")
.add_node(id=id3, label="Third")
}type DotBuilderfn DotBuilder::add_bidirectional_edge(self : DotBuilder, node1~ : String, node2~ : String, label~ : String) -> Unitfn DotBuilder::add_edge(self : DotBuilder, src~ : String, dst~ : String, label~ : String, style? : String, color? : String) -> Unitfn DotBuilder::add_node(self : DotBuilder, id~ : String, label~ : String, shape? : String, color? : String) -> Unitfn DotBuilder::add_subgraph(self : DotBuilder, name~ : String, label~ : String, nodes~ : Array[String]) -> UnitA lightweight MoonBit library for building directed graphs and exporting to Graphviz DOT or Mermaid flowcharts.