use derive::FromVec; use heromodels::db::Db; use macros::{ register_authorized_create_by_id_fn, register_authorized_delete_by_id_fn, register_authorized_get_by_id_fn, register_authorized_list_fn, }; use rhai::plugin::*; use rhai::{CustomType, Dynamic, Engine, EvalAltResult, Module, Position, TypeBuilder}; use serde::Serialize; use serde_json; use std::mem; use std::sync::Arc; use heromodels::db::hero::OurDB; use heromodels::db::Collection as DbCollectionTrait; use heromodels::models::library::collection::Collection as RhaiCollection; use heromodels::models::library::items::{ Book as RhaiBook, Image as RhaiImage, Markdown as RhaiMarkdown, Pdf as RhaiPdf, Slide as RhaiSlide, Slideshow as RhaiSlideshow, TocEntry as RhaiTocEntry, }; /// Registers a `.json()` method for any type `T` that implements the required traits. fn register_json_method(engine: &mut Engine) where T: CustomType + Clone + Serialize, { let to_json_fn = |obj: &mut T| -> Result> { match serde_json::to_string_pretty(obj) { Ok(json_str) => Ok(json_str), Err(e) => Err(format!("Failed to serialize to JSON: {}", e).into()), } }; engine.register_fn("json", to_json_fn); } // Wrapper types for arrays #[derive(Debug, Clone, Serialize, CustomType, FromVec)] #[rhai_type(name = "CollectionArray")] pub struct RhaiCollectionArray(pub Vec); #[derive(Debug, Clone, Serialize, CustomType, FromVec)] #[rhai_type(name = "ImageArray")] pub struct RhaiImageArray(pub Vec); #[derive(Debug, Clone, Serialize, CustomType, FromVec)] #[rhai_type(name = "PdfArray")] pub struct RhaiPdfArray(pub Vec); #[derive(Debug, Clone, Serialize, CustomType, FromVec)] #[rhai_type(name = "MarkdownArray")] pub struct RhaiMarkdownArray(pub Vec); #[derive(Debug, Clone, Serialize, CustomType, FromVec)] #[rhai_type(name = "BookArray")] pub struct RhaiBookArray(pub Vec); #[derive(Debug, Clone, Serialize, CustomType, FromVec)] #[rhai_type(name = "SlideshowArray")] pub struct RhaiSlideshowArray(pub Vec); #[derive(Debug, Clone, Serialize, CustomType, FromVec)] #[rhai_type(name = "TocEntryArray")] pub struct RhaiTocEntryArray(pub Vec); #[export_module] mod rhai_library_module { use super::*; // --- Collection Functions --- #[rhai_fn(name = "new_collection", return_raw)] pub fn new_collection() -> Result> { Ok(RhaiCollection::new()) } #[rhai_fn(name = "collection_title", return_raw)] pub fn collection_title( collection: &mut RhaiCollection, title: String, ) -> Result> { let owned = std::mem::take(collection); *collection = owned.title(title); Ok(collection.clone()) } #[rhai_fn(name = "collection_description", return_raw)] pub fn collection_description( collection: &mut RhaiCollection, description: String, ) -> Result> { let owned = std::mem::take(collection); *collection = owned.description(description); Ok(collection.clone()) } #[rhai_fn(name = "get_collection_id")] pub fn get_collection_id(collection: &mut RhaiCollection) -> i64 { collection.id() as i64 } #[rhai_fn(name = "get_collection_title")] pub fn get_collection_title(collection: &mut RhaiCollection) -> String { collection.title().clone() } // --- Image Functions --- #[rhai_fn(name = "new_image", return_raw)] pub fn new_image() -> Result> { Ok(RhaiImage::new()) } #[rhai_fn(name = "image_title", return_raw)] pub fn image_title( image: &mut RhaiImage, title: String, ) -> Result> { let owned = std::mem::take(image); *image = owned.title(title); Ok(image.clone()) } #[rhai_fn(name = "get_image_id")] pub fn get_image_id(image: &mut RhaiImage) -> i64 { image.id() as i64 } // Additional functions would continue here... } pub fn register_library_rhai_module(engine: &mut Engine) { let mut module = exported_module!(rhai_library_module); register_json_method::(engine); register_json_method::(engine); register_json_method::(engine); register_json_method::(engine); register_json_method::(engine); register_json_method::(engine); register_json_method::(engine); register_json_method::(engine); register_authorized_create_by_id_fn!( module: &mut module, rhai_fn_name: "save_collection", resource_type_str: "Collection", rhai_return_rust_type: heromodels::models::library::collection::Collection ); register_authorized_get_by_id_fn!( module: &mut module, rhai_fn_name: "get_collection", resource_type_str: "Collection", rhai_return_rust_type: heromodels::models::library::collection::Collection ); engine.register_global_module(module.into()); }