Skip to content

A state machine library inspired by Ruby's Statesman gem

Notifications You must be signed in to change notification settings

ameykusurkar/statesman-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

statesman-rs

A state machine library that is inspired by and is intended to match the functionality of Ruby's statesman gem.

This is really just a proof-of-concept at the moment, and not intended for production use.

Usage

Usage for the example state machine defined in the original README would look like this.

This is a working example, see the full integration test!

First define the states and transitions:

use statesman::{machine::State, macros::State};

#[derive(Clone, Copy, State)]
enum OrderState {
    #[can_transition_to(CheckingOut)]
    #[can_transition_to(Cancelled)]
    Pending,

    #[can_transition_to(Purchased)]
    #[can_transition_to(Cancelled)]
    CheckingOut,

    #[can_transition_to(Shipped)]
    #[can_transition_to(Failed)]
    Purchased,

    #[can_transition_to(Refunded)]
    Shipped,

    Cancelled,
    Failed,
    Refunded,
}

Then use the state machine:

use statesman::{
    adapters::{InMemory, InMemoryTransition},
    machine::Machine,
    macros::InMemoryMachine,
};

#[derive(InMemoryMachine)]
struct Order {
    state_machine: InMemory<OrderState>,
}

fn main() {
  let mut order = Order { state_machine: InMemory::new(OrderState::Pending) };

  order.transition_to(OrderState::CheckingOut).unwrap();

  let result = order.transition_to(OrderState::Failed);

  assert_eq!(result.is_err(), true);
  assert_eq!(order.current_state(), OrderState::CheckingOut);

  order.transition_to(OrderState::Purchased).unwrap();

  assert_eq!(
      order.last_transition(),
      InMemoryTransition::new(OrderState::Purchased, 30),
  )
}

About

A state machine library inspired by Ruby's Statesman gem

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages