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

40 lines
1.3 KiB
Rust

use rhai::{Engine, EvalAltResult, Scope};
use std::sync::Arc;
use heromodels::db::hero::OurDB;
use heromodels::models::projects::register_projects_rhai_module;
use std::fs;
fn main() -> Result<(), Box<EvalAltResult>> {
println!("Executing Rhai script: examples/project_rhai/project_test.rhai");
// Create a new Rhai engine
let mut engine = Engine::new();
// Create an Arc<OurDB> instance
let db_instance = Arc::new(OurDB::new(".", false).expect("Failed to create DB"));
// Register the projects module with the engine
register_projects_rhai_module(&mut engine, Arc::clone(&db_instance));
// Read the Rhai script from file
let script_path = "examples/project_rhai/project_test.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)
}
}
}