korken89 / smlang-rs

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

Validating consistent call signatures for actions and guards #37

Closed ryan-summers closed 2 years ago

ryan-summers commented 2 years ago

This PR fixes #36 by flagging attempts to use actions and guards with inconsistent input states, events, or output states as compiler errors to the user.

$ cat examples/reuse_action.rs
//! Reuse the same aciton more than once
//!
//! This example shows how to use the same action in multiple transitions.

#![deny(missing_docs)]

use smlang::statemachine;

statemachine! {
    transitions: {
        *Init + Startup [guard] / action = Idle(u32),
        Idle(u32) + Reset [guard] = Init,
    }
}

/// Action will increment our context
pub struct Context(u32);

impl StateMachineContext for Context {
    fn guard(&mut self) -> Result<(), ()> {
        Ok(())
    }
    fn action(&mut self) -> u32 {
        self.0
    }
}

fn main() {
    let mut sm = StateMachine::new(Context(0));
}
$ cargo build --example reuse_action
   Compiling smlang v0.5.0 (C:\Users\rsummers\Documents\repositories\quartiq\smlang-rs)
error: Guard `guard` can only be reused when all input states and events have the same data
  --> examples\reuse_action.rs:9:1
   |
9  | / statemachine! {
10 | |     transitions: {
11 | |         *Init + Startup [guard] / action = Idle(u32),
12 | |         Idle(u32) + Reset [guard] = Init,
13 | |     }
14 | | }
   | |_^
   |
   = note: this error originates in the macro `statemachine` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0433]: failed to resolve: use of undeclared type `StateMachine`
  --> examples\reuse_action.rs:29:18
   |
29 |     let mut sm = StateMachine::new(Context(0));
   |                  ^^^^^^^^^^^^ use of undeclared type `StateMachine`

error[E0405]: cannot find trait `StateMachineContext` in this scope
  --> examples\reuse_action.rs:19:6
   |
19 | impl StateMachineContext for Context {
   |      ^^^^^^^^^^^^^^^^^^^ not found in this scope

Some errors have detailed explanations: E0405, E0433.
For more information about an error, try `rustc --explain E0405`.
error: could not compile `smlang` due to 3 previous errors
ryan-summers commented 2 years ago

Should all be implemented now! Didn't know about compile-fail tests - that's definitely a useful feature here

dzimmanck commented 2 years ago

@korken89, I don't think I can command the bors bot. Did you want bors to do the final checks and merge?