automata_like_programming/automaton_state.rs
1use std::{cell::RefCell, rc::Rc};
2
3use crate::automaton::NextState;
4
5/// Representation of a node in automaton graph. States act as stop points for an automaton where next states are determined or for
6/// halting the execution when no more state changes can be done.
7pub trait AutomatonState<'a, Id, D, E> {
8 /// Owned identifier used for identifying current state.
9 fn get_id_owned(&self) -> Id;
10 /// Reference to identifier user for identifying current state.
11 fn get_id(&self) -> &Id;
12
13 /// Represents change of current state in graph. Provides state to be executed by automaton. Implementations should use this method for executing operations connected with
14 /// state change.
15 fn execute_next_connection(&self, data: &mut D) -> Result<NextState<'a, Id, D, E>, E>;
16}
17
18pub type SharedAutomatonState<'a, Id, D, E> = Rc<RefCell<dyn AutomatonState<'a, Id, D, E> + 'a>>;
19
20/// Creates shared reference for given state. Returned type signature is: `Rc<RefCell<dyn AutomatonState>>`
21pub fn new_shared_automaton_state<'a, Id, D, E, S: AutomatonState<'a, Id, D, E> + 'a>(state: S) -> SharedAutomatonState<'a, Id, D, E> {
22 Rc::new(RefCell::new(state))
23}
24
25/// Creates shared reference for given state. Returned type signature is: `Rc<RefCell<S>>` where S is a concrete
26/// implementation of AutomatonState.
27pub fn new_shared_concrete_state<'a, Id, D, E, S: AutomatonState<'a, Id, D, E> + 'a>(state: S) -> Rc<RefCell<S>> {
28 Rc::new(RefCell::new(state))
29}
30
31/// Converts type signature from using concrete implementation type to `dyn AutomatonState`.
32pub fn convert_to_dyn_reference<'a, Id, D, E, S: AutomatonState<'a, Id, D, E> + 'a>(state: Rc<RefCell<S>>) -> SharedAutomatonState<'a, Id, D, E> {
33 state as SharedAutomatonState<'a, Id, D, E>
34}