1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//! Submodule defining the `DiagramBuilder` trait for Mermaid diagrams.
use alloc::rc::Rc;
use core::fmt::Display;
use crate::{
shared::{StyleClass, StyleClassBuilder},
traits::{Configuration, ConfigurationBuilder, Diagram, Edge, EdgeBuilder, Node, NodeBuilder},
};
/// Trait defining the builder for Mermaid diagrams.
pub trait DiagramBuilder: Default
where
Self::Diagram: From<Self>,
{
/// Type of the diagram that this builder constructs.
type Diagram: Diagram<
Builder = Self,
Node = Self::Node,
Edge = Self::Edge,
Configuration = Self::Configuration,
>;
/// Type of the node used in the diagram.
type Node: Node<Builder = Self::NodeBuilder> + Display;
/// The type of node builder used to create nodes in the diagram.
type NodeBuilder: NodeBuilder<Node = Self::Node>;
/// Type of edge used in the diagram.
type Edge: Edge<Node = Self::Node> + Display;
/// The type of edge builder used to create edges in the diagram.
type EdgeBuilder: EdgeBuilder<Edge = Self::Edge>;
/// The configuration type for the diagram.
type Configuration: Configuration + Display;
/// The configuration builder type for the diagram.
type ConfigurationBuilder: ConfigurationBuilder<Configuration = Self::Configuration>;
/// The error type for the diagram builder.
type Error: core::error::Error + Display;
/// Sets the configuration for the diagram being built.
///
/// # Arguments
///
/// * `configuration` - The configuration builder to use for constructing
/// the diagram's configuration.
///
/// # Errors
///
/// * If the configuration builder is incomplete or invalid.
fn configuration(self, configuration: Self::ConfigurationBuilder) -> Result<Self, Self::Error>;
/// Adds a style class to the diagram being built.
///
/// # Arguments
///
/// * `style_class` - The style class to add to the diagram.
///
/// # Errors
///
/// * If there are conflicting style class names.
/// * If the style class builder is incomplete or invalid.
fn style_class(
&mut self,
style_class: StyleClassBuilder,
) -> Result<Rc<StyleClass>, Self::Error>;
#[must_use]
/// Returns the number of nodes currently in the diagram.
fn number_of_nodes(&self) -> usize;
#[must_use]
/// Returns the number of edges currently in the diagram.
fn number_of_edges(&self) -> usize;
/// Builds and adds a node to the diagram being built.
///
/// # Arguments
///
/// * `node` - The node builder to use for constructing the node.
///
/// # Errors
///
/// * If the node already exists in the diagram.
/// * If the node cannot be built due to missing attributes or other issues.
fn node(&mut self, node: Self::NodeBuilder) -> Result<Rc<Self::Node>, Self::Error>;
/// Iterates over the nodes in the diagram being built.
fn nodes(&self) -> impl Iterator<Item = &Rc<Self::Node>> + '_;
/// Returns a reference to the requested node by ID if it exists.
fn get_node_by_id(&self, id: u64) -> Option<Rc<Self::Node>>;
/// Returns a reference to the request style class by name if it exists.
fn get_style_class_by_name(&self, name: &str) -> Option<Rc<StyleClass>>;
/// Builds and adds an edge to the diagram being built.
///
/// # Arguments
///
/// * `edge` - The edge builder to use for constructing the edge.
///
/// # Errors
///
/// * If the source or destination nodes cannot be found in the diagram.
/// * If the edge cannot be built due to missing attributes or other issues.
fn edge(&mut self, edge: Self::EdgeBuilder) -> Result<Rc<Self::Edge>, Self::Error>;
}