korken89 / smlang-rs

A State Machine Language DSL procedual macro for Rust
Apache License 2.0
202 stars 28 forks source link

Multiple state machines in one file #1

Closed thejpster closed 2 months ago

thejpster commented 5 years ago

Is it possible to create multiple different statemachines in one file? e.g.

statemachine! (VendProduct) {
    *State1 + Event1 [guard] / action1 = State2,
    State2 + Event2 [guard_fail] / action2 = State3,
}

statemachine! (HandleMoney) {
    *State1 + Event1 [guard] / action1 = State2,
    State2 + Event2 [guard_fail] / action2 = State3,
}

fn run() {
   let fsm1 = VendProduct::new();
   let fsm2 = HandleMoney::new();
}

Does putting them in a module act as a workaround?

korken89 commented 5 years ago

For now one need to put the macro in separate modules, but if you check the TODO in the readme - what do you think about the proposed syntex for setting name?

korken89 commented 4 years ago

For now, the recommended action is:


mod vend_product {
   statemachine! {
       *State1 + Event1 [guard] / action1 = State2,
       State2 + Event2 [guard_fail] / action2 = State3,
   }
}

mod handle_money {
   statemachine! {
       *State1 + Event1 [guard] / action1 = State2,
       State2 + Event2 [guard_fail] / action2 = State3,
   }
}

fn run() {
   let fsm1 = vend_product::StateMachine::new();
   let fsm2 = handle_money::StateMachine::new();
}
dzimmanck commented 2 years ago

I like the syntax proposed by @thejpster. I could design the macro so that, if a name is provided, it generates the state machine code in a dedicated mod{} the the name provides but if a name is not provided, it will generate the code in the same the existing file namespace so it shouldn't break anything.

Unless there are any objections, I will pursue integrating this into a 0.5.0 release candidate along with some other features.

ryan-summers commented 2 months ago

I believe this is fixed by https://github.com/korken89/smlang-rs/pull/62, where you can provide a name parameter in the DSL. This gives all the traits/states/events a prefix to allow them all to exist in the same file/mod (as long as they have unique names)