new-home-application-macro 1.0.2

Provides a derive macro for the Method in the new-home-application framework.
Documentation

New Home Application (Macro)

This crate provides a macro for deriving the Method trait.

Attributes

There are a couple of attributes that can be used on the struct and/or fields in the struct

Name Scope Description
description Struct Provides a description for the method
help Struct/Field Provides a help text for an argument/struct field or the method itself
argument Field Marks a struct field as an argument and contains a description for it
default Field Contains a default value for a struct field

Arguments

The word "Arguments" describes fields on the Method struct, that are set through the Method::set_arguments method. While you can implement the Method trait by yourself and just handle it on your own, this macro allows you to have the argument attribute on a field which causes the field to be automatically set by the set_arguments method. This however requires the field to have the type [serde_json::Value].

The argument attribute also has to have a content, which will become the description for the field.

To set a default value for the argument, there is the default attribute. The content of this attribute will be literally passed to the [serde_json::Value]::from method. So you will have to wrap strings in quotes. On the other side however, you can easily put a plain number or boolean in there and it will be filled in to the argument.

Example

You can see a full example in the examples/macro-example.rs file, but here you also have a small example for applying the macro onto a struct:

use serde_json::Value;
use new_home_application::method::{Method, MethodCallable};
use new_home_application::communication::{MethodCall, MethodResult};
use new_home_application_macro::Method;

#[derive(Method)]
#[description("Can convert a word into uppercase")]
#[help("Will convert the word to uppercase if uppercase flag is set")]
struct ExampleMethod {
#[argument("The word that should be converted")]
#[help("Can be any valid string")]
pub word: Value,

#[argument("Determines if the word will be converted to uppercase")]
#[default(false)]
pub uppercase: Value,
}

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

The logic for executing whatever the method should do is not implemented here as the Method trait only provides basic information for the method. The logic will be added through the MethodCallable trait which cannot be derived and has to be implemented by yourself.