1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#![doc = include_str!("../README.md")]
#![cfg_attr(not(test), no_std)]

/// Contains definitions for a state machine that contains error handling mechanisms
pub mod fallible;

/// Contains definitions used by a state machine without any error handling support
pub mod non_fallible;

/// Contains definitions and code for the messaging system
pub mod message;

/// Enum used to indicate to the guard function if the transition should transit to the
/// next state or remain in the current one.
/// ```rust
/// # use sfsm_base::non_fallible::{Transition, State};
/// # use sfsm_base::TransitGuard;
/// # struct FooState;
/// # struct BarState;
/// # impl State for FooState {};
/// # impl Into<BarState> for FooState {
/// #     fn into(self) -> BarState {
/// #         BarState{}
/// #     }
/// # }
///
/// # impl Transition<BarState> for FooState {
///     fn guard(&self) -> TransitGuard {
///         let foo = 0;
///         if foo == 0 {
///             TransitGuard::Remain
///         } else {
///             TransitGuard::Transit
///         }
///     }
/// # }
/// ```
#[derive(PartialEq)]
pub enum TransitGuard {
    /// Remains in the current state
    Remain,
    /// Transits into the next state
    Transit
}

/// Implements from<bool> trait for use of use.
/// This allows to transit by returning true. Which simplify the code since it allows to return the
/// TransitGuard from a simple comparison.
/// ```rust
/// # use sfsm_base::non_fallible::{Transition, State};
/// # use sfsm_base::TransitGuard;
/// # struct FooState;
/// # struct BarState;
/// # impl State for FooState {};
/// # impl Into<BarState> for FooState {
/// #     fn into(self) -> BarState {
/// #         BarState{}
/// #     }
/// # }
///
/// # impl Transition<BarState> for FooState {
///     fn guard(&self) -> TransitGuard {
///         let foo = 0;
///         (foo == 0).into() // Returns TransitGuard::Transit
///     }
/// # }
/// ```
impl From<bool> for TransitGuard {
    fn from(transit: bool) -> Self {
        if transit {
            TransitGuard::Transit
        } else {
            TransitGuard::Remain
        }
    }
}

/// Contains traits that are used to interact with the state machine but should not be implemented
/// manually. All necessary implementations will be created by the macros.
pub mod __protected {

    /// Trait that will be implemented for the state machine.
    pub trait StateMachine {
        /// The initial state of the state machine.
        type InitialState;

        /// The returned error. This is also implemented in non fallible state machines,
        /// but will be ignore as there is no case this error could occur.
        type Error;

        /// The generator enum containing all states
        type StatesEnum;

        /// Start function that must be called first. It populates the internal enum with the
        /// initial state. If step is called before start, the state machine will return an error.
        fn start(&mut self, state: Self::InitialState) -> Result<(), Self::Error>;

        /// The step function that executes all states and transitions.
        fn step(&mut self) -> Result<(), Self::Error>;

        /// If desired, the state machine can be stopped. When doing so, the internal states enum
        /// is returned.
        fn stop(self) -> Result<Self::StatesEnum, Self::Error>;

        /// Peek the internal states enum.
        fn peek_state(&self) -> &Self::StatesEnum;
    }

    /// An implementation of this trait will be generated for every state.
    /// This is can be used to test if the state machine is in a desired state.
    pub trait IsState<State>: StateMachine {
        /// The method must be called with the turbo fish syntax as otherwise Rust cannot figure out
        /// which implementation to call. To check if the state machine is in a given state call:
        ///
        /// ```ignore
        /// let is_in_state: bool = IsState::<State>::is_state(&sfsm);
        /// ```
        fn is_state(&self) -> bool;
    }
}

pub use __protected::*;
pub use non_fallible::*;
pub use fallible::*;
pub use message::*;
pub use message::__protected::*;