use crate::objects::Note; use rhai::{CustomType, Engine, TypeBuilder, Module, FuncRegistration}; impl CustomType for Note { fn build(mut builder: TypeBuilder) { builder .with_name("Note") .with_fn("new", |ns: String| Note::new(ns)) .with_fn("set_title", |note: &mut Note, title: String| { note.title = Some(title); note.base_data.update_modified(); }) .with_fn("set_content", |note: &mut 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(); }) .with_fn("add_tag", |note: &mut Note, key: String, value: String| { note.tags.insert(key, value); note.base_data.update_modified(); }) .with_fn("set_mime", |note: &mut Note, mime: String| { note.base_data.set_mime(Some(mime)); }) .with_fn("get_id", |note: &mut Note| note.base_data.id.clone()) .with_fn("get_title", |note: &mut Note| note.title.clone().unwrap_or_default()) .with_fn("get_content", |note: &mut Note| note.content.clone().unwrap_or_default()) .with_fn("to_json", |note: &mut Note| { serde_json::to_string_pretty(note).unwrap_or_default() }); } } /// Register Note API in Rhai engine pub fn register_note_api(engine: &mut Engine) { engine.build_type::(); // Register builder-style constructor engine.register_fn("note", |ns: String| Note::new(ns)); // Register chainable methods that return Self engine.register_fn("title", |mut note: Note, title: String| { note.title = Some(title); note.base_data.update_modified(); note }); engine.register_fn("content", |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 }); engine.register_fn("tag", |mut note: Note, key: String, value: String| { note.tags.insert(key, value); note.base_data.update_modified(); note }); engine.register_fn("mime", |mut note: Note, mime: String| { note.base_data.set_mime(Some(mime)); 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"); // 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 }); }