implement more models and rhai examples

This commit is contained in:
timurgordon
2025-05-22 03:57:03 +03:00
parent aa8ef90f9f
commit 56ec505874
79 changed files with 4546 additions and 182 deletions

View File

@@ -1,11 +1,20 @@
// Get the database instance
let db = get_db();
// Create a new calendar
let calendar = calendar__builder(1);
calendar.name = "My First Calendar";
set_description(calendar, "A calendar for testing Rhai integration");
// Create a new calendar using the constructor and builder methods
print("Creating a new calendar with ID 1 via registered constructor...");
let calendar = new_calendar(1).
name("My First Calendar").
description("A calendar for testing Rhai integration");
let event = new_event(1).
title("My First Event").
description("An event for testing Rhai integration")
.add_attendee(new_attendee(1));
calendar.add_event(1);
print("Type of calendar object: " + type_of(calendar));
print("Created calendar: " + calendar.name);
// Save the calendar to the database
@@ -16,20 +25,24 @@ print("Calendar saved to database");
if calendar_exists(db, 1) {
let retrieved_calendar = get_calendar_by_id(db, 1);
print("Retrieved calendar: " + retrieved_calendar.name);
let desc = get_description(retrieved_calendar);
if desc != "" {
// Access the 'description' field directly.
// Note: 'description' is Option<String>. Rhai handles options.
// You might want to check for 'is_some()' or 'is_none()' or use 'unwrap_or()' pattern if needed.
let desc = retrieved_calendar.description;
if desc != () && desc != "" { // Check against '()' for None and empty string
print("Description: " + desc);
} else {
print("No description available");
print("No description available or it's None");
}
} else {
print("Failed to retrieve calendar with ID 1");
}
// Create another calendar
let calendar2 = calendar__builder(2);
calendar2.name = "My Second Calendar";
set_description(calendar2, "Another calendar for testing");
print("Creating a new calendar with ID 2 using builder methods...");
let calendar2 = new_calendar(2).
name("My Second Calendar").
description("Another calendar for testing");
set_calendar(db, calendar2);
print("Second calendar saved");
@@ -38,8 +51,9 @@ print("Second calendar saved");
let all_calendars = get_all_calendars(db);
print("Total calendars: " + all_calendars.len());
for calendar in all_calendars {
print("Calendar ID: " + get_id(calendar) + ", Name: " + calendar.name);
for cal_item in all_calendars { // Renamed loop variable to avoid conflict if 'calendar' is still in scope
// Access 'base_data.id' and 'name' fields directly
print("Calendar ID: " + cal_item.base_data.id + ", Name: " + cal_item.name);
}
// Delete a calendar

View File

@@ -1,7 +1,6 @@
use heromodels::db::hero::OurDB;
use heromodels::models::calendar::Calendar;
use heromodels::models::calendar::rhai::register_rhai_engine_functions;
use rhai::Engine;
use rhai_wrapper::wrap_vec_return;
use std::sync::Arc;
use std::{fs, path::Path};
@@ -9,66 +8,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize Rhai engine
let mut engine = Engine::new();
// Initialize database
let db = Arc::new(OurDB::new("temp_calendar_db", true).expect("Failed to create database"));
// 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| {
Calendar::new(id as u32, "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(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(1, "Calendar 1"), Calendar::new(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);
});
// Register functions using the new centralized method
register_rhai_engine_functions(&mut engine, db.clone());
// Load and evaluate the Rhai script
let script_path = Path::new("examples/calendar_rhai/calendar.rhai");