dotspace 0.3.2

Explore your Graphviz dot files in interactive 3D space
Documentation
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#![allow(clippy::cast_possible_truncation)] // Stack depth won't exceed u32::MAX

use crate::types::{GraphData, NodeInfo, NodeType};
use petgraph::graph::DiGraph;
use std::collections::HashMap;

#[must_use]
pub fn parse_dot_file(content: &str) -> GraphData {
    let mut graph = DiGraph::new();
    let mut node_map = HashMap::new();
    let mut node_attributes = HashMap::new();

    // Check if this is a nested subgraph format (no edges)
    let has_edges = content.contains("->");

    if !has_edges && content.contains("subgraph") {
        // Parse nested subgraph format
        return parse_nested_subgraphs(content);
    }

    // Parse nodes with attributes (original edge-based format)
    let lines: Vec<&str> = content.lines().collect();
    for line in &lines {
        let trimmed = line.trim();

        // Parse node definitions with attributes
        if trimmed.contains('[') && trimmed.contains(']') && !trimmed.contains("->") {
            if let Some(node_end) = trimmed.find('[') {
                let node_id = trimmed[..node_end].trim().trim_matches('"');

                // Extract attributes
                let attrs_str = &trimmed[node_end + 1..trimmed.rfind(']').unwrap_or(trimmed.len())];
                let mut node_type = NodeType::Default;
                let mut level = 0u32;

                // Parse attributes
                for attr in attrs_str.split(',') {
                    let parts: Vec<&str> = attr.split('=').collect();
                    if parts.len() == 2 {
                        let key = parts[0].trim();
                        let value = parts[1].trim().trim_matches('"');

                        match key {
                            "type" => node_type = NodeType::parse(value),
                            "level" => level = value.parse().unwrap_or(0),
                            _ => {}
                        }
                    }
                }

                node_attributes.insert(node_id.to_string(), (node_type, level));
            }
        }
    }

    // Parse edges and create nodes
    for line in &lines {
        let trimmed = line.trim();
        if trimmed.contains("->") {
            // Remove comments
            let edge_line = trimmed
                .find("//")
                .map_or(trimmed, |comment_pos| &trimmed[..comment_pos]);

            let parts: Vec<&str> = edge_line.split("->").collect();
            if parts.len() >= 2 {
                let from = parts[0].trim().trim_matches('"');
                let to_part = parts[1].trim();
                let to = to_part.find('[').map_or_else(
                    || to_part.trim_end_matches(';').trim().trim_matches('"'),
                    |bracket_pos| {
                        to_part[..bracket_pos]
                            .trim()
                            .trim_matches('"')
                            .trim_end_matches(';')
                    },
                );

                // Ensure nodes exist
                let from_idx = *node_map.entry(from.to_string()).or_insert_with(|| {
                    let (node_type, level) = node_attributes
                        .get(from)
                        .cloned()
                        .unwrap_or((NodeType::Default, 0));
                    graph.add_node(NodeInfo {
                        name: from.to_string(),
                        node_type,
                        level,
                    })
                });

                let to_idx = *node_map.entry(to.to_string()).or_insert_with(|| {
                    let (node_type, level) = node_attributes
                        .get(to)
                        .cloned()
                        .unwrap_or((NodeType::Default, 0));
                    graph.add_node(NodeInfo {
                        name: to.to_string(),
                        node_type,
                        level,
                    })
                });

                graph.add_edge(from_idx, to_idx, ());
            }
        }
    }

    GraphData { graph, node_map }
}

fn parse_nested_subgraphs(content: &str) -> GraphData {
    let mut graph = DiGraph::new();
    let mut node_map = HashMap::new();
    let mut stack: Vec<(String, Option<petgraph::graph::NodeIndex>)> = Vec::new();

    let lines: Vec<&str> = content.lines().collect();
    let mut i = 0;

    while i < lines.len() {
        let line = lines[i].trim();

        // Parse subgraph clusters
        if line.starts_with("subgraph cluster_") {
            let cluster_name = line
                .strip_prefix("subgraph cluster_")
                .unwrap_or("")
                .trim_end_matches('{')
                .trim();

            // Look for the label in the next few lines
            let mut label = cluster_name.to_string();
            let level = stack.len() as u32;

            for next_line in lines.iter().skip(i + 1).take(9) {
                let next_line = next_line.trim();
                if next_line.starts_with("Label=") || next_line.starts_with("label=") {
                    label = next_line
                        .split('=')
                        .nth(1)
                        .unwrap_or(cluster_name)
                        .trim_matches('"')
                        .trim_matches(';')
                        .to_string();

                    // Extract meaningful name from label (after the colon if present)
                    if let Some(colon_pos) = label.find(':') {
                        label = label[colon_pos + 1..].trim().to_string();
                    }
                    break;
                }
            }

            // Determine node type based on label content
            let node_type = if label.to_lowercase().contains("tenant")
                || label.to_lowercase().contains("organization")
            {
                NodeType::Organization
            } else if label.to_lowercase().contains("contact center") {
                NodeType::LineOfBusiness
            } else if label.to_lowercase().contains("site") {
                NodeType::Site
            } else {
                NodeType::Default
            };

            // Create node for this cluster
            let node_idx = graph.add_node(NodeInfo {
                name: label.clone(),
                node_type,
                level,
            });

            node_map.insert(label, node_idx);

            // Connect to parent if exists
            if let Some((_, Some(parent_idx))) = stack.last() {
                graph.add_edge(*parent_idx, node_idx, ());
            }

            stack.push((cluster_name.to_string(), Some(node_idx)));
        }
        // Parse standalone nodes (like "supervisor32")
        else if line.contains('[') && line.contains("label=") && !line.contains("->") {
            if let Some(node_end) = line.find('[') {
                let node_id = line[..node_end].trim().trim_matches('"');

                // Extract label from attributes
                let label = line
                    .find("label=")
                    .and_then(|label_start| {
                        let label_part = &line[label_start + 6..];
                        label_part.find('"').and_then(|label_end| {
                            label_part[label_end + 1..].find('"').map(|second_quote| {
                                label_part[label_end + 1..label_end + 1 + second_quote]
                                    .replace("\\n", " ")
                                    .trim()
                                    .to_string()
                            })
                        })
                    })
                    .unwrap_or_else(|| node_id.to_string());

                let level = stack.len() as u32;
                let node_type = if label.to_lowercase().contains("supervisor") {
                    NodeType::Team
                } else {
                    NodeType::User
                };

                let node_idx = graph.add_node(NodeInfo {
                    name: label.clone(),
                    node_type,
                    level,
                });

                node_map.insert(label, node_idx);

                // Connect to parent if exists
                if let Some((_, Some(parent_idx))) = stack.last() {
                    graph.add_edge(*parent_idx, node_idx, ());
                }
            }
        }
        // Handle closing braces
        else if line.contains('}') && !stack.is_empty() {
            stack.pop();
        }

        i += 1;
    }

    GraphData { graph, node_map }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_simple_graph() {
        let dot = r"
            digraph {
                A -> B;
                B -> C;
            }
        ";
        let graph_data = parse_dot_file(dot);
        assert_eq!(graph_data.graph.node_count(), 3);
        assert_eq!(graph_data.graph.edge_count(), 2);
    }

    #[test]
    fn test_node_type_parsing() {
        let test_cases = vec![
            ("organization", NodeType::Organization),
            ("org", NodeType::Organization),
            ("lob", NodeType::LineOfBusiness),
            ("lineofbusiness", NodeType::LineOfBusiness),
            ("line_of_business", NodeType::LineOfBusiness),
            ("site", NodeType::Site),
            ("team", NodeType::Team),
            ("user", NodeType::User),
            ("unknown", NodeType::Default),
        ];

        for (input, expected) in test_cases {
            assert_eq!(NodeType::parse(input), expected);
        }
    }

    #[test]
    fn test_parse_node_with_attributes() {
        let dot = r#"
            digraph {
                "Node1" [type="team", level="2"];
                "Node2" [type="user", level="1"];
                "Node1" -> "Node2";
            }
        "#;
        let graph_data = parse_dot_file(dot);

        let node1 = &graph_data.graph[graph_data.node_map["Node1"]];
        assert_eq!(node1.node_type, NodeType::Team);
        assert_eq!(node1.level, 2);
    }

    #[test]
    fn test_parse_edge_with_style() {
        let dot = r#"
            digraph {
                A -> B [style="dashed"];
                B -> C; // Comment
            }
        "#;
        let graph_data = parse_dot_file(dot);
        assert_eq!(graph_data.graph.edge_count(), 2);
    }

    #[test]
    fn test_quoted_node_names() {
        let dot = r#"
            digraph {
                "Node with spaces" -> "Another node";
                "Another node" -> SimpleNode;
            }
        "#;
        let graph_data = parse_dot_file(dot);
        assert_eq!(graph_data.graph.node_count(), 3);
        assert!(graph_data.node_map.contains_key("Node with spaces"));
    }

    #[test]
    fn test_parse_nodes_without_attributes() {
        let dot = r"
            digraph {
                NodeA;
                NodeB;
                NodeC;
                NodeA -> NodeB;
                NodeB -> NodeC;
            }
        ";
        let graph_data = parse_dot_file(dot);
        assert_eq!(graph_data.graph.node_count(), 3);

        for node in graph_data.graph.node_weights() {
            assert_eq!(node.node_type, NodeType::Default);
            assert_eq!(node.level, 0);
        }
    }

    #[test]
    fn test_nested_subgraph_parsing() {
        let dot = r#"
            digraph OrgHierarchy {
                subgraph cluster_tenant {
                    Label="Tenant: main_tenant";
                    subgraph cluster_org {
                        label="Organization: org1";
                        subgraph cluster_site {
                            label="Site: site1";
                            "user1" [label="User One"];
                        }
                    }
                }
            }
        "#;

        let graph_data = parse_dot_file(dot);

        // Should create 4 nodes: tenant, org, site, and user
        assert_eq!(graph_data.graph.node_count(), 4);
        // Should create 3 edges: tenant->org, org->site, site->user
        assert_eq!(graph_data.graph.edge_count(), 3);
    }

    #[test]
    fn test_complex_graph_parsing() {
        let dot = r#"
            digraph OrgChart {
                rankdir=TB;
                
                // Organization level
                "ACME Corp" [type="organization", level="3"];
                
                // Business units
                "Sales" [type="lob", level="2"];
                "Engineering" [type="lob", level="2"];
                
                // Sites
                "NYC Office" [type="site", level="1"];
                "SF Office" [type="site", level="1"];
                
                // Teams
                "Frontend Team" [type="team", level="1"];
                "Backend Team" [type="team", level="1"];
                
                // Connections
                "ACME Corp" -> "Sales";
                "ACME Corp" -> "Engineering";
                "Sales" -> "NYC Office";
                "Engineering" -> "SF Office";
                "Engineering" -> "Frontend Team";
                "Engineering" -> "Backend Team";
            }
        "#;

        let graph_data = parse_dot_file(dot);

        // Check node count
        assert_eq!(graph_data.graph.node_count(), 7);
        assert_eq!(graph_data.graph.edge_count(), 6);

        // Verify specific nodes
        let acme = &graph_data.graph[graph_data.node_map["ACME Corp"]];
        assert_eq!(acme.node_type, NodeType::Organization);
        assert_eq!(acme.level, 3);

        let frontend = &graph_data.graph[graph_data.node_map["Frontend Team"]];
        assert_eq!(frontend.node_type, NodeType::Team);
        assert_eq!(frontend.level, 1);
    }
}