Module macros

Source
Expand description

Macros for defining AIMX functions with type safety.

This module provides three macros that simplify the process of registering functions with the AIMX function registry while maintaining type safety:

These macros abstract away the boilerplate code needed to implement the FunctionHandler trait and register functions with the registry.

§Usage

Functions are typically registered using closures that take a mutable reference to a FunctionRegistry. The macros are used within these registration closures:

use aimx::{define_function, define_direct_function, function_registry::FunctionRegistry};

fn register_example_functions(registry: &mut FunctionRegistry) {
    // Define a function that returns a Result
    define_function!(add, args: [f64, f64], |a: f64, b: f64| {
        Ok(a + b)
    })(registry);
     
    // Define a function that returns a direct value
    define_direct_function!(multiply, args: [f64, f64], |a: f64, b: f64| {
        a * b
    })(registry);
}

§Design Principles

The macros are designed with several key principles:

  • Type Safety: Automatic conversion between Value types and native Rust types
  • Error Handling: Proper integration with the anyhow error handling system
  • Simplicity: Minimal boilerplate for common function registration patterns
  • Flexibility: Support for functions with 0-4 arguments of any type

See also: crate::function_registry, crate::Value, crate::function_registry::FunctionHandler