Comments (2)
Planning to compile to the following structure:
mod bottle_filler {
#[derive(Debug, Clone)]
pub enum State {
Idle { remaining: f32 },
Filling(f32),
Empty,
}
#[allow(dead_code)]
enum StateIdentifier {
Idle,
Filling,
Empty,
}
pub struct Machine {
current_state: StateIdentifier,
idle: State,
filling: State,
empty: State,
}
impl Machine {
pub fn new() -> Self {
Self {
current_state: StateIdentifier::Idle,
idle: State::Idle {
remaining: <f32 as core::default::Default>::default(),
},
filling: State::Filling(<f32 as core::default::Default>::default()),
empty: State::Empty,
}
}
fn handle_fill_bottle(_from: Option<&State>, to: &mut State, volume: f32) {
if let State::Filling(ref mut filling_volume) = to {
*filling_volume = volume;
}
}
fn handle_fuel_tank(_from: Option<&State>, to: &mut State) {
if let State::Idle { ref mut remaining } = to {
*remaining = 42.0;
}
}
fn handle_bottle_full(from: Option<&State>, to: &mut State) {
if let Some(State::Filling(ref volume)) = from {
if let State::Idle { ref mut remaining } = to {
*remaining -= volume;
}
}
}
#[allow(unreachable_code)]
pub fn fill(&mut self, volume: f32) -> bool {
match self.current_state {
StateIdentifier::Idle => {
{
let volume = volume;
if let State::Idle { remaining } = self.idle {
if (|| -> bool {
volume <= remaining
})() {
self.current_state = StateIdentifier::Filling;
Self::handle_fill_bottle(Some(&mut self.idle), &mut self.filling, volume);
return true;
}
}
}
{
let volume = volume;
if let State::Idle { remaining } = self.idle {
if (|| -> bool {
volume > remaining
})() {
self.current_state = StateIdentifier::Empty;
return true;
}
}
}
},
_ => {},
}
false
}
#[allow(unreachable_code)]
pub fn dump(&mut self) -> bool {
match self.current_state {
StateIdentifier::Idle => {
{
if let State::Idle { .. } = self.idle {
self.current_state = StateIdentifier::Empty;
return true;
}
}
},
_ => {},
}
false
}
#[allow(unreachable_code)]
pub fn fuel(&mut self) -> bool {
match self.current_state {
StateIdentifier::Idle => {
{
if let State::Idle { .. } = self.idle {
self.current_state = StateIdentifier::Idle;
Self::handle_fuel_tank(None, &mut self.idle);
return true;
}
}
},
StateIdentifier::Empty => {
{
if let State::Empty = self.empty {
self.current_state = StateIdentifier::Idle;
Self::handle_fuel_tank(Some(&mut self.empty), &mut self.idle);
return true;
}
}
}
_ => {},
}
false
}
#[allow(unreachable_code)]
pub fn full(&mut self) -> bool {
match self.current_state {
StateIdentifier::Filling => {
{
if let State::Filling(..) = self.filling {
self.current_state = StateIdentifier::Idle;
Self::handle_bottle_full(Some(&mut self.filling), &mut self.idle);
return true;
}
}
},
_ => {},
}
false
}
}
}
from declarative-state-machine.
Implemented data types for the new syntax and code generation from dummy data on transition-rework. Parsing will be the next step.
from declarative-state-machine.
Related Issues (1)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from declarative-state-machine.