sfsm_base/
message.rs

1
2
3/// Error type that will be returned if an error during the message polling or pushing occurred.
4/// It will indicate what the cause for the error was and return the original message in the push
5/// case.
6#[derive(Debug)]
7#[non_exhaustive]
8pub enum MessageError<T> {
9    /// Will be returned if the state is not active. If it originated during a push, the rejected messaged will be returned with the error.
10    StateIsNotActive(T),
11}
12
13pub mod __protected {
14    use crate::message::MessageError;
15    use crate::__protected::StateMachine;
16    use crate::{ReturnMessage, ReceiveMessage, State};
17
18    /// The PushMessage trait implementation will be generated by the add_message! macro and is used
19    /// to send messages into the state machine where they will then be forwarded to the correct
20    /// state.
21    pub trait PushMessage<TargetState, Message>: StateMachine
22        where TargetState: State + ReceiveMessage<Message>
23    {
24        /// This will call the receive_message function of ``` FooState ``` if it implemented the ReceiveMessage
25        /// trait for message 'FooMessage' and it has been declared to do so with the add_message! macro.
26        ///```ignore
27        /// use sfsm_base::PushMessage;
28        /// let some_message = 2u32;
29        /// PushMessage::<FooState, FooMessage>::push_message(&mut sfsm, some_message);
30        ///```
31        fn push_message(&mut self, message: Message) -> Result<(), MessageError<Message>>;
32    }
33
34    /// The PollMessage trait implementation will be generated by the add_message! macro and is used
35    /// to return messages from states.
36    pub trait PollMessage<TargetState, Message>: StateMachine
37        where TargetState: State + ReturnMessage<Message>
38    {
39        /// This will call the return_message function of ``` FooState ``` if it implemented the ReturnMessage
40        /// trait for message 'FooMessage' and it has been declared to do so with the add_message! macro.
41        ///```ignore
42        /// use sfsm_base::PollMessage;
43        /// let some_message = PollMessage::<FooState, FooMessage>::poll_message(&mut sfsm);
44        ///```
45        fn poll_message(&mut self) -> Result<Option<Message>, MessageError<()>>;
46    }
47}
48
49
50/// Trait to handle an incoming message
51///
52/// Implement this trait for a state that can receive a message according to the add_message
53/// definition.
54/// Note: for the state to actually be able to receive a message, the message has to be added with
55/// the add_message!
56pub trait ReceiveMessage<Message> {
57    /// Receive a message when a message for the corresponding state is pushed into the state
58    /// machine
59    ///
60    /// ```rust
61    /// # use sfsm_base::message::ReceiveMessage;
62    /// # #[derive(Debug)]
63    /// # struct Message {};
64    /// # struct State {};
65    /// #
66    /// # impl ReceiveMessage<Message> for State {
67    ///     fn receive_message(&mut self, message: Message) {
68    ///         println!("Got a message {:?}", message);
69    ///     }
70    /// # }
71    /// ```
72    fn receive_message(&mut self, message: Message);
73}
74
75/// Trait to handle an outgoing message
76///
77/// Implement this trait for a state that can return a message according to the add_message
78/// definition.
79/// Note: for the state to actually be able to return a message, the message has to be added with
80/// the add_message! macro.
81pub trait ReturnMessage<Message> {
82    /// Return a message from a state when polled
83    ///
84    /// ```rust
85    /// # use sfsm_base::message::ReturnMessage;
86    /// # struct Message {};
87    /// # struct State {};
88    /// #
89    /// # impl ReturnMessage<Message> for State {
90    ///     fn return_message(&mut self) -> Option<Message> {
91    ///         let message = Message{};
92    ///         println!("Returning a message when polled");
93    ///         return Some(message);
94    ///     }
95    /// # }
96    ///
97    /// ```
98    fn return_message(&mut self) -> Option<Message>;
99}