start porting model specs into heromodels
This commit is contained in:
		
							
								
								
									
										58
									
								
								heromodels/examples/calendar_rhai/calendar.rhai
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								heromodels/examples/calendar_rhai/calendar.rhai
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,58 @@
 | 
			
		||||
// 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");
 | 
			
		||||
 | 
			
		||||
print("Created calendar: " + calendar.name);
 | 
			
		||||
 | 
			
		||||
// Save the calendar to the database
 | 
			
		||||
set_calendar(db, calendar);
 | 
			
		||||
print("Calendar saved to database");
 | 
			
		||||
 | 
			
		||||
// Check if calendar exists and retrieve it
 | 
			
		||||
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 != "" {
 | 
			
		||||
        print("Description: " + desc);
 | 
			
		||||
    } else {
 | 
			
		||||
        print("No description available");
 | 
			
		||||
    }
 | 
			
		||||
} 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");
 | 
			
		||||
 | 
			
		||||
set_calendar(db, calendar2);
 | 
			
		||||
print("Second calendar saved");
 | 
			
		||||
 | 
			
		||||
// Get all calendars
 | 
			
		||||
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);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Delete a calendar
 | 
			
		||||
delete_calendar_by_id(db, 1);
 | 
			
		||||
print("Deleted calendar with ID 1");
 | 
			
		||||
 | 
			
		||||
// Verify deletion
 | 
			
		||||
if !calendar_exists(db, 1) {
 | 
			
		||||
    print("Calendar with ID 1 was successfully deleted");
 | 
			
		||||
} else {
 | 
			
		||||
    print("Failed to delete calendar with ID 1");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Count remaining calendars
 | 
			
		||||
let remaining_calendars = get_all_calendars(db);
 | 
			
		||||
print("Remaining calendars: " + remaining_calendars.len());
 | 
			
		||||
							
								
								
									
										83
									
								
								heromodels/examples/calendar_rhai/example.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										83
									
								
								heromodels/examples/calendar_rhai/example.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,83 @@
 | 
			
		||||
use heromodels::db::hero::OurDB;
 | 
			
		||||
use heromodels::models::calendar::Calendar;
 | 
			
		||||
use rhai::Engine;
 | 
			
		||||
use rhai_wrapper::wrap_vec_return;
 | 
			
		||||
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
 | 
			
		||||
    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);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    // 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(())
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user