command_core 0.1.1

A no_std flexible function interpreter using phf for compile-time command dispatch.
Documentation
#![no_std]
pub use phf;

/// A replacement for `fn(&mut T, &[&str])`
pub type CommandFn<T> = fn(&FI<T>,&mut T, &[&str]);

/// This structure stores an inner PHF map which routes inputted commands
/// to the proper functions that mutate a shared state passed to `interpret`.
pub struct FI<T: 'static> {
    functions: &'static phf::Map<&'static str, CommandFn<T>>,
}

impl<T: 'static> FI<T> {
    /// Creates a new function interpreter from the given PHF map.
    #[inline(always)]
    pub const fn new(inner: &'static phf::Map<&'static str, CommandFn<T>>) -> Self {
        Self { functions: inner }
    }

    /// Interprets a single command and runs the associated function with the provided arguments, also gives acess to an immutable reference of the interpreter for recursive commands or conditional logic.
    /// Returns `true` if the command was found and executed, `false` otherwise.
    #[inline(always)]
    pub fn interpret(&self, command_et_args: &[&str], input: &mut T) -> bool {
        if let Some((cmd, args)) = command_et_args.split_first() {
            if let Some(&inner_function) = self.functions.get(cmd) {
                inner_function(self,input, args);
                return true;
            }
        }
        false
    }
}