Skip to main content

basic_demo/
basic_demo.rs

1use yasm::*;
2
3// Define a simple door state machine
4mod door {
5    use yasm::*;
6
7    define_state_machine! {
8        name: DoorStateMachine,
9        states: { Closed, Open, Locked },
10        inputs: { OpenDoor, CloseDoor, Lock, Unlock },
11        initial: Closed,
12        transitions: {
13            Closed + OpenDoor => Open,
14            Open + CloseDoor => Closed,
15            Closed + Lock => Locked,
16            Locked + Unlock => Closed
17        }
18    }
19}
20
21// Define a more complex order processing state machine
22mod order {
23    use yasm::*;
24
25    define_state_machine! {
26        name: OrderStateMachine,
27        states: { Created, Paid, Shipped, Delivered, Cancelled },
28        inputs: { Pay, Ship, Deliver, Cancel, Refund },
29        initial: Created,
30        transitions: {
31            Created + Pay => Paid,
32            Created + Cancel => Cancelled,
33            Paid + Ship => Shipped,
34            Paid + Refund => Cancelled,
35            Shipped + Deliver => Delivered,
36            Shipped + Cancel => Cancelled
37        }
38    }
39}
40
41fn main() {
42    println!("=== YASM (Yet Another State Machine) Basic Demo ===\n");
43
44    // Demonstrate door state machine
45    demo_door_state_machine();
46
47    println!("\n{}\n", "=".repeat(50));
48
49    // Demonstrate order state machine
50    demo_order_state_machine();
51
52    println!("\n{}\n", "=".repeat(50));
53
54    // Demonstrate query functions
55    demo_query_functions();
56
57    println!("\n{}\n", "=".repeat(50));
58
59    // Demonstrate documentation generation
60    demo_documentation_generation();
61}
62
63fn demo_door_state_machine() {
64    println!("🚪 Door State Machine Demo");
65    println!("{}", "-".repeat(25));
66
67    let mut door_sm = StateMachineInstance::<door::DoorStateMachine>::new();
68    println!("Initial state: {:?}", door_sm.current_state());
69
70    // Demonstrate state transitions
71    println!("\nCurrent valid inputs: {:?}", door_sm.valid_inputs());
72
73    println!("\nTrying to open door...");
74    match door_sm.transition(door::Input::OpenDoor) {
75        Ok(_) => println!("✅ Success! Current state: {:?}", door_sm.current_state()),
76        Err(e) => println!("❌ Failed: {e}"),
77    }
78
79    println!("\nCurrent valid inputs: {:?}", door_sm.valid_inputs());
80
81    println!("\nTrying to lock door (should fail because door is open)...");
82    match door_sm.transition(door::Input::Lock) {
83        Ok(_) => println!("✅ Success! Current state: {:?}", door_sm.current_state()),
84        Err(e) => println!("❌ Failed: {e}"),
85    }
86
87    println!("\nClosing door...");
88    match door_sm.transition(door::Input::CloseDoor) {
89        Ok(_) => println!("✅ Success! Current state: {:?}", door_sm.current_state()),
90        Err(e) => println!("❌ Failed: {e}"),
91    }
92
93    println!("\nLocking door...");
94    match door_sm.transition(door::Input::Lock) {
95        Ok(_) => println!("✅ Success! Current state: {:?}", door_sm.current_state()),
96        Err(e) => println!("❌ Failed: {e}"),
97    }
98
99    println!("\nTransition history: {:?}", door_sm.history());
100}
101
102fn demo_order_state_machine() {
103    println!("📦 Order State Machine Demo");
104    println!("{}", "-".repeat(27));
105
106    let mut order_sm = StateMachineInstance::<order::OrderStateMachine>::new();
107    println!("Initial state: {:?}", order_sm.current_state());
108
109    // Normal workflow
110    println!("\n=== Normal Order Workflow ===");
111    let transitions = vec![
112        (order::Input::Pay, "Pay for order"),
113        (order::Input::Ship, "Ship order"),
114        (order::Input::Deliver, "Deliver order"),
115    ];
116
117    for (input, description) in transitions {
118        println!("\n{}: {:?} -> ", description, order_sm.current_state());
119        match order_sm.transition(input) {
120            Ok(_) => println!("✅ {:?}", order_sm.current_state()),
121            Err(e) => println!("❌ {e}"),
122        }
123    }
124
125    // Demonstrate cancellation workflow
126    println!("\n=== Order Cancellation Workflow ===");
127    let mut order2 = StateMachineInstance::<order::OrderStateMachine>::new();
128    println!("New order state: {:?}", order2.current_state());
129
130    println!("\nPaying for order...");
131    order2.transition(order::Input::Pay).unwrap();
132    println!("State: {:?}", order2.current_state());
133
134    println!("\nRequesting refund...");
135    match order2.transition(order::Input::Refund) {
136        Ok(_) => println!("✅ Refund successful! State: {:?}", order2.current_state()),
137        Err(e) => println!("❌ Refund failed: {e}"),
138    }
139}
140
141fn demo_query_functions() {
142    println!("🔍 Query Functions Demo");
143    println!("{}", "-".repeat(22));
144
145    // Door state machine queries
146    println!("Door state machine queries:");
147    println!(
148        "All states reachable from Closed: {:?}",
149        StateMachineQuery::<door::DoorStateMachine>::reachable_states(&door::State::Closed)
150    );
151
152    println!(
153        "All states that can reach Locked: {:?}",
154        StateMachineQuery::<door::DoorStateMachine>::states_leading_to(&door::State::Locked)
155    );
156
157    println!(
158        "Path exists from Open to Locked: {}",
159        StateMachineQuery::<door::DoorStateMachine>::has_path(
160            &door::State::Open,
161            &door::State::Locked
162        )
163    );
164
165    // Order state machine queries
166    println!("\nOrder state machine queries:");
167    println!(
168        "All states reachable from Created: {:?}",
169        StateMachineQuery::<order::OrderStateMachine>::reachable_states(&order::State::Created)
170    );
171
172    println!(
173        "All states that can reach Delivered: {:?}",
174        StateMachineQuery::<order::OrderStateMachine>::states_leading_to(&order::State::Delivered)
175    );
176}
177
178fn demo_documentation_generation() {
179    println!("📚 Documentation Generation Demo");
180    println!("{}", "-".repeat(32));
181
182    println!("Door state machine Mermaid diagram:");
183    println!(
184        "{}",
185        StateMachineDoc::<door::DoorStateMachine>::generate_mermaid()
186    );
187
188    println!("\nOrder state machine Mermaid diagram:");
189    println!(
190        "{}",
191        StateMachineDoc::<order::OrderStateMachine>::generate_mermaid()
192    );
193
194    println!("\nDoor state machine transition table:");
195    println!(
196        "{}",
197        StateMachineDoc::<door::DoorStateMachine>::generate_transition_table()
198    );
199}