Skip to content

Commit

Permalink
Fallible actions support
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitriy Kumshayev committed Jul 2, 2024
1 parent 832a366 commit 8bd2571
Show file tree
Hide file tree
Showing 23 changed files with 116 additions and 86 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ process.
- `StateMachine::new` and `StateMachine::new_with_state` are now const functions
- Fixed clippy warnings
- [breaking] Changed guard functions return type from Result<(),_> to Result<bool,_>
- [breaking] Changed action functions return type from () to Result<NextStateData,_>

## [v0.6.0] - 2022-11-02

Expand Down
10 changes: 6 additions & 4 deletions examples/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,24 @@ impl StateMachineContext for Context {
Ok(true)
}

async fn action2(&mut self) -> () {
async fn action2(&mut self) -> Result<(), ()> {
println!("`action2` called from async context");
if !*self.lock.read().await {
self.done = true;
}
Ok(())
}

async fn action1(&mut self) -> () {
async fn action1(&mut self) -> Result<(), ()> {
println!("`action1` called from async context");
let mut lock = self.lock.write().await;
*lock = true;
Ok(())
}

fn action3(&mut self) -> bool {
fn action3(&mut self) -> Result<bool, ()> {
println!("`action3` called from sync context, done = `{}`", self.done);
self.done
Ok(self.done)
}
}

Expand Down
6 changes: 4 additions & 2 deletions examples/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ pub struct Context {
}

impl StateMachineContext for Context {
fn count_transition1(&mut self) {
fn count_transition1(&mut self) -> Result<(), ()> {
self.num_transitions += 1;
Ok(())
}

fn count_transition2(&mut self) {
fn count_transition2(&mut self) -> Result<(), ()> {
self.num_transitions += 1;
Ok(())
}
}

Expand Down
16 changes: 8 additions & 8 deletions examples/dominos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ statemachine! {
pub struct Context;

impl StateMachineContext for Context {
fn to_d2(&mut self) -> Option<Events> {
Some(Events::ToD2)
fn to_d2(&mut self) -> Result<Option<Events>, ()> {
Ok(Some(Events::ToD2))
}

fn to_d3(&mut self, _state_data: &Option<Events>) -> Option<Events> {
Some(Events::ToD3)
fn to_d3(&mut self, _state_data: &Option<Events>) -> Result<Option<Events>, ()> {
Ok(Some(Events::ToD3))
}

fn to_d4(&mut self, _state_data: &Option<Events>) -> Option<Events> {
Some(Events::ToD4)
fn to_d4(&mut self, _state_data: &Option<Events>) -> Result<Option<Events>, ()> {
Ok(Some(Events::ToD4))
}

fn to_d5(&mut self, _state_data: &Option<Events>) -> Option<Events> {
Some(Events::ToD5)
fn to_d5(&mut self, _state_data: &Option<Events>) -> Result<Option<Events>, ()> {
Ok(Some(Events::ToD5))
}
}

Expand Down
3 changes: 2 additions & 1 deletion examples/event_with_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ impl StateMachineContext for Context {
Ok(event_data == &MyEventData(42))
}

fn action(&mut self, event_data: MyEventData) {
fn action(&mut self, event_data: MyEventData) -> Result<(), ()> {
println!("Got valid Event Data = {}", event_data.0);
Ok(())
}
}

Expand Down
3 changes: 2 additions & 1 deletion examples/event_with_mutable_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ impl StateMachineContext for Context {
Ok(true)
}

fn action(&mut self, event_data: &mut MyEventData) {
fn action(&mut self, event_data: &mut MyEventData) -> Result<(), ()> {
println!("Got valid Event Data = {}", event_data.0);
Ok(())
}
}

Expand Down
6 changes: 4 additions & 2 deletions examples/event_with_reference_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,18 @@ impl StateMachineContext for Context {
Ok(!event_data.is_empty())
}

fn action1(&mut self, event_data: &[u8]) {
fn action1(&mut self, event_data: &[u8]) -> Result<(), ()> {
println!("Got valid Event Data = {:?}", event_data);
Ok(())
}

fn guard2(&mut self, event_data: &MyReferenceWrapper) -> Result<bool, ()> {
Ok(*event_data.0 > 9000)
}

fn action2(&mut self, event_data: MyReferenceWrapper) {
fn action2(&mut self, event_data: MyReferenceWrapper) -> Result<(), ()> {
println!("Got valid Event Data = {}", event_data.0);
Ok(())
}
}

Expand Down
6 changes: 4 additions & 2 deletions examples/ex3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ impl StateMachineContext for Context {
Ok(false)
}

fn action1(&mut self) {
fn action1(&mut self) -> Result<(), ()> {
//println!("Action 1");
Ok(())
}

fn action2(&mut self) {
fn action2(&mut self) -> Result<(), ()> {
//println!("Action 1");
Ok(())
}
}

Expand Down
4 changes: 2 additions & 2 deletions examples/guard_action_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl StateMachineContext for Context {
}

// Action1 has access to the data from Event1, and need to return the state data for State2
fn action1(&mut self, _event_data: MyEventData) -> MyStateData {
fn action1(&mut self, _event_data: MyEventData) -> Result<MyStateData, ()> {
todo!()
}

Expand All @@ -42,7 +42,7 @@ impl StateMachineContext for Context {
}

// Action2 has access to the data from State2
fn action2(&mut self, _state_data: &MyStateData) {
fn action2(&mut self, _state_data: &MyStateData) -> Result<(), ()> {
todo!()
}
}
Expand Down
11 changes: 8 additions & 3 deletions examples/guard_action_syntax_with_temporary_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,14 @@ impl StateMachineContext for Context {
}

// Action1 has access to the data from Event1, and need to return the state data for State2
fn action1(&mut self, temp_context: &mut u16, _event_data: MyEventData) -> MyStateData {
fn action1(
&mut self,
temp_context: &mut u16,
_event_data: MyEventData,
) -> Result<MyStateData, ()> {
*temp_context += 1;

MyStateData(1)
Ok(MyStateData(1))
}

// Guard2 has access to the data from State2
Expand All @@ -49,8 +53,9 @@ impl StateMachineContext for Context {
}

// Action2 has access to the data from State2
fn action2(&mut self, temp_context: &mut u16, _state_data: &MyStateData) {
fn action2(&mut self, temp_context: &mut u16, _state_data: &MyStateData) -> Result<(), ()> {
*temp_context += 1;
Ok(())
}
}

Expand Down
4 changes: 2 additions & 2 deletions examples/guard_custom_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl StateMachineContext for Context {
}

// Action1 has access to the data from Event1, and need to return the state data for State2
fn action1(&mut self, _event_data: MyEventData) -> MyStateData {
fn action1(&mut self, _event_data: MyEventData) -> Result<MyStateData, Self::GuardError> {
todo!()
}

Expand All @@ -50,7 +50,7 @@ impl StateMachineContext for Context {
}

// Action2 has access to the data from State2
fn action2(&mut self, _state_data: &MyStateData) {
fn action2(&mut self, _state_data: &MyStateData) -> Result<(), Self::GuardError> {
todo!()
}
}
Expand Down
16 changes: 9 additions & 7 deletions examples/named_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,24 @@ impl AsyncSimpleStateMachineContext for Context {
Ok(true)
}

async fn action1(&mut self) -> () {
fn action3(&mut self) -> Result<bool, ()> {
println!("`action3` called from sync context, done = `{}`", self.done);
Ok(self.done)
}

async fn action1(&mut self) -> Result<(), ()> {
println!("`action1` called from async context");
let mut lock = self.lock.write().await;
*lock = true;
Ok(())
}

fn action3(&mut self) -> bool {
println!("`action3` called from sync context, done = `{}`", self.done);
self.done
}

async fn action2(&mut self) -> () {
async fn action2(&mut self) -> Result<(), ()> {
println!("`action2` called from async context");
if !*self.lock.read().await {
self.done = true;
}
Ok(())
}
}

Expand Down
16 changes: 8 additions & 8 deletions examples/named_dominos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ statemachine! {
pub struct Context;

impl DominosStateMachineContext for Context {
fn to_d2(&mut self) -> Option<DominosEvents> {
Some(DominosEvents::ToD2)
fn to_d2(&mut self) -> Result<Option<DominosEvents>, ()> {
Ok(Some(DominosEvents::ToD2))
}

fn to_d3(&mut self, _state_data: &Option<DominosEvents>) -> Option<DominosEvents> {
Some(DominosEvents::ToD3)
fn to_d3(&mut self, _state_data: &Option<DominosEvents>) -> Result<Option<DominosEvents>, ()> {
Ok(Some(DominosEvents::ToD3))
}

fn to_d4(&mut self, _state_data: &Option<DominosEvents>) -> Option<DominosEvents> {
Some(DominosEvents::ToD4)
fn to_d4(&mut self, _state_data: &Option<DominosEvents>) -> Result<Option<DominosEvents>, ()> {
Ok(Some(DominosEvents::ToD4))
}

fn to_d5(&mut self, _state_data: &Option<DominosEvents>) -> Option<DominosEvents> {
Some(DominosEvents::ToD5)
fn to_d5(&mut self, _state_data: &Option<DominosEvents>) -> Result<Option<DominosEvents>, ()> {
Ok(Some(DominosEvents::ToD5))
}
}

Expand Down
6 changes: 4 additions & 2 deletions examples/named_ex3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ impl LoopingWithGuardsStateMachineContext for Context {
Ok(false)
}

fn action1(&mut self) {
fn action1(&mut self) -> Result<(), ()> {
//println!("Action 1");
Ok(())
}

fn action2(&mut self) {
fn action2(&mut self) -> Result<(), ()> {
//println!("Action 1");
Ok(())
}
}

Expand Down
4 changes: 2 additions & 2 deletions examples/named_state_with_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ statemachine! {
pub struct Context;

impl StatesWithDataStateMachineContext for Context {
fn action(&mut self) -> MyStateData {
MyStateData(42)
fn action(&mut self) -> Result<MyStateData, ()> {
Ok(MyStateData(42))
}
}

Expand Down
4 changes: 2 additions & 2 deletions examples/named_state_with_reference_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ statemachine! {
pub struct Context;

impl StatesWithRefDataStateMachineContext for Context {
fn action<'a>(&mut self) -> MyStateData<'a> {
MyStateData(&42)
fn action<'a>(&mut self) -> Result<MyStateData<'a>, ()> {
Ok(MyStateData(&42))
}
}

Expand Down
3 changes: 2 additions & 1 deletion examples/reuse_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ statemachine! {
pub struct Context(usize);

impl StateMachineContext for Context {
fn action(&mut self) {
fn action(&mut self) -> Result<(), ()> {
self.0 += 1;
Ok(())
}
}

Expand Down
4 changes: 2 additions & 2 deletions examples/starting_state_with_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ statemachine! {
pub struct Context;

impl StateMachineContext for Context {
fn action(&mut self) -> MyStateData {
MyStateData(42)
fn action(&mut self) -> Result<MyStateData, ()> {
Ok(MyStateData(42))
}
}

Expand Down
7 changes: 4 additions & 3 deletions examples/state_machine_logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ impl StateMachineContext for Context {
}

// Action1 has access to the data from Event1, and need to return the state data for State2
fn action1(&mut self, event_data: MyEventData) -> MyStateData {
fn action1(&mut self, event_data: MyEventData) -> Result<MyStateData, ()> {
println!("Creating state data for next state");
MyStateData(event_data.0)
Ok(MyStateData(event_data.0))
}

// Guard2 has access to the data from State2
Expand All @@ -46,8 +46,9 @@ impl StateMachineContext for Context {
}

// Action2 has access to the data from State2
fn action2(&mut self, state_data: &MyStateData) {
fn action2(&mut self, state_data: &MyStateData) -> Result<(), ()> {
println!("Printing state data {:?}", state_data);
Ok(())
}

fn log_process_event(&self, current_state: &States, event: &Events) {
Expand Down
4 changes: 2 additions & 2 deletions examples/state_with_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ statemachine! {
pub struct Context;

impl StateMachineContext for Context {
fn action(&mut self) -> MyStateData {
MyStateData(42)
fn action(&mut self) -> Result<MyStateData, ()> {
Ok(MyStateData(42))
}
}

Expand Down
4 changes: 2 additions & 2 deletions examples/state_with_reference_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ statemachine! {
pub struct Context;

impl StateMachineContext for Context {
fn action<'a>(&mut self) -> MyStateData<'a> {
MyStateData(&42)
fn action<'a>(&mut self) -> Result<MyStateData<'a>, ()> {
Ok(MyStateData(&42))
}
}

Expand Down
Loading

0 comments on commit 8bd2571

Please sign in to comment.