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 serde::{Deserialize, Serialize};
use serde_json::Value;

/// Contains the definition for a single method argument
/// contains the name, description, help, value and whether the argument is required or not
/// The default value can be set in the `value` field which is optional when the argument is not required
#[derive(Clone)]
pub struct MethodArgument {
    /// The name of the argument
    pub name: String,

    /// A small description, what the argument is meant for
    pub description: String,

    /// A short help, for example which data type is expected for the argument
    pub help: String,

    /// A default value which can be given to the argument if not required
    pub value: Option<Value>,

    /// Whether the argument is required or not
    /// If the argument is required, the default value is ignored and can be `None`
    /// If the argument is not required, the default value can be `None` or `Some`
    pub required: bool,
}

/// Contains general information about the method
#[derive(Clone)]
pub struct Method {
    /// The name of the method
    pub name: String,

    /// A short description what the method will do
    /// Can contain a description for error codes as well
    pub description: String,

    /// Supplies help for example about required steps before this method is called (e.g. something
    /// has to be configured via a different method)
    pub help: String,

    /// A list of required or not required arguments
    pub arguments: Vec<MethodArgument>,
}

/// Contains the arguments for a called method
/// Stores them in a `HashMap` which can only be accessed by a single getter method
pub struct MethodArguments {
    /// The argument that can be requested by the method
    arguments: HashMap<String, Value>
}

impl MethodArguments {
    /// Creates an arguments struct from an empty HashMap
    pub fn empty() -> Self {
        Self::new(HashMap::new())
    }

    /// Creates an arguments struct from a given HashMap
    /// The type for the value might change to a `serde_json::Value`
    pub fn new(arguments: HashMap<String, Value>) -> Self {
        Self {
            arguments
        }
    }

    /// Gets the argument by the given name
    /// The return value may be changed to a `serde_json::Value` type
    pub fn get_argument(&self, name: &String) -> Option<Value> {
        match self.arguments.get(name) {
            Some(value) => Some(value.clone()),
            _ => None
        }
    }
}

/// This struct contains the structure for a method return
/// It consists of a return code and a single message which can be an error, a success or an info message
/// Is used together with `serde` for serialization and deserialization to and from JSON (mainly)
#[derive(Clone, Deserialize, Serialize)]
pub struct MethodResult {
    /// The return code of the method
    /// 0 = success
    /// > 0 = error
    pub code: i8,

    /// The message of the method
    /// Can be empty if there is nothing to say about the actions that were done
    pub message: Option<Value>,
}