automata_like_programming/
lib.rs

1//!
2//! # Example of an automaton
3//! 
4//! ```
5//! use std::rc::Rc;
6//! use automata_like_programming::{
7//!         automaton::AutomatonResult, automaton_state::{
8//!             new_shared_automaton_state, AutomatonState, SharedAutomatonState
9//!            }
10//!     };
11//!
12//! use automata_like_programming::automaton::{Automaton, NextState};
13//!
14//! // Example implementation of an automaton state that appends specified text into
15//! // mutable buffer.
16//! pub struct TestState {
17//!     id: u8,
18//!     text: &'static str,
19//!     next_state: Option<SharedAutomatonState<'static, u8, String, String>>
20//! }
21//! 
22//! impl TestState {
23//!     pub fn new(
24//!         id: u8, 
25//!         text: &'static str, 
26//!         next_state: Option<SharedAutomatonState<'static, u8, String, String>>
27//!     ) -> Self {
28//!         Self { id, text, next_state }
29//!     }
30//! }
31//! 
32//! impl AutomatonState<'static, u8, String, String> for TestState {
33//!     fn get_id_owned(
34//!         &self
35//!     ) -> u8 {
36//!         self.id
37//!     }
38//!     
39//!     fn get_id(
40//!         &self
41//!     ) -> &u8 {
42//!         &self.id
43//!     }
44//!     
45//!     fn execute_next_connection(
46//!         &self, 
47//!         data: &mut String
48//!     ) -> Result<NextState<'static, u8, String, String>, String> {
49//!         data.push_str(self.text);
50//!         if let Option::Some(nxt_state) = &self.next_state {
51//!             Result::Ok(NextState::Continue(Rc::clone(nxt_state)))
52//!         } else {
53//!             Result::Ok(NextState::NotFound)
54//!         }
55//!     }
56//! }
57//! 
58//! let mut automaton = Automaton::new(|| {
59//!     // First we create the "Bar" state as it's the last state and it doesn't connect to
60//!     // any other state.
61//!     let bar_state = new_shared_automaton_state(
62//!                         TestState::new(2, "Bar", Option::None)
63//!                     );
64//!     // Secondly we declare the "Foo" state which is connected to "Bar" state.
65//!     let foo_state = new_shared_automaton_state(
66//!                         TestState::new(1, "Foo", Option::Some(Rc::clone(&bar_state)))
67//!                     );
68//!     foo_state
69//! });
70//! let mut buffer = String::with_capacity(6);
71//! let result = automaton.run(&mut buffer);
72//! assert!(result.is_could_not_find_next_state());
73//! assert_eq!("FooBar", buffer);
74//! ```
75/// Basic part of automaton representing a node which is connected to either other nodes or itself.
76pub mod automaton_state;
77/// Core mechanism representing an automaton that travels through defined states.
78pub mod automaton;
79/// Simple implementations of automaton state.
80pub mod simple_impl;