Giter Site home page Giter Site logo

statemachine-rs's Introduction

Workflow Status

statemachine-rs

A zero dependency crate to implement state machine.

Usage

Let's have a look at the following simple example. This example shows the state machine can transit its number (it called current_state in this machine) from given string ("next") and then, it produces outputs.

use statemachine_rs::machine::{
    builder::{BasicStateMachineBuilder, StateMachineBuilder},
    StateMachine,
};

fn main() {
    let sm = BasicStateMachineBuilder::start()
        .initial_state(1)
        .transition(|state, input| match (state, input) {
            (1, "next") => 2,
            (2, "next") => 3,
            _ => unreachable!(),
        })
        .build()
        .unwrap();

    assert_eq!(1, sm.current_state());
    sm.consume("next");
    assert_eq!(2, sm.current_state());
}

You can assemble your state machine by using statemachine_rs::machine::builder::BasicStateMachineBuilder. BasicStateMachineBuilder::initial_state() initializes the initial state of its machine. BasicStateMachineBuilder::transition() defines the transition model.

Of cource we can use enums for representing states and inputs. Let's have a look at another example.

The following example describes if you press the button, the state turns to be On. Otherwise, Off.

use statemachine_rs::machine::{
    builder::{BasicStateMachineBuilder, StateMachineBuilder},
    StateMachine,
};

#[derive(Clone, Debug, PartialEq)]
enum ButtonState {
    On,
    Off,
}

enum Input {
    Press,
}

fn main() {
    let sm = BasicStateMachineBuilder::start()
        .initial_state(ButtonState::Off)
        .transition(|state, input| match (state, input) {
            (ButtonState::On, Input::Press) => ButtonState::Off,
            (ButtonState::Off, Input::Press) => ButtonState::On,
        })
        .build()
        .unwrap();

    assert_eq!(ButtonState::Off, sm.current_state());
    sm.consume(Input::Press);
    assert_eq!(ButtonState::On, sm.current_state());
}

License

MIT

Contribution

All contributions are welcome.

If you have an idea to improve this crate, create new issue or submit new pull request.

License: MIT

statemachine-rs's People

Contributors

yuk1ty avatar

Stargazers

Christopher Maverick avatar  avatar  avatar tomotomo avatar cz avatar Masanori Ogino avatar

Watchers

James Cloos avatar  avatar

statemachine-rs's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.