https://github.com/neo-ciber94/restate
A state machine library in Rust
https://github.com/neo-ciber94/restate
Last synced: 8 months ago
JSON representation
A state machine library in Rust
- Host: GitHub
- URL: https://github.com/neo-ciber94/restate
- Owner: Neo-Ciber94
- License: mit
- Created: 2023-02-25T18:51:18.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-02-25T21:24:55.000Z (over 2 years ago)
- Last Synced: 2024-12-28T12:30:53.615Z (10 months ago)
- Language: Rust
- Size: 17.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# restate
[![ci-badge]][ci] [![Latest Version]][crates.io] [![docs-badge]][docs]
[ci]: https://github.com/Neo-Ciber94/restate/actions/workflows/ci.yml
[ci-badge]: https://github.com/Neo-Ciber94/restate/actions/workflows/ci.yml/badge.svg
[latest version]: https://img.shields.io/crates/v/restate.svg
[crates.io]: https://crates.io/crates/restate
[docs]: https://docs.rs/restate
[docs-badge]: https://docs.rs/restate/badge.svg`restate` is a Rust library that provides a simple way of defining and using finite state machines.
## Installation
Add the following to your Cargo.toml file:
```toml
[dependencies]
restate = "0.1.0-alpha"
```Or use
```bash
cargo add restate
```## Usage
restate can be used to define state machines that can transition between different states based on events. Here is an example:
```rust
use restate::blocking::*;#[derive(Debug, Clone, PartialEq, Eq)]
struct Active;#[derive(PartialEq, Eq)]
enum CountEvent {
Increment,
Decrement,
}let mut sm = Machine::with_context(0)
.on_next(
Builder::self_transition(Active, CountEvent::Increment).action(
|cx: ContextMut| {
*cx.context += 1;
},
),
)
.on_next(
Builder::self_transition(Active, CountEvent::Decrement).action(
|cx: ContextMut| {
*cx.context -= 1;
},
),
)
.start(Active);sm.send(CountEvent::Increment).unwrap();
sm.send(CountEvent::Increment).unwrap();
sm.send(CountEvent::Increment).unwrap();
sm.send(CountEvent::Decrement).unwrap();assert_eq!(*sm.context(), 2);
```This example creates a state machine with a single state Active and two events Increment and Decrement. It then adds a self-transition for each event that increments or decrements an integer in the machine's context. Finally, it starts the machine with the Active state, sends some events to it, and checks the final value of the context.
## License
This library is licensed under the MIT license. See the LICENSE file for more details.