Skip to main content

parse

Function parse 

Source
pub fn parse(input: &str) -> Result<Vec<GraphEvent>, String>
Expand description

Parse a PlantUML sequence diagram and return events

Examples found in repository?
examples/plantuml_sequence.rs (line 21)
3fn main() {
4    let sequence_diagram = r#"
5@startuml
6actor User
7participant "Web Server" as Web
8database "Database" as DB
9
10User -> Web: HTTP Request
11activate Web
12Web -> DB: Query
13activate DB
14DB --> Web: Results
15deactivate DB
16Web --> User: HTTP Response
17deactivate Web
18@enduml
19"#;
20
21    match plantuml::parse(sequence_diagram) {
22        Ok(events) => {
23            println!("Parsed PlantUML sequence diagram as events:");
24
25            // Count participants and messages
26            let mut participants = Vec::new();
27            let mut messages = Vec::new();
28
29            for event in &events {
30                match event {
31                    GraphEvent::AddNode {
32                        id,
33                        label,
34                        node_type,
35                        ..
36                    } => {
37                        participants.push((id.clone(), label.clone(), node_type.clone()));
38                    }
39                    GraphEvent::AddEdge {
40                        edge_type: EdgeType::Message { .. },
41                        from,
42                        to,
43                        label,
44                        ..
45                    } => {
46                        messages.push((from.clone(), to.clone(), label.clone()));
47                    }
48                    _ => {}
49                }
50            }
51
52            println!("Participants: {}", participants.len());
53            for (id, label, node_type) in &participants {
54                println!(
55                    "  - {} ({:?}): {}",
56                    id,
57                    node_type,
58                    label.as_deref().unwrap_or("no label")
59                );
60            }
61
62            println!("\nMessages: {}", messages.len());
63            for (i, (from, to, label)) in messages.iter().enumerate() {
64                println!(
65                    "  {}. {} -> {}: {}",
66                    i + 1,
67                    from,
68                    to,
69                    label.as_deref().unwrap_or("no label")
70                );
71            }
72        }
73        Err(e) => {
74            eprintln!("Error parsing PlantUML: {e}");
75        }
76    }
77}
More examples
Hide additional examples
examples/events_demo.rs (line 82)
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}