Move package registration functions to respective rhai modules

- Moved register_note_functions to objects/note/rhai.rs
- Moved register_event_functions to objects/event/rhai.rs
- Updated engine.rs to import these functions from their modules
- Better code organization with functions co-located with their types
This commit is contained in:
Timur Gordon
2025-10-28 12:34:11 +01:00
parent e760a184b1
commit 03e5615541
3 changed files with 63 additions and 62 deletions

View File

@@ -3,68 +3,11 @@
/// Creates a Rhai engine configured with OSIRIS contexts and methods.
use crate::context::OsirisContext;
use crate::objects::{Note, Event};
use rhai::{Engine, Module, def_package, FuncRegistration};
use crate::objects::note::rhai::register_note_functions;
use crate::objects::event::rhai::register_event_functions;
use rhai::{Engine, def_package, FuncRegistration};
use rhai::packages::{Package, StandardPackage};
/// Register Note functions into a module
fn register_note_functions(module: &mut Module) {
// Register Note type
module.set_custom_type::<Note>("Note");
// Register builder-style constructor
FuncRegistration::new("note")
.set_into_module(module, |ns: String| Note::new(ns));
// Register chainable methods that return Self
FuncRegistration::new("title")
.set_into_module(module, |mut note: Note, title: String| {
note.title = Some(title);
note.base_data.update_modified();
note
});
FuncRegistration::new("content")
.set_into_module(module, |mut note: Note, content: String| {
let size = content.len() as u64;
note.content = Some(content);
note.base_data.set_size(Some(size));
note.base_data.update_modified();
note
});
FuncRegistration::new("tag")
.set_into_module(module, |mut note: Note, key: String, value: String| {
note.tags.insert(key, value);
note.base_data.update_modified();
note
});
FuncRegistration::new("mime")
.set_into_module(module, |mut note: Note, mime: String| {
note.base_data.set_mime(Some(mime));
note
});
}
/// Register Event functions into a module
fn register_event_functions(module: &mut Module) {
// Register Event type
module.set_custom_type::<Event>("Event");
// Register builder-style constructor
FuncRegistration::new("event")
.set_into_module(module, |ns: String, title: String| Event::new(ns, title));
// Register chainable methods
FuncRegistration::new("description")
.set_into_module(module, |mut event: Event, desc: String| {
event.description = Some(desc);
event.base_data.update_modified();
event
});
}
/// Register get_context function in a Rhai engine with signatory-based access control
///
/// Simple logic:

View File

@@ -1,5 +1,5 @@
use crate::objects::Event;
use rhai::{CustomType, Engine, TypeBuilder};
use rhai::{CustomType, Engine, TypeBuilder, Module, FuncRegistration};
impl CustomType for Event {
fn build(mut builder: TypeBuilder<Self>) {
@@ -69,3 +69,21 @@ pub fn register_event_api(engine: &mut Engine) {
event
});
}
/// Register Event functions into a module (for use in packages)
pub fn register_event_functions(module: &mut Module) {
// Register Event type
module.set_custom_type::<Event>("Event");
// Register builder-style constructor
FuncRegistration::new("event")
.set_into_module(module, |ns: String, title: String| Event::new(ns, title));
// Register chainable methods
FuncRegistration::new("description")
.set_into_module(module, |mut event: Event, desc: String| {
event.description = Some(desc);
event.base_data.update_modified();
event
});
}

View File

@@ -1,5 +1,5 @@
use crate::objects::Note;
use rhai::{CustomType, Engine, TypeBuilder};
use rhai::{CustomType, Engine, TypeBuilder, Module, FuncRegistration};
impl CustomType for Note {
fn build(mut builder: TypeBuilder<Self>) {
@@ -65,3 +65,43 @@ pub fn register_note_api(engine: &mut Engine) {
note
});
}
/// Register Note functions into a module (for use in packages)
pub fn register_note_functions(module: &mut Module) {
// Register Note type
module.set_custom_type::<Note>("Note");
// Register builder-style constructor
FuncRegistration::new("note")
.set_into_module(module, |ns: String| Note::new(ns));
// Register chainable methods that return Self
FuncRegistration::new("title")
.set_into_module(module, |mut note: Note, title: String| {
note.title = Some(title);
note.base_data.update_modified();
note
});
FuncRegistration::new("content")
.set_into_module(module, |mut note: Note, content: String| {
let size = content.len() as u64;
note.content = Some(content);
note.base_data.set_size(Some(size));
note.base_data.update_modified();
note
});
FuncRegistration::new("tag")
.set_into_module(module, |mut note: Note, key: String, value: String| {
note.tags.insert(key, value);
note.base_data.update_modified();
note
});
FuncRegistration::new("mime")
.set_into_module(module, |mut note: Note, mime: String| {
note.base_data.set_mime(Some(mime));
note
});
}