new-home-application 1.0.2

New Home iot application framework. Meant to build application for the New Home Core
Documentation
extern crate new_home_application;
extern crate serde_json;

use std::collections::HashMap;

use serde_json::Value;

use new_home_application::communication::{MethodCall, MethodResult};
use new_home_application::method::{Method, MethodCallable, MethodError};

struct ExampleMethod {
    pub word: Value,

    pub uppercase: Value,
}

impl Method for ExampleMethod {
    fn name(&self) -> String {
        String::from(r#"example_method"#)
    }

    fn description(&self) -> String {
        String::from(r#"Can convert a word into uppercase"#)
    }

    fn help(&self) -> String {
        String::from(r#"Will convert the word to uppercase if uppercase flag is set"#)
    }

    fn set_arguments(
        &mut self,
        arguments: HashMap<String, serde_json::Value>,
    ) -> Result<(), MethodError> {
        match arguments.get("word") {
            Some(value) => {
                self.word = value.clone();
            }
            _ => {
                return Err(MethodError::message("Missing value for word"));
            }
        }
        self.uppercase = arguments
            .get("uppercase")
            .unwrap_or(&serde_json::Value::from(false))
            .clone();
        Ok(())
    }
}

impl MethodCallable for ExampleMethod {
    fn call(&mut self, _: MethodCall) -> MethodResult {
        unimplemented!()
    }
}

fn main() {
    let mut ex = ExampleMethod {
        word: Value::Null,
        uppercase: Value::Null,
    };

    let mut map = HashMap::<String, Value>::new();

    map.insert(
        String::from("word"),
        Value::String(String::from("The word")),
    );

    if let Err(error) = ex.set_arguments(map) {
        println!("Could not complete set_arguments: {}", error);

        return;
    }

    println!("{:?}", ex.word);
    println!("{:?}", ex.uppercase);
}