db/heromodels/examples/library_rhai/example.rs
2025-06-12 05:22:17 +03:00

39 lines
1.2 KiB
Rust

use heromodels::db::hero::OurDB;
use heromodels::models::register_library_rhai_module;
use rhai::Engine;
use std::sync::Arc;
use std::{fs, path::Path};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize Rhai engine
let mut engine = Engine::new();
// Initialize database with OurDB
let db_path = "temp_library_db";
// Clean up previous database file if it exists
if Path::new(db_path).exists() {
fs::remove_dir_all(db_path)?;
}
let db = Arc::new(OurDB::new(db_path, true).expect("Failed to create database"));
// Register the library module with Rhai
register_library_rhai_module(&mut engine, db.clone());
// Load and evaluate the Rhai script
let script_path = Path::new(file!()).parent().unwrap().join("library.rhai");
let script = fs::read_to_string(&script_path)?;
println!("--- Running Library Rhai Script ---");
match engine.eval::<()>(&script) {
Ok(_) => println!("\n--- Script executed successfully! ---"),
Err(e) => eprintln!("\n--- Script execution failed: {} ---", e),
}
// Clean up the database file
fs::remove_dir_all(db_path)?;
println!("--- Cleaned up temporary database. ---");
Ok(())
}