94 lines
2.4 KiB
Markdown
94 lines
2.4 KiB
Markdown
# 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:
|
|
|
|
1. A central Rhai engine that registers all HeroModels modules
|
|
2. Helper functions for evaluating Rhai scripts and ASTs
|
|
3. Example scripts demonstrating how to use HeroModels with Rhai
|
|
4. A mock database implementation for testing and examples
|
|
|
|
## Usage
|
|
|
|
### Basic Usage
|
|
|
|
```rust
|
|
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:
|
|
|
|
```rust
|
|
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 attendees
|
|
- `flow_example`: Working with flows, steps, and signature requirements
|
|
- `legal_example`: Working with contracts, revisions, and signers
|
|
- `projects_example`: Working with projects and their properties
|
|
- `finance_example`: Working with financial models (if available)
|
|
|
|
To run an example:
|
|
|
|
```bash
|
|
cargo run --example calendar_example
|
|
```
|
|
|
|
## Features
|
|
|
|
The crate supports the following features:
|
|
|
|
- `flow`: Enable the Flow module
|
|
- `legal`: Enable the Legal module
|
|
- `projects`: Enable the Projects module
|
|
- `biz`: Enable the Business module
|
|
|
|
By default, only the Calendar module is always enabled.
|
|
|
|
## License
|
|
|
|
Same as the HeroModels project.
|