Skip to content

Commit 2416648

Browse files
authored
feat: add introspection and visualization support with Mermaid/JSON export (#5)
1 parent 5c5a2bc commit 2416648

File tree

19 files changed

+609
-3
lines changed

19 files changed

+609
-3
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ members = [
33
"state-machines",
44
"state-machines-core",
55
"state-machines-macro",
6+
"state-machines-cli",
67
"examples/no_std_flight",
78
"examples/basic_transitions",
89
"examples/guards_and_validation",

examples/async_patterns/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ publish = false
77
[dependencies]
88
state-machines = { path = "../../state-machines" }
99
pollster = "0.4"
10+
11+
[features]
12+
default = []
13+
inspect = ["state-machines/inspect"]

examples/basic_transitions/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@ edition = "2024"
55
publish = false
66

77
[dependencies]
8-
state-machines = { path = "../../state-machines" }
8+
state-machines = { path = "../../state-machines", features = ["std"] }
9+
10+
[features]
11+
default = ["inspect"]
12+
inspect = ["state-machines/inspect"]

examples/basic_transitions/src/main.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,21 @@ state_machine! {
127127
fn main() {
128128
println!("=== Airlock Pressure Cycle Demo ===\n");
129129

130+
// Demonstrate introspection capabilities
131+
#[cfg(feature = "inspect")]
132+
{
133+
let schema = Airlock::<(), Pressurized>::schema();
134+
println!("=== Machine Introspection ===\n");
135+
println!("Machine: {}", schema.name);
136+
println!("Initial State: {}", schema.initial);
137+
println!("States: {:?}", schema.states);
138+
println!("\nMermaid Diagram:");
139+
println!("{}", schema.to_mermaid());
140+
println!("JSON Schema:");
141+
println!("{}", schema.to_json_pretty());
142+
println!();
143+
}
144+
130145
// Create new airlock in Pressurized state
131146
// Type: Airlock<(), Pressurized>
132147
let airlock = Airlock::new(());

examples/callbacks_lifecycle/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ publish = false
66

77
[dependencies]
88
state-machines = { path = "../../state-machines" }
9+
10+
[features]
11+
default = []
12+
inspect = ["state-machines/inspect"]

examples/dynamic_dispatch_when/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ publish = false
66

77
[dependencies]
88
state-machines = { path = "../../state-machines", features = ["dynamic"] }
9+
10+
[features]
11+
default = []
12+
inspect = ["state-machines/inspect"]

examples/guards_and_validation/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ publish = false
66

77
[dependencies]
88
state-machines = { path = "../../state-machines" }
9+
10+
[features]
11+
default = []
12+
inspect = ["state-machines/inspect"]

examples/hierarchical_thinking/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@ edition = "2024"
55
publish = false
66

77
[dependencies]
8-
state-machines = { path = "../../state-machines" }
8+
state-machines = { path = "../../state-machines", features = ["std"] }
9+
10+
[features]
11+
default = ["inspect"]
12+
inspect = ["state-machines/inspect"]

examples/hierarchical_thinking/src/main.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,26 @@ fn main() {
265265
println!("=== Life Support System Hierarchy Demo ===\n");
266266
println!("This example demonstrates hierarchical states (superstates).\n");
267267

268+
// Demonstrate introspection with hierarchical states
269+
#[cfg(feature = "inspect")]
270+
{
271+
let schema = LifeSupportSystem::<(), Offline>::schema();
272+
println!("=== Machine Introspection (with Superstates) ===\n");
273+
println!("Machine: {}", schema.name);
274+
println!("States: {:?}", schema.states);
275+
if !schema.superstates.is_empty() {
276+
println!("Superstates:");
277+
for ss in &schema.superstates {
278+
println!(
279+
" {} (initial: {}, descendants: {:?})",
280+
ss.name, ss.initial, ss.descendants
281+
);
282+
}
283+
}
284+
println!("\nMermaid Diagram:");
285+
println!("{}", schema.to_mermaid());
286+
}
287+
268288
// Scenario 1: Activating life support and cycling through modes
269289
println!("--- Scenario 1: Normal Operations ---");
270290

examples/no_std_flight/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ publish = false
66

77
[dependencies]
88
state-machines = { path = "../../state-machines", default-features = false }
9+
10+
[features]
11+
default = []
12+
inspect = ["state-machines/inspect"]

0 commit comments

Comments
 (0)