Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Would be awesome to have an attribute that draws an ASCII picture of the resulting tree #11

Open
matthewgapp opened this issue Mar 10, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@matthewgapp
Copy link

matthewgapp commented Mar 10, 2023

Thinking something like

#[state_machine(visualize)]
impl MyStruct { ... } 

fn main() { 
  let mut state_machine = Blinky::default().uninitialized_state_machine().init();
  let ascii_visualization: String = state_machine.visualize();
  println!("{}", ascii_visualiztion);
}

Which would print something like

                          ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐             
                                    Top                       
                          └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘             
                                     │                        
                        ┌────────────┴────────────┐           
                        │                         │           
             ┌─────────────────────┐   ╔═════════════════════╗
             │      Blinking       │   ║     NotBlinking     ║
             │─────────────────────│   ╚═════════════════════╝
             │ counter: &'a usize  │                          
             └─────────────────────┘                          
                        │                                     
           ┌────────────┴────────────┐                        
           │                         │                        
╔═════════════════════╗   ╔═════════════════════╗             
║        LedOn        ║   ║        LedOff       ║             
║─────────────────────║   ║─────────────────────║             
║ counter: usize      ║   ║ counter: usize      ║             
╚═════════════════════╝   ╚═════════════════════╝

@matthewgapp matthewgapp changed the title Would be awesome to have a debug attribute that draws an ASCII picture of the resulting tree Would be awesome to have an attribute that draws an ASCII picture of the resulting tree Mar 10, 2023
@mdeloof
Copy link
Owner

mdeloof commented Mar 13, 2023

I definitely want to add some way to visualise the state machines, though I'm still thinking about the best way to go about it. The tree representation is a nice start and should be pretty easy to do, but it doesn't include the possible transitions in response to events. Because that logic is defined as Rust code it's almost impossible to derive that reliably, so I'm thinking of maybe adding a way to annotate your state machines to provide that information to the visualiser.

I've been considering something like this, though I'm still not completely happy with it.

#[state]
fn led_on(&mut self, event: &Event) -> Response<State> {
    match event {
        #[event(transition(led_off))]   
        Event::TimerElapsed => {
            self.led = false;
            Transition(State::led_off())
        }
        #[event(transition(not_blinking))]
        Event::ButtonPressed => Transition(State::not_blinking())
    }
}

So yeah, maybe I'll just start with tree and go from there.

@mdeloof mdeloof added the enhancement New feature or request label Mar 13, 2023
@sadilekivan
Copy link

I'd love this feature too. I have been trying multiple state machine crates to enforce some safety between transitions with not too much boilerplate. typestate-rs crate has the ability to generate such flowcharts from the code. It uses macros to define which transitions are allowed with zero sized types and traits.

I find that statig is much more concise with defining which transitions occur together with any conditions or events that make them happen inside the function body. There could be something to define the transitions at compile time, but that adds complexity to the nice approach you have.

@v-morlock
Copy link

To solve the „retrieve transitions from code problem“, wouldn’t one solution be to add the possible target states for a certain state within the states macro above the function signature? I think that would be a bit more brief than adding a macro to each returned value.

One could even generate an Enum that has to be returned from the state instead of the „States“ enum thats containing all states. This of course wouldn’t prevent transitions from never happen but it would make sure that the generated graph contains all possible transitions. Something like:

#[state(target_states=[StateB, StateC])]
fn state_a() -> Response<StateAOut> {}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants