[][src]Crate new_home_application_macro

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

NameScopeDescription
descriptionStructProvides a description for the method
helpStruct/FieldProvides a help text for an argument/struct field or the method itself
argumentFieldMarks a struct field as an argument and contains a description for it
defaultFieldContains 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.

Derive Macros

Method