Crate automata_like_programming

Source
Expand description

§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);

Modules§

automaton
Core mechanism representing an automaton that travels through defined states.
automaton_state
Basic part of automaton representing a node which is connected to either other nodes or itself.
simple_impl
Simple implementations of automaton state.

Macros§

series
Simplifies creation of series by creating states based on provided iterator and connects them coninously starting from root. Returns all states created in the process. Function can be passed to be executed at any default state switch (when no states from series can be matched). Function to be executed while switching states can also be provided. Most useful for long patterns. !!! WARNING Won’t create returning connections for matched starting patterns (starting elements existing in further parts of matched pattern meanining that new match could be possible while current match is still being processed).