mermaid_markdown_api/syntax/mod.rs
1use crate::objects::connection::Connection;
2use crate::objects::node::Node;
3use crate::syntax::flow_chart::ObjectConfig;
4use enum_as_inner::EnumAsInner;
5// TODO:
6pub mod flow_chart;
7
8/// An Enum representing the possible options for the direction of flow for the diagram.
9#[derive(AsRefStr)]
10pub enum FlowDirection {
11 /// top to bottom
12 TB,
13 /// top-down (same as top to bottom)
14 TD,
15 /// bottom to top
16 BT,
17 /// right to left
18 BL,
19 /// left to right
20 LR,
21}
22
23/// An enum representation of the available syntax's.
24#[derive(EnumProperty, EnumAsInner, Debug)]
25pub enum SyntaxConfigFile<'a> {
26 FlowChart(ObjectConfig<'a>),
27}
28
29pub trait CoreSyntaxFunctions {
30 /// Returns a `FlowChart` struct to allow you to build the necessary markdown text.
31 ///
32 /// # Arguments
33 ///
34 /// * `direction` - The enum representation of the flow direction of the diagram
35 ///
36 /// # Examples
37 ///
38 /// ```
39 /// use mermaid_markdown_api::syntax::flow_chart::FlowChart;
40 /// use mermaid_markdown_api::syntax::{CoreSyntaxFunctions, FlowDirection};
41 ///
42 /// let mut flow_chart = FlowChart::new(FlowDirection::TD);
43 /// ```
44 fn new(direction: FlowDirection) -> Self;
45
46 /// Creates a [Mermaid.js Node](https://mermaid-js.github.io/mermaid/#/flowchart?id=a-node-default) with the supplied [configuration](NodeConfig) & appends it to the current data of the flow chart struct (i.e. `self.data`).
47 ///
48 /// # Arguments
49 /// * `node_config` - [NodeConfig]
50 ///
51 /// # Examples
52 ///
53 /// ```
54 /// use mermaid_markdown_api::syntax::flow_chart::{FlowChart, NodeConfig, Shape};
55 /// use mermaid_markdown_api::syntax::{CoreSyntaxFunctions, FlowDirection, SyntaxConfigFile};
56 ///
57 /// let mut flow_chart = FlowChart::new(FlowDirection::TD);
58 ///
59 /// let node_config = SyntaxConfigFile::FlowChart(ConfigFile::NodeConfig(NodeConfig {
60 /// id: "A",
61 /// class: None,
62 /// shape: Shape::Circle,
63 /// inner_text: "inner text",
64 /// }));
65 ///
66 /// flow_chart.add_node(node_config);
67 /// ```
68 fn add_node(
69 &mut self,
70 node_config: SyntaxConfigFile,
71 );
72
73 /// Creates a [Mermaid.js Connection](https://mermaid-js.github.io/mermaid/#/flowchart?id=links-between-nodes) with the supplied [configuration] & appends it to the current data of the flow chart struct (i.e. `self.data`).
74 ///
75 /// # Arguments
76 /// * `connection_config` - [ConnectionConfig]
77 ///
78 /// # Examples
79 ///
80 /// ```
81 /// use mermaid_markdown_api::syntax::flow_chart::{ArrowDirection, ArrowType, ConnectionConfig, FlowChart, LineType, NodeConfig, Shape};
82 /// use mermaid_markdown_api::syntax::{CoreSyntaxFunctions, FlowDirection, SyntaxConfigFile};
83 /// let mut flow_chart = FlowChart::new(FlowDirection::TD);
84 ///
85 /// let node_config = SyntaxConfigFile::FlowChart(ConfigFile::NodeConfig(NodeConfig {
86 /// id: "A",
87 /// class: None,
88 /// shape: Shape::Circle,
89 /// inner_text: "inner text",
90 /// }));
91 ///
92 /// let connection_config = SyntaxConfigFile::FlowChart(ConfigFile::ConnectionConfig(ConnectionConfig {
93 /// line_type: LineType::Dashed,
94 /// arrow_type: ArrowType::Standard,
95 /// arrow_direction: ArrowDirection::Right,
96 /// extra_length_num: None,
97 /// }));
98 ///
99 /// flow_chart.add_node(node_config);
100 /// flow_chart.add_connection(connection_config);
101 /// ```
102 fn add_connection(
103 &mut self,
104 connection_config: SyntaxConfigFile,
105 );
106
107 /// Appends a linebreak & the preceding whitespace to the current data of the flow chart struct (i.e. `self.data`).
108 ///
109 /// # Arguments
110 ///
111 /// * `num_of_indents` - Optional number of indents to insert once the new line is added (default it 1)
112 ///
113 /// # Examples
114 ///
115 /// ```
116 /// use mermaid_markdown_api::syntax::flow_chart::FlowChart;
117 /// use mermaid_markdown_api::syntax::{CoreSyntaxFunctions, FlowDirection};
118 /// let mut flow_chart = FlowChart::new(FlowDirection::TD);
119 ///
120 /// flow_chart.add_linebreak(None);
121 /// ```
122 fn add_linebreak(
123 &mut self,
124 num_of_indents: Option<u8>,
125 );
126
127 /// This method creates a [NodeConfig] referencing data from a supplied [Node].
128 ///
129 /// # Arguments
130 ///
131 /// * `node` - The [Node] that is going to determine the configuration
132 ///
133 /// # Examples
134 ///
135 /// ```
136 /// use mermaid_markdown_api::objects::node::{ActionType, Node, ScopeType};
137 /// use mermaid_markdown_api::syntax::{CoreSyntaxFunctions, FlowDirection};
138 /// use mermaid_markdown_api::syntax::flow_chart::FlowChart;
139 /// let mut flow_chart = FlowChart::new(FlowDirection::TD);
140 ///
141 /// let node = Node {
142 /// name: "function_a".to_string(),
143 /// scope: ScopeType::Public,
144 /// action: ActionType::Mutation,
145 /// connections: vec![],
146 /// };
147 ///
148 /// let node_config = flow_chart.build_node_config(&node);
149 /// ```
150 fn build_node_config<'a>(
151 &self,
152 node: &'a Node,
153 id: Option<&'a str>,
154 ) -> SyntaxConfigFile<'a>;
155
156 /// This method creates a [ConnectionConfig] referencing data from a supplied [Connection].
157 ///
158 /// # Arguments
159 ///
160 /// * `connection` - The [Connection] that is going to determine the configuration
161 ///
162 /// # Examples
163 ///
164 /// ```
165 /// use mermaid_markdown_api::objects::connection::{Connection, ConnectionType};
166 /// use mermaid_markdown_api::objects::node::{ActionType, Node, ScopeType};
167 /// use mermaid_markdown_api::syntax::{CoreSyntaxFunctions, FlowDirection};
168 /// use mermaid_markdown_api::syntax::flow_chart::FlowChart;
169 /// let mut flow_chart = FlowChart::new(FlowDirection::TD);
170 ///
171 /// let connection = Connection {
172 /// connection_type: ConnectionType::DirectConnection,
173 /// node: Node {
174 /// name: "function_a".to_string(),
175 /// scope: ScopeType::Public,
176 /// action: ActionType::Mutation,
177 /// connections: vec![],
178 /// }
179 /// };
180 ///
181 /// let connection_config = flow_chart.build_connection_config(&connection);
182 /// ```
183 fn build_connection_config<'a>(
184 &self,
185 connection: &'a Connection,
186 extra_length_num: Option<u8>,
187 ) -> SyntaxConfigFile<'a>;
188
189 /// This method returns a clone of `self.data`.
190 ///
191 /// # Examples
192 ///
193 /// ```
194 /// use mermaid_markdown_api::objects::node::{ActionType, Node, ScopeType};
195 /// use mermaid_markdown_api::syntax::{CoreSyntaxFunctions, FlowDirection};
196 /// use mermaid_markdown_api::syntax::flow_chart::FlowChart;
197 /// let mut flow_chart = FlowChart::new(FlowDirection::TD);
198 ///
199 /// let node = Node {
200 /// name: "function_a".to_string(),
201 /// scope: ScopeType::Public,
202 /// action: ActionType::Mutation,
203 /// connections: vec![],
204 /// };
205 ///
206 /// let node_config = flow_chart.build_node_config(&node);
207 ///
208 /// flow_chart.add_node(node_config);
209 ///
210 /// let markdown_string = flow_chart.return_schema();
211 /// ```
212 fn return_schema(&self) -> String;
213}