statemachine_rs/
lib.rs

1//! A zero dependency crate to implement state machine.
2//!
3//! ## Current Version
4//! 0.1.0
5//!
6//! ## Usage
7//! Let's have a look at the following simple example. This example shows the state machine
8//! can transit its number (it called `current_state` in this machine)
9//! from given string ("next") and then, it produces outputs.
10//!
11//! ```rust
12//! use statemachine_rs::machine::{
13//!     builder::BasicStateMachineBuilder, builder::StateMachineBuilder, StateMachine,
14//! };
15//!
16//! let sm = BasicStateMachineBuilder::start()
17//!     .initial_state(1)
18//!     .transition(|state, input| match (state, input) {
19//!         (1, "next") => 2,
20//!         (2, "next") => 3,
21//!         _ => unreachable!(),
22//!     })
23//!     .build()
24//!     .unwrap();
25//!
26//! assert_eq!(1, sm.current_state());
27//! sm.consume("next");
28//! assert_eq!(2, sm.current_state());
29//! ```
30//!
31//! You can assemble your state machine by using `statemachine_rs::machine::builder::StateMachineBUilder`.
32//! `StateMachineBuilder::initial_state()` initializes the initial state of its machine.
33//! `StateMachineBuilder::transition()` defines the transition model.
34//!
35//! Of cource we can use `enum`s for representing states and inputs. Let's have a look at another example.
36//!
37//! The following example describes if you press the button, the state turns to be `On`. Otherwise, `Off`.
38//!
39//! ```rust
40//! use statemachine_rs::machine::{
41//!     builder::BasicStateMachineBuilder, builder::StateMachineBuilder, StateMachine,
42//! };
43//!
44//! #[derive(Clone, Debug, PartialEq)]
45//! enum ButtonState {
46//!     On,
47//!     Off,
48//! }
49//!
50//! enum Input {
51//!     Press,
52//! }
53//!
54//! let sm = BasicStateMachineBuilder::start()
55//!     .initial_state(ButtonState::Off)
56//!     .transition(|state, input| match (state, input) {
57//!         (ButtonState::On, Input::Press) => ButtonState::Off,
58//!         (ButtonState::Off, Input::Press) => ButtonState::On,
59//!     })
60//!     .build()
61//!     .unwrap();
62//!
63//! assert_eq!(ButtonState::Off, sm.current_state());
64//! sm.consume(Input::Press);
65//! assert_eq!(ButtonState::On, sm.current_state());
66//! ```
67//! ## License
68//! MIT
69//!
70//! ## Contribution
71//! All contributions are welcome.
72//!
73//! If you have an idea to improve this crate, create new issue or submit new pull request.
74
75pub mod machine;