83 lines
3.3 KiB
Rust
83 lines
3.3 KiB
Rust
use heromodels::db::hero::OurDB;
|
|
use heromodels::models::calendar::rhai::register_rhai_engine_functions;
|
|
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 = Arc::new(OurDB::new("temp_calendar_db", true).expect("Failed to create database"));
|
|
|
|
// Register the Calendar type with Rhai
|
|
// This function is generated by the #[rhai_model_export] attribute
|
|
Calendar::register_rhai_bindings_for_calendar(&mut engine, db.clone());
|
|
|
|
// Register a function to get the database instance
|
|
engine.register_fn("get_db", move || db.clone());
|
|
|
|
// Register a calendar builder function
|
|
engine.register_fn("calendar__builder", |id: i64| {
|
|
let id_option = if id <= 0 { None } else { Some(id as u32) };
|
|
Calendar::new(id_option, "New Calendar")
|
|
});
|
|
|
|
// Register setter methods for Calendar properties
|
|
engine.register_fn("set_description", |calendar: &mut Calendar, desc: String| {
|
|
calendar.description = Some(desc);
|
|
});
|
|
|
|
// Register getter methods for Calendar properties
|
|
engine.register_fn("get_description", |calendar: Calendar| -> String {
|
|
calendar.description.clone().unwrap_or_default()
|
|
});
|
|
|
|
// Register getter for base_data.id
|
|
engine.register_fn("get_id", |calendar: Calendar| -> i64 {
|
|
calendar.base_data.id as i64
|
|
});
|
|
|
|
// Register additional functions needed by the script
|
|
engine.register_fn("set_calendar", |_db: Arc<OurDB>, _calendar: Calendar| {
|
|
// In a real implementation, this would save the calendar to the database
|
|
println!("Calendar saved: {}", _calendar.name);
|
|
});
|
|
|
|
engine.register_fn("get_calendar_by_id", |_db: Arc<OurDB>, id: i64| -> Calendar {
|
|
// In a real implementation, this would retrieve the calendar from the database
|
|
Calendar::new(Some(id as u32), "Retrieved Calendar")
|
|
});
|
|
|
|
// Register a function to check if a calendar exists
|
|
engine.register_fn("calendar_exists", |_db: Arc<OurDB>, id: i64| -> bool {
|
|
// In a real implementation, this would check if the calendar exists in the database
|
|
id == 1 || id == 2
|
|
});
|
|
|
|
// Define the function separately to use with the wrap_vec_return macro
|
|
fn get_all_calendars(_db: Arc<OurDB>) -> Vec<Calendar> {
|
|
// In a real implementation, this would retrieve all calendars from the database
|
|
vec![Calendar::new(Some(1), "Calendar 1"), Calendar::new(Some(2), "Calendar 2")]
|
|
}
|
|
|
|
// Register the function with the wrap_vec_return macro
|
|
engine.register_fn("get_all_calendars", wrap_vec_return!(get_all_calendars, Arc<OurDB> => Calendar));
|
|
|
|
engine.register_fn("delete_calendar_by_id", |_db: Arc<OurDB>, _id: i64| {
|
|
// In a real implementation, this would delete the calendar from the database
|
|
println!("Calendar deleted with ID: {}", _id);
|
|
});
|
|
|
|
// Load and evaluate the Rhai script
|
|
let script_path = Path::new("examples/calendar_rhai/calendar.rhai");
|
|
let script = fs::read_to_string(script_path)?;
|
|
|
|
match engine.eval::<()>(&script) {
|
|
Ok(_) => println!("Script executed successfully!"),
|
|
Err(e) => eprintln!("Script execution failed: {}", e),
|
|
}
|
|
|
|
Ok(())
|
|
} |