db/heromodels/examples/biz_rhai/example.rs
2025-05-22 03:57:03 +03:00

42 lines
1.5 KiB
Rust

use rhai::{Engine, EvalAltResult, Scope};
use std::sync::Arc;
use heromodels::db::hero::OurDB; // Corrected path for OurDB
use heromodels::models::biz::register_biz_rhai_module; // Corrected path
use std::fs;
fn main() -> Result<(), Box<EvalAltResult>> {
println!("Executing Rhai script: examples/biz_rhai/biz.rhai");
// Create a new Rhai engine
let mut engine = Engine::new();
// Create an Arc<Mutex<OurDB>> instance
// For this example, we'll use an in-memory DB.
// The actual DB path or configuration might come from elsewhere in a real app.
let db_instance = Arc::new(OurDB::new(".", false).expect("Failed to create DB")); // Corrected OurDB::new args and removed Mutex
// Register the biz module with the engine
register_biz_rhai_module(&mut engine, Arc::clone(&db_instance));
// Read the Rhai script from file
let script_path = "examples/biz_rhai/biz.rhai";
let script_content = fs::read_to_string(script_path)
.map_err(|e| Box::new(EvalAltResult::ErrorSystem(format!("Cannot read script file: {}", script_path), e.into())))?;
// Create a new scope
let mut scope = Scope::new();
// Execute the script
match engine.run_with_scope(&mut scope, &script_content) {
Ok(_) => {
println!("Rhai script executed successfully!");
Ok(())
}
Err(e) => {
println!("Rhai script execution failed: {}", e);
println!("Details: {:?}", e);
Err(e)
}
}
}