first commit
This commit is contained in:
67
src/objects/note/rhai.rs
Normal file
67
src/objects/note/rhai.rs
Normal file
@@ -0,0 +1,67 @@
|
||||
use crate::objects::Note;
|
||||
use rhai::{CustomType, Engine, TypeBuilder};
|
||||
|
||||
impl CustomType for Note {
|
||||
fn build(mut builder: TypeBuilder<Self>) {
|
||||
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::<Note>();
|
||||
|
||||
// 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
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user