Skip to main content

events_demo/
events_demo.rs

1use dotparser::{GraphEvent, dot, plantuml};
2
3fn main() {
4    println!("=== DOT Graph Events ===");
5    demo_dot_events();
6
7    println!("\n=== PlantUML Sequence Events ===");
8    demo_plantuml_events();
9}
10
11fn demo_dot_events() {
12    let dot_content = r#"
13        digraph G {
14            rankdir=LR;
15            
16            // Nodes with attributes
17            Start [type="initial", label="Start"];
18            Process [type="process", label="Data Processing"];
19            Decision [type="decision", label="Valid?"];
20            End [type="final", label="End"];
21            
22            // Edges
23            Start -> Process;
24            Process -> Decision;
25            Decision -> End [label="Yes"];
26            Decision -> Process [label="No"];
27        }
28    "#;
29
30    let events = dot::parse(dot_content);
31
32    for event in &events {
33        match event {
34            GraphEvent::SetLayout { layout_type, .. } => {
35                println!("Layout: {layout_type:?}");
36            }
37            GraphEvent::AddNode {
38                id,
39                label,
40                node_type,
41                properties,
42            } => {
43                println!("Node: {} ({})", id, label.as_deref().unwrap_or("no label"));
44                println!("  Type: {node_type:?}");
45                if let Some(pos) = &properties.position {
46                    println!("  Position: {pos:?}");
47                }
48            }
49            GraphEvent::AddEdge {
50                from, to, label, ..
51            } => {
52                println!("Edge: {from} -> {to}");
53                if let Some(lbl) = label {
54                    println!("  Label: {lbl}");
55                }
56            }
57            GraphEvent::BatchStart => println!("--- Batch Start ---"),
58            GraphEvent::BatchEnd => println!("--- Batch End ---"),
59            _ => {}
60        }
61    }
62}
63
64fn demo_plantuml_events() {
65    let plantuml_content = r#"
66        @startuml
67        actor User
68        participant "Web Server" as Web
69        database "Database" as DB
70        
71        User -> Web: HTTP Request
72        activate Web
73        Web -> DB: Query
74        activate DB
75        DB --> Web: Results
76        deactivate DB
77        Web --> User: HTTP Response
78        deactivate Web
79        @enduml
80    "#;
81
82    match plantuml::parse(plantuml_content) {
83        Ok(events) => {
84            for event in &events {
85                match event {
86                    GraphEvent::SetLayout { layout_type, .. } => {
87                        println!("Layout: {layout_type:?}");
88                    }
89                    GraphEvent::AddNode {
90                        id,
91                        label,
92                        node_type,
93                        properties,
94                    } => {
95                        println!(
96                            "Participant: {} ({})",
97                            id,
98                            label.as_deref().unwrap_or("no label")
99                        );
100                        println!("  Type: {node_type:?}");
101                        if let Some(pos) = &properties.position {
102                            println!("  Position: {pos:?}");
103                        }
104                    }
105                    GraphEvent::AddEdge {
106                        id,
107                        from,
108                        to,
109                        edge_type,
110                        label,
111                        ..
112                    } => {
113                        println!("Message {id}: {from} -> {to}");
114                        println!("  Type: {edge_type:?}");
115                        if let Some(lbl) = label {
116                            println!("  Text: {lbl}");
117                        }
118                    }
119                    GraphEvent::UpdateNode { id, properties, .. } => {
120                        println!("Update {}: {:?}", id, properties.custom);
121                    }
122                    GraphEvent::BatchStart => println!("--- Batch Start ---"),
123                    GraphEvent::BatchEnd => println!("--- Batch End ---"),
124                    _ => {}
125                }
126            }
127        }
128        Err(e) => eprintln!("Parse error: {e}"),
129    }
130}