add engine and rpc client, archive old code
This commit is contained in:
101
engine/examples/calendar/calendar_script.rhai
Normal file
101
engine/examples/calendar/calendar_script.rhai
Normal file
@@ -0,0 +1,101 @@
|
||||
// calendar_script.rhai
|
||||
// Example Rhai script for working with Calendar models
|
||||
|
||||
// Constants for AttendanceStatus
|
||||
const NO_RESPONSE = "NoResponse";
|
||||
const ACCEPTED = "Accepted";
|
||||
const DECLINED = "Declined";
|
||||
const TENTATIVE = "Tentative";
|
||||
|
||||
// Create a new calendar using builder pattern
|
||||
let my_calendar = new_calendar()
|
||||
.name("Team Calendar")
|
||||
.description("Calendar for team events and meetings");
|
||||
|
||||
print(`Created calendar: ${my_calendar.name} (${my_calendar.id})`);
|
||||
|
||||
|
||||
// Add attendees to the event
|
||||
let alice = new_attendee()
|
||||
.with_contact_id(1)
|
||||
.with_status(NO_RESPONSE);
|
||||
let bob = new_attendee()
|
||||
.with_contact_id(2)
|
||||
.with_status(ACCEPTED);
|
||||
let charlie = new_attendee()
|
||||
.with_contact_id(3)
|
||||
.with_status(TENTATIVE);
|
||||
|
||||
|
||||
// Create a new event using builder pattern
|
||||
// Note: Timestamps are in seconds since epoch
|
||||
let now = timestamp_now();
|
||||
let one_hour = 60 * 60;
|
||||
let meeting = new_event()
|
||||
.title("Weekly Sync")
|
||||
.reschedule(now, now + one_hour)
|
||||
.location("Conference Room A")
|
||||
.description("Regular team sync meeting")
|
||||
.add_attendee(alice)
|
||||
.add_attendee(bob)
|
||||
.add_attendee(charlie)
|
||||
.save_event();
|
||||
|
||||
print(`Created event: ${meeting.title}`);
|
||||
|
||||
meeting.delete_event();
|
||||
|
||||
print(`Deleted event: ${meeting.title}`);
|
||||
|
||||
// Print attendees info
|
||||
let attendees = meeting.attendees;
|
||||
print(`Added attendees to the event`);
|
||||
|
||||
// Update Charlie's attendee status directly
|
||||
meeting.update_attendee_status(3, ACCEPTED);
|
||||
print(`Updated Charlie's status to: ${ACCEPTED}`);
|
||||
|
||||
// Add the event to the calendar
|
||||
my_calendar.add_event_to_calendar(meeting);
|
||||
// Print events info
|
||||
print(`Added event to calendar`);
|
||||
|
||||
// Save the calendar to the database
|
||||
let saved_calendar = my_calendar.save_calendar();
|
||||
print(`Calendar saved to database with ID: ${saved_calendar.id}`);
|
||||
|
||||
// Retrieve the calendar from the database using the ID from the saved calendar
|
||||
let retrieved_calendar = get_calendar_by_id(saved_calendar.id);
|
||||
if retrieved_calendar != () {
|
||||
print(`Retrieved calendar: ${retrieved_calendar.name}`);
|
||||
print(`Retrieved calendar successfully`);
|
||||
} else {
|
||||
print("Failed to retrieve calendar from database");
|
||||
}
|
||||
|
||||
// List all calendars in the database
|
||||
let all_calendars = list_calendars();
|
||||
print("\nListing all calendars in database:");
|
||||
let calendar_count = 0;
|
||||
for calendar in all_calendars {
|
||||
print(` - Calendar: ${calendar.name} (ID: ${calendar.id})`);
|
||||
calendar_count += 1;
|
||||
}
|
||||
print(`Total calendars: ${calendar_count}`);
|
||||
|
||||
// List all events in the database
|
||||
let all_events = list_events();
|
||||
print("\nListing all events in database:");
|
||||
let event_count = 0;
|
||||
for event in all_events {
|
||||
print(` - Event: ${event.title} (ID: ${event.id})`);
|
||||
event_count += 1;
|
||||
}
|
||||
print(`Total events: ${event_count}`);
|
||||
|
||||
// Helper function to get current timestamp
|
||||
fn timestamp_now() {
|
||||
// This would typically be provided by the host application
|
||||
// For this example, we'll use a fixed timestamp
|
||||
1685620800 // June 1, 2023, 12:00 PM
|
||||
}
|
66
engine/examples/calendar/example.rs
Normal file
66
engine/examples/calendar/example.rs
Normal file
@@ -0,0 +1,66 @@
|
||||
use std::sync::Arc;
|
||||
use std::path::Path;
|
||||
use rhai::{Engine, Scope};
|
||||
use heromodels::models::calendar::{Calendar, Event, Attendee, AttendanceStatus};
|
||||
use engine::{create_heromodels_engine, eval_file};
|
||||
use engine::mock_db::{create_mock_db, seed_mock_db};
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!("Calendar Rhai Example");
|
||||
println!("=====================");
|
||||
|
||||
// Create a mock database
|
||||
let db = create_mock_db();
|
||||
|
||||
// Seed the database with some initial data
|
||||
seed_mock_db(db.clone());
|
||||
|
||||
// Create the Rhai engine using our central engine creator
|
||||
let mut engine = create_heromodels_engine(db.clone());
|
||||
|
||||
// Register timestamp helper functions
|
||||
register_timestamp_helpers(&mut engine);
|
||||
|
||||
// Get the path to the script
|
||||
let script_path = Path::new(file!())
|
||||
.parent()
|
||||
.unwrap()
|
||||
.join("calendar_script.rhai");
|
||||
|
||||
println!("\nRunning script: {}", script_path.display());
|
||||
println!("---------------------");
|
||||
|
||||
// Run the script
|
||||
match eval_file(&engine, &script_path.to_string_lossy()) {
|
||||
Ok(result) => {
|
||||
if !result.is_unit() {
|
||||
println!("\nScript returned: {:?}", result);
|
||||
}
|
||||
println!("\nScript executed successfully!");
|
||||
Ok(())
|
||||
},
|
||||
Err(err) => {
|
||||
eprintln!("\nError running script: {}", err);
|
||||
Err(Box::new(std::io::Error::new(std::io::ErrorKind::Other, err.to_string())))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Register timestamp helper functions with the engine
|
||||
fn register_timestamp_helpers(engine: &mut Engine) {
|
||||
use chrono::{DateTime, Utc, TimeZone, NaiveDateTime};
|
||||
|
||||
// Function to get current timestamp
|
||||
engine.register_fn("timestamp_now", || {
|
||||
Utc::now().timestamp() as i64
|
||||
});
|
||||
|
||||
// Function to format a timestamp
|
||||
engine.register_fn("format_timestamp", |ts: i64| {
|
||||
let dt = Utc.timestamp_opt(ts, 0).single()
|
||||
.expect("Invalid timestamp");
|
||||
dt.format("%Y-%m-%d %H:%M:%S UTC").to_string()
|
||||
});
|
||||
|
||||
println!("Timestamp helper functions registered successfully.");
|
||||
}
|
Reference in New Issue
Block a user