new-home-application 0.1.3

New Home iot application framework. Meant to build application for the New Home Core
Documentation
use std::collections::HashMap;

use crate::method::method_callable::MethodCallable;
use crate::method::method_structure::Method;

/// Implements the `MethodManager` with the help of a `Vec`tor and a `HashMap`
/// `Method`s are stored in the vector which is later looped through to get the requested method struct
/// `MethodCallable`s will be stored in the `HashMap`
pub struct MethodManagerImpl {
    /// Contains the `Method` structs
    /// Is initialized in the constructor as empty Vector
    methods: Vec<Method>,

    /// Contains the `MethodCallable`s
    /// Is initialized as empty `HashMap` in the constructor
    callables: HashMap<String, Box<dyn MethodCallable>>,
}

/// The `MethodManager` contains all methods with information about their name, description and help
/// as well as the arguments that can be supplied by the callee
/// Contains `MethodCallable`s in relation with the methods name
/// The name for a method is given by the `Method` struct
pub trait MethodManager {
    /// Registers the given `Method` struct with the given callable
    /// Uses the name of the `Method` struct as the name for the method used in later calls
    fn add_method(&mut self, method: Method, callable: Box<dyn MethodCallable>) -> ();

    /// Gets the method definition for the method by the given name
    /// Returns the `Method` if exists, otherwise returns a `None`
    fn get_method(&self, name: &String) -> Option<Method>;

    /// Gets a method callback from the given name
    /// If it exists the `MethodCallable` is returned otherwise a `None` is returned
    fn get_method_callback(&self, name: &String) -> Option<&Box<dyn MethodCallable>>;

    /// Returns a Map filled with method names as key and the Method description as value
    /// Creates a new copy of its own index at the current time
    fn get_methods(&self) -> HashMap<String, Method>;
}

impl MethodManagerImpl {
    pub fn new() -> Self {
        Self {
            methods: Vec::new(),
            callables: HashMap::new(),
        }
    }
}

impl MethodManager for MethodManagerImpl {
    fn add_method(&mut self, method: Method, callable: Box<dyn MethodCallable>) -> () {
        self.methods.push(method.clone());
        self.callables.insert(method.name, callable);
    }

    fn get_method(&self, name: &String) -> Option<Method> {
        for method in self.methods.iter() {
            if &method.name == name {
                return Some(method.clone());
            }
        }

        None
    }

    fn get_method_callback(&self, name: &String) -> Option<&Box<dyn MethodCallable>> {
        let callable = self.callables.get(name);

        match callable {
            Some(callable) => Some(callable),
            _ => None
        }
    }

    fn get_methods(&self) -> HashMap<String, Method> {
        let mut map = HashMap::new();

        for method in &self.methods {
            map.insert(method.name.clone(), method.clone());
        }

        map
    }
}