| Crates.io | herosal-rhai-derive |
| lib.rs | herosal-rhai-derive |
| version | 0.1.1 |
| created_at | 2025-12-13 07:44:21.315662+00 |
| updated_at | 2025-12-14 13:31:37.702568+00 |
| description | Derive macros for herosal-rhai-lib |
| homepage | |
| repository | https://github.com/threefoldtech/sal |
| max_upload_size | |
| id | 1982701 |
| size | 15,051 |
This crate provides procedural macros to simplify the integration of Rust types with the Rhai scripting engine.
RhaiApi Derive MacroThe RhaiApi macro automatically generates a Rhai module with a fluent, builder-style API for your Rust structs. This allows you to create and modify your structs in Rhai scripts using chained method calls.
When you derive RhaiApi on a struct, the macro generates:
{struct_name}_rhai_dsl.export_module within that module named generated_rhai_module.new_{struct_name}() function to create a new instance of your struct.id() function to retrieve the object's ID.Rust Struct Definition:
use derive::RhaiApi;
#[derive(RhaiApi, Clone)]
pub struct Product {
pub id: i64,
pub name: String,
pub price: f64,
}
Generated Rhai API Usage:
// Import the generated module
import product_rhai_dsl::generated_rhai_module as product_api;
// Use the fluent API to build a new product
let my_product = product_api::new_product()
.id(1)
.name("Awesome Gadget")
.price(99.99);
print(my_product.id()); // prints 1
FromVec Derive MacroThe FromVec macro is a utility for tuple structs that contain a single field. It implements the From<T> trait, where T is the inner type, allowing for seamless conversions.
Rust Struct Definition:
use derive::FromVec;
#[derive(FromVec)]
pub struct MyVec(Vec<u8>);
Usage:
let data = vec![1, 2, 3];
let my_vec = MyVec::from(data);
To use these macros in your project, add this crate as a dependency in your Cargo.toml file:
[dependencies]
derive = { path = "../path/to/rhailib/src/derive" }