funcall
A lightweight Rust library that turns functions into JSON-callable tools with zero boilerplate using a simple #[funcall]
macro.
Overview
funcall
is a lightweight Rust framework that enables dynamic function calling through JSON interfaces. It provides macros to automatically wrap Rust functions and expose them as callable tools with JSON serialization/deserialization.
Features
- Automatic function wrapping: Convert regular Rust functions into JSON-callable tools
- Type-safe argument handling: Supports primitive types,
Option
,Vec
, and anyDeserialize
types - Dynamic invocation: Call functions by name at runtime
- Minimal boilerplate: Simple attribute macro syntax
Installation
Add to your Cargo.toml
:
[]
= { = "https://github.com/realmorrisliu/funcall" }
= "1.0"
= { = "1.0", = ["derive"] }
Usage
1. Define Functions
use funcall;
use Deserialize;
2. Register and Call Functions
use tools;
use json;
let tools = tools!;
// Basic math operations
let result = tools;
println!; // 5
let result = tools;
println!; // 5
// Flexible greeting function
let result = tools;
println!; // "Hello, Morris!!!"
let result = tools;
println!; // "Hello!"
// Working with collections
let result = tools;
println!; // 10
// Custom struct handling
let result = tools;
println!; // "Morris is 30 years old"
Supported Argument Types
Type | JSON Representation | Notes |
---|---|---|
i32 |
Number | Converted from i64 |
f64 |
Number | |
bool |
Boolean | |
String |
String | |
Option<T> |
Any or null | Null becomes None |
Vec<T> |
Array | T must be deserializable |
Custom | Object/Array/etc | Must implement Deserialize |
Error Handling
- Invalid JSON input will panic
- Wrong number of arguments will panic
- Type mismatches will panic
For production use, consider wrapping calls in error handling.
Limitations
- Function names must be valid Rust identifiers
- All arguments must be positional (no named arguments)
- Return types must be serializable to JSON
License
MIT
Contribution
Contributions are welcome! Please open issues or pull requests on GitHub.