fix: Fix all examples

This commit is contained in:
Mahmoud Emad
2025-05-17 16:03:00 +03:00
parent 57f59da43e
commit e4c50ca9d7
19 changed files with 166 additions and 80 deletions

View File

@@ -1,8 +1,8 @@
// Get the database instance
let db = get_db();
// Create a new calendar
let calendar = calendar__builder(1);
// Create a new calendar with auto-generated ID (pass 0 for auto-generated ID)
let calendar = calendar__builder(0);
calendar.name = "My First Calendar";
set_description(calendar, "A calendar for testing Rhai integration");
@@ -26,8 +26,8 @@ if calendar_exists(db, 1) {
print("Failed to retrieve calendar with ID 1");
}
// Create another calendar
let calendar2 = calendar__builder(2);
// Create another calendar with auto-generated ID
let calendar2 = calendar__builder(0);
calendar2.name = "My Second Calendar";
set_description(calendar2, "Another calendar for testing");

View File

@@ -18,53 +18,54 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// 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")
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(id as u32, "Retrieved Calendar")
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(1, "Calendar 1"), Calendar::new(2, "Calendar 2")]
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);
@@ -73,7 +74,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// 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),