Skip to main content

Crate mermaid_builder

Crate mermaid_builder 

Source
Expand description

§Mermaid Builder

CI Security Audit License: MIT Codecov Crates.io Docs.rs

Mermaid Builder is a Rust crate that provides a type-safe, builder-pattern-based API for generating Mermaid diagram syntax. It allows you to define diagrams programmatically in Rust and export them as strings that can be rendered by Mermaid tools.

The crate is no_std compatible, using the alloc crate for dynamic memory allocation.

§Examples

§Flowchart

Flowcharts represent workflows or processes using nodes (shapes) and edges (arrows).

For more details on the Rust implementation, check the documentation for FlowchartBuilder, FlowchartNodeBuilder, FlowchartEdgeBuilder, and Flowchart.

For more information about the Mermaid syntax, please refer to the Mermaid Flowchart documentation.

use mermaid_builder::prelude::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut builder = FlowchartBuilder::default();

    // Create nodes
    let node1 = builder
        .node(FlowchartNodeBuilder::default().label("Start")?)?;
    let node2 = builder
        .node(FlowchartNodeBuilder::default().label("End")?)?;

    // Create edge
    builder
        .edge(
            FlowchartEdgeBuilder::default()
                .source(node1)?
                .destination(node2)?
                .right_arrow_shape(ArrowShape::Normal)?
        )?;

    // Build the flowchart
    let flowchart = Flowchart::from(builder);

    // Print the mermaid syntax
    println!("{}", flowchart);

    let expected = r#"flowchart LR
  v0@{shape: rect, label: "Start"}
  v1@{shape: rect, label: "End"}
  v0 ---> v1
"#;
    assert_eq!(flowchart.to_string(), expected);
    Ok(())
}

Output:

flowchart LR
  v0@{shape: rect, label: "Start"}
  v1@{shape: rect, label: "End"}
  v0 ---> v1

§Class Diagram

Class diagrams describe the structure of a system by showing classes, their attributes, methods, and relationships.

For more details on the Rust implementation, check the documentation for ClassDiagramBuilder, ClassNodeBuilder, ClassEdgeBuilder, and ClassDiagram.

For more information about the Mermaid syntax, please refer to the Mermaid Class Diagram documentation.

use mermaid_builder::prelude::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut builder = ClassDiagramBuilder::default();

    // Create class nodes
    let animal = builder
        .node(ClassNodeBuilder::default().label("Animal")?)?;
    let dog = builder
        .node(ClassNodeBuilder::default().label("Dog")?)?;

    // Create inheritance edge
    builder
        .edge(
            ClassEdgeBuilder::default()
                .source(animal)?
                .destination(dog)?
                .right_arrow_shape(ArrowShape::Triangle)?
        )?;

    let class_diagram = ClassDiagram::from(builder);
    println!("{}", class_diagram);

    let expected = r#"---
config:
  class:
    hideEmptyMembersBox: "false"
---
classDiagram
  direction LR
  class v0["Animal"] {
  }
  class v1["Dog"] {
  }
  v0 --|> v1
"#;
    assert_eq!(class_diagram.to_string(), expected);
    Ok(())
}

Output:

---
config:
  class:
    hideEmptyMembersBox: "false"
---
classDiagram
  direction LR
  class v0["Animal"] {
  }
  class v1["Dog"] {
  }
  v0 --|> v1

§Entity Relationship Diagram

Entity Relationship (ER) diagrams model entities and the relationships between them. They are commonly used to design database schemas.

For more details on the Rust implementation, check the documentation for ERDiagramBuilder, ERNodeBuilder, EREdgeBuilder, and ERDiagram.

For more information about the Mermaid syntax, please refer to the Mermaid Entity Relationship Diagram documentation.

use mermaid_builder::prelude::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut builder = ERDiagramBuilder::default();

    let customer = builder
        .node(ERNodeBuilder::default().label("CUSTOMER")?)?;
    let order = builder
        .node(ERNodeBuilder::default().label("ORDER")?)?;

    // Create relationship
    builder
        .edge(EREdgeBuilder::one_or_more(customer, order))?;

    let er_diagram = ERDiagram::from(builder);
    println!("{}", er_diagram);

    let expected = r#"---
config:
  layout: dagre
  theme: default
  look: classic
---
erDiagram
  direction LR
  v0["CUSTOMER"]
  v1["ORDER"]
  v0 }|--|{ v1 : ""
"#;
    assert_eq!(er_diagram.to_string(), expected);
    Ok(())
}

Output:

---
config:
  layout: dagre
  theme: default
  look: classic
---
erDiagram
  direction LR
  v0["CUSTOMER"]
  v1["ORDER"]
  v0 }|--|{ v1 : ""

§Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

§License

This project is licensed under the MIT License.

Modules§

diagrams
Submodule defining properties and structures shared across different types of Mermaid diagrams.
prelude
Submodule providing common traits and types for Mermaid diagrams.
traits
Submodule defining traits for Mermaid diagrams and annexed objects.

Enums§

ConfigError
Enum representing errors related to configuration in Mermaid diagrams.
EdgeError
Enum representing errors related to edges in Mermaid diagrams.
Error
Enum representing the different types of errors that can occur in the Mermaid library.
NodeError
Enum representing errors related to nodes in Mermaid diagrams.
StyleClassError
Enum representing the different types of errors that can occur when creating or using style classes in Mermaid diagrams.