3个版本 (破坏性更新)

使用旧Rust 2015

0.3.0 2017年5月19日
0.2.0 2017年3月1日
0.1.0 2017年2月28日

#192 in #状态机

MIT许可证

9KB
215

fxsm

MIT licensed

一个非常简单的枚举状态机过程宏。

它是如何工作的

它简单地在您的枚举上生成适当的 StateMachine 特性的方法上的匹配条件。您可以通过 StateMachine 特性来使用状态机。

用法

  1. 在您的 Cargo.toml 中将 fxsm 添加为依赖项
[dependencies]
fxsm-derive = "0.2"
fxsm = "0.2"
  1. 创建状态机
#[macro_use]
extern crate fxsm_derive;
extern crate fxsm;


#[derive(Clone, Debug, FiniteStateMachine)]
enum CupState {
    #[state_transitions(Checkins, Aborted, Rescheduled)]
    Waiting,
    #[state_transitions(InProgress, Aborted, Rescheduled)]
    Checkins,
    #[state_transitions(Finished, Aborted, Rescheduled)]
    InProgress(String),

    // Finish-states
    Aborted(u64),
    Rescheduled { info: String },
    Finished,
}
  1. 使用它
fn main() {
    use fxsm::{ StateMachine };
    let mut fsm = CupState::Waiting;
    assert_eq!(CupState::finish_states(), 3);
    // must not be able to change to itself
    assert!(!fsm.can_change(CupState::Waiting));
    assert!(fsm.can_change(CupState::Checkins));
    assert!(!fsm.can_change(CupState::InProgress(String::default())));
    assert!(fsm.can_change(CupState::Aborted(0u64)));
    assert!(fsm.can_change(CupState::Rescheduled{info: String::default()}));
    assert!(!fsm.can_change(CupState::Finished));
    assert!(!fsm.at_finish_state());

    assert!(fsm.change(CupState::Checkins));
    assert!(!fsm.can_change(CupState::Waiting));
    assert!(fsm.can_change(CupState::Aborted(0u64)));
    assert!(fsm.can_change(CupState::Rescheduled{info: String::default()}));
    assert!(fsm.can_change(CupState::InProgress(String::default())));
    assert!(!fsm.can_change(CupState::Finished));
    assert!(!fsm.at_finish_state());

    // You still always able to change it without FSM rules:
    fsm = CupState::Finished;
    assert!(fsm.at_finish_state());
    assert!(CupState::is_finish_state(CupState::Finished));
    assert!(CupState::is_finish_state(CupState::Aborted(0u64)));
    assert!(CupState::is_finish_state(CupState::Rescheduled{ info: String::default()}));
    assert!(!CupState::is_finish_state(CupState::Waiting));
    assert!(!CupState::is_finish_state(CupState::Checkins));
    assert!(!CupState::is_finish_state(CupState::InProgress(String::default())));
}

更多和更新的示例在 示例目录 中。

许可证

本项目遵循 MIT许可证

依赖项

~1.5MB
~41K SLoC