Files
osiris/src/objects/event/rhai.rs
Timur Gordon e04012c8c0 Refactor Rhai integration with context-based execution and type registry
Major Changes:
- Moved Rhai support from rhai_support/ to rhai/ module
- Implemented context-based execution with signatory access control
- Added TypeRegistry for dynamic type registration and object creation
- Refactored engine to use context (Vec<String>) instead of instance
- Removed old runner binary (moved to runner_rust crate)

Rhai Module:
- engine.rs: Core Rhai engine with context-based get_context()
- functions.rs: Rhai function bindings (create_note, create_event, etc.)
- mod.rs: Module exports and organization

Store Improvements:
- TypeRegistry for registering object types and creators
- Generic store uses type registry for dynamic object creation
- Improved error handling and type safety

Documentation:
- RHAI_REFACTOR_COMPLETE.md: Refactoring details
- SIGNATORY_ACCESS_CONTROL.md: Context-based access control
- TYPE_REGISTRY_DESIGN.md: Type registry architecture
- REFACTORING_COMPLETE.md: Overall refactoring summary
- TESTS_COMPLETE.md: Testing documentation

Build Status:  Compiles successfully with minor warnings
2025-10-28 03:33:39 +01:00

72 lines
2.5 KiB
Rust

use crate::objects::Event;
use rhai::{CustomType, Engine, TypeBuilder};
impl CustomType for Event {
fn build(mut builder: TypeBuilder<Self>) {
builder
.with_name("Event")
.with_fn("new", |ns: String, title: String| Event::new(ns, title))
.with_fn("set_description", |event: &mut Event, desc: String| {
event.description = Some(desc);
event.base_data.update_modified();
})
.with_fn("set_location", |event: &mut Event, location: String| {
event.location = Some(location);
event.base_data.update_modified();
})
.with_fn("set_category", |event: &mut Event, category: String| {
event.category = Some(category);
event.base_data.update_modified();
})
.with_fn("set_all_day", |event: &mut Event, all_day: bool| {
event.all_day = all_day;
event.base_data.update_modified();
})
.with_fn("get_id", |event: &mut Event| event.base_data.id.clone())
.with_fn("get_title", |event: &mut Event| event.title.clone())
.with_fn("to_json", |event: &mut Event| {
serde_json::to_string_pretty(event).unwrap_or_default()
});
}
}
/// Register Event API in Rhai engine
pub fn register_event_api(engine: &mut Engine) {
engine.build_type::<Event>();
// Register builder-style constructor (namespace only, like note())
engine.register_fn("event", |ns: String| Event::new(ns, String::new()));
// Register title as a chainable method
engine.register_fn("title", |mut event: Event, title: String| {
event.title = title;
event.base_data.update_modified();
event
});
// Register chainable methods that return Self
engine.register_fn("description", |mut event: Event, desc: String| {
event.description = Some(desc);
event.base_data.update_modified();
event
});
engine.register_fn("location", |mut event: Event, location: String| {
event.location = Some(location);
event.base_data.update_modified();
event
});
engine.register_fn("category", |mut event: Event, category: String| {
event.category = Some(category);
event.base_data.update_modified();
event
});
engine.register_fn("all_day", |mut event: Event, all_day: bool| {
event.all_day = all_day;
event.base_data.update_modified();
event
});
}