2.4 KiB
2.4 KiB
HeroModels Rhai
A central Rhai scripting engine for the HeroModels project that provides a unified way to interact with all HeroModels types through Rhai scripts.
Overview
This crate provides:
- A central Rhai engine that registers all HeroModels modules
- Helper functions for evaluating Rhai scripts and ASTs
- Example scripts demonstrating how to use HeroModels with Rhai
- A mock database implementation for testing and examples
Usage
Basic Usage
use std::sync::Arc;
use engine::{create_heromodels_engine, eval_script};
use engine::mock_db::create_mock_db;
// Create a mock database
let db = create_mock_db();
// Create the Rhai engine with all modules registered
let engine = create_heromodels_engine(db);
// Run a Rhai script
let result = eval_script(&engine, r#"
let calendar = new_calendar("My Calendar");
calendar.set_description("My personal calendar");
print(`Created calendar: ${calendar.get_name()}`);
calendar
"#);
match result {
Ok(val) => println!("Script returned: {:?}", val),
Err(err) => eprintln!("Script error: {}", err),
}
Using Specific Modules
If you only need specific modules, you can register them individually:
use std::sync::Arc;
use rhai::Engine;
use engine::mock_db::create_mock_db;
// Create a mock database
let db = create_mock_db();
// Create a new Rhai engine
let mut engine = Engine::new();
// Register only the calendar module
heromodels::models::calendar::register_calendar_rhai_module(&mut engine, db.clone());
// Now you can use calendar-related functions in your scripts
Examples
This crate includes several examples demonstrating how to use different HeroModels modules with Rhai:
calendar_example
: Working with calendars, events, and attendeesflow_example
: Working with flows, steps, and signature requirementslegal_example
: Working with contracts, revisions, and signersprojects_example
: Working with projects and their propertiesfinance_example
: Working with financial models (if available)
To run an example:
cargo run --example calendar_example
Features
The crate supports the following features:
flow
: Enable the Flow modulelegal
: Enable the Legal moduleprojects
: Enable the Projects modulebiz
: Enable the Business module
By default, only the Calendar module is always enabled.
License
Same as the HeroModels project.