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
//!
//! # Example of an automaton
//!
//! ```
//! use std::rc::Rc;
//! use automata_like_programming::{
//! automaton::AutomatonResult, automaton_state::{
//! new_shared_automaton_state, AutomatonState, SharedAutomatonState
//! }
//! };
//!
//! use automata_like_programming::automaton::{Automaton, NextState};
//!
//! // Example implementation of an automaton state that appends specified text into
//! // mutable buffer.
//! pub struct TestState {
//! id: u8,
//! text: &'static str,
//! next_state: Option<SharedAutomatonState<'static, u8, String, String>>
//! }
//!
//! impl TestState {
//! pub fn new(
//! id: u8,
//! text: &'static str,
//! next_state: Option<SharedAutomatonState<'static, u8, String, String>>
//! ) -> Self {
//! Self { id, text, next_state }
//! }
//! }
//!
//! impl AutomatonState<'static, u8, String, String> for TestState {
//! fn get_id_owned(
//! &self
//! ) -> u8 {
//! self.id
//! }
//!
//! fn get_id(
//! &self
//! ) -> &u8 {
//! &self.id
//! }
//!
//! fn execute_next_connection(
//! &self,
//! data: &mut String
//! ) -> Result<NextState<'static, u8, String, String>, String> {
//! data.push_str(self.text);
//! if let Option::Some(nxt_state) = &self.next_state {
//! Result::Ok(NextState::Continue(Rc::clone(nxt_state)))
//! } else {
//! Result::Ok(NextState::NotFound)
//! }
//! }
//! }
//!
//! let mut automaton = Automaton::new({
//! // First we create the "Bar" state as it's the last state and it doesn't connect to
//! // any other state.
//! let bar_state = new_shared_automaton_state(
//! TestState::new(2, "Bar", Option::None)
//! );
//! // Secondly we declare the "Foo" state which is connected to "Bar" state.
//! let foo_state = new_shared_automaton_state(
//! TestState::new(1, "Foo", Option::Some(Rc::clone(&bar_state)))
//! );
//! foo_state
//! });
//! let mut buffer = String::with_capacity(6);
//! let result = automaton.run(&mut buffer);
//! assert!(result.is_could_not_find_next_state());
//! assert_eq!("FooBar", buffer);
//! ```
/// Basic part of automaton representing a node which is connected to either other nodes or itself.
/// Core mechanism representing an automaton that travels through defined states.
/// Simple implementations of automaton state.
/// Common implementations for testing purposes.