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};
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(|| {
let bar_state = new_shared_automaton_state(
TestState::new(2, "Bar", Option::None)
);
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);